Spaces:
Running
Running
File size: 4,315 Bytes
7d30bd0 95ebd02 765202e 95ebd02 765202e 95ebd02 d73c837 55a9b45 95ebd02 d73c837 95ebd02 55a9b45 7d30bd0 95ebd02 d73c837 95ebd02 55a9b45 95ebd02 f10a5b6 95ebd02 d73c837 7c2fcfb f10a5b6 d73c837 9385a19 d73c837 7c2fcfb d73c837 95ebd02 d73c837 95ebd02 7d30bd0 7569161 7d30bd0 7c2fcfb d73c837 |
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
from flask import Flask, render_template, request, jsonify, Response, send_file
from flask_cors import CORS
import internetarchive as ia
import os
from datetime import datetime
import uuid
import threading
import time
import requests
app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}})
@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
response.headers.add('Access-Control-Allow-Methods', 'GET,POST,OPTIONS')
return response
# Optional background ping to keep Render service alive
def ping_file2link():
while True:
try:
response = requests.get('https://file2link-ol4p.onrender.com')
print(f"[{datetime.now()}] Ping status: {response.status_code}")
except Exception as e:
print(f"[{datetime.now()}] Ping error: {str(e)}")
time.sleep(10)
threading.Thread(target=ping_file2link, daemon=True).start()
@app.route('/')
def index():
return render_template('index.html')
@app.route('/upload', methods=['POST', 'OPTIONS'])
def upload_file():
if request.method == 'OPTIONS':
return '', 200
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
if file:
item_id = 'VELTRIX_DB'
original_filename = file.filename
file_extension = os.path.splitext(original_filename)[1]
unique_filename = f"{uuid.uuid4()}{file_extension}"
try:
file.seek(0)
r = ia.upload(
item_id,
files={unique_filename: file},
metadata=None,
access_key='YXzY5OrREXLL6XBh',
secret_key='m2XnL3X7xCB1pNGE',
retries=5,
verbose=True
)
if r[0].status_code == 200:
hf_url = f'https://shashwatidrnpo-inv.hf.space/files/serve/veltrixdbforsld0110/2c16gb/live/{unique_filename}'
return jsonify({
'success': True,
'access_url': hf_url,
'item_id': item_id,
'original_filename': original_filename,
'unique_filename': unique_filename
})
else:
return jsonify({'error': 'Upload failed', 'details': r[0].text}), 500
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/files/serve/<path:folder1>/<path:folder2>/<path:folder3>/<filename>')
def serve_restricted_file(folder1, folder2, folder3, filename):
# Full path mapping from route
archive_url = f'https://archive.org/download/VELTRIX_DB/{filename}'
# Only allow from specific domains
allowed_origins = ['https://gcx.co.in', 'https://gcx-admin.vercel.app']
referer = request.headers.get('Referer', '')
origin = request.headers.get('Origin', '')
is_allowed = any(
referer.startswith(allowed) or origin.startswith(allowed)
for allowed in allowed_origins
)
if not is_allowed:
# Serve fallback file from root
fallback_path = os.path.join(os.getcwd(), 'no.mp4')
if os.path.exists(fallback_path):
return send_file(
fallback_path,
mimetype='video/mp4',
download_name='no.mp4',
as_attachment=False
)
else:
return "not allowed", 403
try:
r = requests.get(archive_url, stream=True)
return Response(
r.iter_content(chunk_size=8192),
headers={
'Content-Type': r.headers.get('Content-Type', 'application/octet-stream'),
'Content-Disposition': f'inline; filename="{filename}"',
'Accept-Ranges': 'bytes',
'Cache-Control': 'public, max-age=604800'
},
status=r.status_code
)
except Exception as e:
return jsonify({'error': str(e)}), 500
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port, debug=True)
|