File size: 1,866 Bytes
0491fc6 33f8f7c 0491fc6 6048a58 059bbb9 6048a58 059bbb9 6048a58 33f8f7c 07d1566 6048a58 0491fc6 6048a58 0491fc6 6048a58 daa26e9 6048a58 07d1566 6048a58 059bbb9 ae24aac 6048a58 |
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 51 52 53 54 55 56 57 58 59 60 61 |
from flask import Flask, request, jsonify, Response
import random
import string
import threading
from queue import Queue
app = Flask(__name__)
transfer_queues = {}
lock = threading.Lock()
def generate_id():
return ''.join(random.choices(string.ascii_letters + string.digits, k=8))
@app.route('/create_transfer', methods=['POST'])
def create_transfer():
transfer_id = generate_id()
with lock:
transfer_queues[transfer_id] = {
'queue': Queue(maxsize=10),
'filename': request.json['filename'],
'active': False
}
return jsonify({'transfer_id': transfer_id})
@app.route('/upload/<transfer_id>', methods=['POST'])
def upload(transfer_id):
if transfer_id not in transfer_queues:
return jsonify({'error': 'Invalid ID'}), 404
chunk = request.data
try:
transfer_queues[transfer_id]['queue'].put(chunk, timeout=30)
return jsonify({'status': 'ok'})
except:
return jsonify({'error': 'Receiver not connected'}), 408
@app.route('/download/<transfer_id>')
def download(transfer_id):
def generate():
with lock:
if transfer_id not in transfer_queues:
yield 'Transfer ID invalid'
return
transfer = transfer_queues[transfer_id]
transfer['active'] = True
try:
while True:
chunk = transfer['queue'].get(timeout=30)
yield chunk
except:
transfer_queues.pop(transfer_id, None)
headers = {
'Content-Disposition': f'attachment; filename="{transfer_queues[transfer_id]["filename"]}"'
}
return Response(generate(), mimetype='application/octet-stream', headers=headers)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, threaded=True) |