File size: 1,315 Bytes
0c20b62
 
 
4903704
0c20b62
 
 
 
 
4903704
 
0c20b62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4903704
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
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  # 64KB chunks
    
    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)  # Evita bloquear el hilo del servidor

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