Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from flask import Flask, render_template, request, jsonify
|
2 |
from flask_cors import CORS
|
3 |
import internetarchive as ia
|
4 |
import os
|
@@ -7,7 +7,6 @@ import uuid
|
|
7 |
import threading
|
8 |
import time
|
9 |
import requests
|
10 |
-
import json
|
11 |
|
12 |
app = Flask(__name__)
|
13 |
CORS(app, resources={r"/*": {"origins": "*"}})
|
@@ -19,6 +18,7 @@ def after_request(response):
|
|
19 |
response.headers.add('Access-Control-Allow-Methods', 'GET,POST,OPTIONS')
|
20 |
return response
|
21 |
|
|
|
22 |
def ping_file2link():
|
23 |
while True:
|
24 |
try:
|
@@ -28,6 +28,7 @@ def ping_file2link():
|
|
28 |
print(f"[{datetime.now()}] Ping error: {str(e)}")
|
29 |
time.sleep(10)
|
30 |
|
|
|
31 |
threading.Thread(target=ping_file2link, daemon=True).start()
|
32 |
|
33 |
@app.route('/')
|
@@ -57,7 +58,7 @@ def upload_file():
|
|
57 |
r = ia.upload(
|
58 |
item_id,
|
59 |
files={unique_filename: file},
|
60 |
-
metadata=None,
|
61 |
access_key='YXzY5OrREXLL6XBh',
|
62 |
secret_key='m2XnL3X7xCB1pNGE',
|
63 |
retries=5,
|
@@ -65,10 +66,11 @@ def upload_file():
|
|
65 |
)
|
66 |
|
67 |
if r[0].status_code == 200:
|
68 |
-
|
|
|
69 |
return jsonify({
|
70 |
'success': True,
|
71 |
-
'access_url':
|
72 |
'item_id': item_id,
|
73 |
'original_filename': original_filename,
|
74 |
'unique_filename': unique_filename
|
@@ -79,6 +81,24 @@ def upload_file():
|
|
79 |
except Exception as e:
|
80 |
return jsonify({'error': str(e)}), 500
|
81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
if __name__ == '__main__':
|
83 |
port = int(os.environ.get('PORT', 5000))
|
84 |
app.run(host='0.0.0.0', port=port, debug=True)
|
|
|
1 |
+
from flask import Flask, render_template, request, jsonify, Response
|
2 |
from flask_cors import CORS
|
3 |
import internetarchive as ia
|
4 |
import os
|
|
|
7 |
import threading
|
8 |
import time
|
9 |
import requests
|
|
|
10 |
|
11 |
app = Flask(__name__)
|
12 |
CORS(app, resources={r"/*": {"origins": "*"}})
|
|
|
18 |
response.headers.add('Access-Control-Allow-Methods', 'GET,POST,OPTIONS')
|
19 |
return response
|
20 |
|
21 |
+
# Background pinger to keep file2link warm
|
22 |
def ping_file2link():
|
23 |
while True:
|
24 |
try:
|
|
|
28 |
print(f"[{datetime.now()}] Ping error: {str(e)}")
|
29 |
time.sleep(10)
|
30 |
|
31 |
+
# Start the pinger thread
|
32 |
threading.Thread(target=ping_file2link, daemon=True).start()
|
33 |
|
34 |
@app.route('/')
|
|
|
58 |
r = ia.upload(
|
59 |
item_id,
|
60 |
files={unique_filename: file},
|
61 |
+
metadata=None,
|
62 |
access_key='YXzY5OrREXLL6XBh',
|
63 |
secret_key='m2XnL3X7xCB1pNGE',
|
64 |
retries=5,
|
|
|
66 |
)
|
67 |
|
68 |
if r[0].status_code == 200:
|
69 |
+
# Instead of Archive.org link, return masked Hugging Face Space URL
|
70 |
+
hf_url = f'https://shashwatidrnpo-inv.hf.space/files/serve/{unique_filename}'
|
71 |
return jsonify({
|
72 |
'success': True,
|
73 |
+
'access_url': hf_url,
|
74 |
'item_id': item_id,
|
75 |
'original_filename': original_filename,
|
76 |
'unique_filename': unique_filename
|
|
|
81 |
except Exception as e:
|
82 |
return jsonify({'error': str(e)}), 500
|
83 |
|
84 |
+
@app.route('/files/serve/<filename>')
|
85 |
+
def serve_file(filename):
|
86 |
+
archive_url = f'https://archive.org/download/VELTRIX_DB/{filename}'
|
87 |
+
try:
|
88 |
+
r = requests.get(archive_url, stream=True)
|
89 |
+
return Response(
|
90 |
+
r.iter_content(chunk_size=8192),
|
91 |
+
headers={
|
92 |
+
'Content-Type': r.headers.get('Content-Type', 'application/octet-stream'),
|
93 |
+
'Content-Disposition': f'inline; filename="{filename}"',
|
94 |
+
'Accept-Ranges': 'bytes',
|
95 |
+
'Cache-Control': 'public, max-age=604800'
|
96 |
+
},
|
97 |
+
status=r.status_code
|
98 |
+
)
|
99 |
+
except Exception as e:
|
100 |
+
return jsonify({'error': str(e)}), 500
|
101 |
+
|
102 |
if __name__ == '__main__':
|
103 |
port = int(os.environ.get('PORT', 5000))
|
104 |
app.run(host='0.0.0.0', port=port, debug=True)
|