Spaces:
Running
Running
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": "*"}}) | |
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() | |
def index(): | |
return render_template('index.html') | |
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 | |
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) | |