|
import os |
|
from flask import Flask, send_file |
|
from flask_socketio import SocketIO |
|
import threading, subprocess |
|
|
|
app = Flask(__name__) |
|
socketio = SocketIO(app, async_mode='eventlet') |
|
base_dir = os.path.abspath('files') |
|
|
|
subprocess.Popen(["venv/bin/python", "main.py"]) |
|
|
|
@socketio.on('request_dir_structure') |
|
def send_dir_structure(): |
|
structure = {} |
|
for root, dirs, files in os.walk(base_dir): |
|
rel_path = os.path.relpath(root, base_dir) |
|
structure[rel_path] = { |
|
'dirs': dirs, |
|
'files': [{'name': f, 'size': os.path.getsize(os.path.join(root, f))} for f in files] |
|
} |
|
socketio.emit('dir_structure', structure) |
|
|
|
@socketio.on('download_file') |
|
def send_file_chunk(data): |
|
file_path = os.path.join(base_dir, data['path']) |
|
offset = data.get('offset', 0) |
|
chunk_size = 1024 * 64 |
|
|
|
with open(file_path, 'rb') as f: |
|
f.seek(offset) |
|
while True: |
|
chunk = f.read(chunk_size) |
|
if not chunk: |
|
break |
|
socketio.emit('file_chunk', { |
|
'path': data['path'], |
|
'offset': f.tell(), |
|
'data': chunk |
|
}) |
|
socketio.sleep(0.01) |
|
|
|
if __name__ == '__main__': |
|
socketio.run(app, port=7860) |