Guest User

Upstream.py

a guest
Sep 12th, 2025
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. from flask import Flask, request, Response
  2. import os
  3. import re
  4.  
  5. app = Flask(__name__)
  6.  
  7. @app.route('/download/<filename>')
  8. def dmain_download(filename):
  9.     return download_file(filename)
  10.  
  11. def download_file(filename):
  12.     file_path = os.path.join('/tmp', filename) # Update this, if the directory is different
  13.     test_header = request.headers.get('Test-Header')
  14.  
  15.     if not os.path.exists(file_path):
  16.         return "File not found", 404
  17.     file_size = os.path.getsize(file_path)
  18.  
  19.     print("------------------------------------------------------------------------")
  20.     range_header = request.headers.get('Range')
  21.     print(f"Slice Range Header: {range_header}")
  22.     print("------------------------------------------------------------------------")
  23.     if not range_header:
  24.         return send_full_file(file_path, filename, file_size)
  25.  
  26.     try:
  27.         byte1, byte2 = 0, None
  28.  
  29.         m = re.search(r'bytes=(\d+)-(\d*)', range_header)
  30.         if m:
  31.             g = m.groups()
  32.             byte1 = int(g[0])
  33.             if g[1]:
  34.                 byte2 = int(g[1])
  35.  
  36.         # Adjust ranges if they are invalid
  37.         if byte1 >= file_size:
  38.             # If the start byte is greater than or equal to the file size,
  39.             # it's an invalid range request per HTTP RFC.
  40.             return Response(status=416, headers={'Content-Range': f'bytes */{file_size}'})
  41.  
  42.         byte2 = min(byte2 if byte2 else file_size - 1, file_size - 1)  # Clamp byte2 to file size
  43.         length = byte2 - byte1 + 1
  44.  
  45.         with open(file_path, 'rb') as f:
  46.             f.seek(byte1)
  47.             data = f.read(length)
  48.  
  49.         rv = Response(data, 206, mimetype='application/octet-stream', direct_passthrough=True)
  50.         rv.headers['Content-Range'] = f'bytes {byte1}-{byte2}/{file_size}'
  51.         rv.headers['Accept-Ranges'] = 'bytes'
  52.         rv.headers['Content-Length'] = str(length)
  53.         rv.headers['Content-Disposition'] = f'attachment; filename={filename}'
  54.  
  55.         return rv
  56.  
  57.     except Exception as e:
  58.         print(f"Error handling range request: {e}")
  59.         return "Internal Server Error", 500
  60.  
  61.  
  62. def send_full_file(file_path, filename, file_size):
  63.     with open(file_path, 'rb') as f:
  64.         data = f.read()
  65.  
  66.     rv = Response(data, 200, mimetype='application/octet-stream', direct_passthrough=True)
  67.     rv.headers['Accept-Ranges'] = 'bytes'
  68.     rv.headers['Content-Length'] = str(file_size)
  69.     rv.headers['Content-Disposition'] = f'attachment; filename={filename}'
  70.     return rv
  71.  
  72. if __name__ == '__main__':
  73.     app.run(debug=True)
Advertisement
Add Comment
Please, Sign In to add comment