File size: 1,622 Bytes
95bd32d
8589307
2851c63
95bd32d
 
8589307
 
2851c63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95bd32d
8589307
 
 
 
 
 
 
 
95bd32d
 
8589307
2851c63
 
 
 
 
 
8589307
 
2851c63
95bd32d
 
8589307
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from flask import Flask, render_template
from flask_socketio import SocketIO, send, emit
import json, subprocess

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, cors_allowed_origins="*")
subprocess.Popen(["venv/bin/python","main.py"])

def list_directory(path):
    result = []
    for root, dirs, files in os.walk(path):
        rel_path = os.path.relpath(root, path)
        if rel_path == ".":
            rel_path = ""
        for file in files:
            file_path = os.path.join(rel_path, file)
            file_full_path = os.path.join(root, file)
            file_size = os.path.getsize(file_full_path)
            result.append({"type": "file", "path": file_path, "size": file_size})
        for dir in dirs:
            dir_path = os.path.join(rel_path, dir)
            dir_full_path = os.path.join(root, dir)
            dir_content = list_directory(dir_full_path)
            result.append({"type": "directory", "path": dir_path, "files": dir_content})
    return result

@socketio.on('connect')
def handle_connect():
    print('Client connected')
    emit('response', {'data': 'Connected'})

@socketio.on('disconnect')
def handle_disconnect():
    print('Client disconnected')

@socketio.on('message')
def handle_message(message):
    if message == "files":
        dir = './files'
        if not os.path.exists(dir):
            os.mkdir(dir)
        files_list = list_directory(directory)
        emit("files",files_list)
@app.route('/')
def index():
    return json.dumps({"connect":True})

if __name__ == '__main__':
    socketio.run(app, host='0.0.0.0', port=7860)