Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,21 +1,92 @@
|
|
1 |
-
import
|
2 |
-
from
|
3 |
-
|
4 |
-
import torch
|
5 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
login(token)
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
14 |
|
15 |
-
def
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, render_template, request, jsonify, Response
|
2 |
+
from flask_cors import CORS
|
3 |
+
import internetarchive as ia
|
|
|
4 |
import os
|
5 |
+
from datetime import datetime
|
6 |
+
import uuid
|
7 |
+
import threading
|
8 |
+
import time
|
9 |
+
import requests
|
10 |
+
import json
|
11 |
|
12 |
+
app = Flask(__name__)
|
13 |
+
CORS(app)
|
|
|
14 |
|
15 |
+
@app.after_request
|
16 |
+
def after_request(response):
|
17 |
+
response.headers.add('Access-Control-Allow-Origin', '*')
|
18 |
+
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
|
19 |
+
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
|
20 |
+
return response
|
21 |
|
22 |
+
def ping_file2link():
|
23 |
+
while True:
|
24 |
+
try:
|
25 |
+
response = requests.get('https://file2link-3pse.onrender.com')
|
26 |
+
print(f"[{datetime.now()}] Ping status: {response.status_code}")
|
27 |
+
except Exception as e:
|
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('/')
|
34 |
+
def index():
|
35 |
+
return render_template('index.html')
|
36 |
+
|
37 |
+
@app.route('/upload', methods=['POST', 'OPTIONS'])
|
38 |
+
def upload_file():
|
39 |
+
if request.method == 'OPTIONS':
|
40 |
+
return '', 200
|
41 |
+
|
42 |
+
if 'file' not in request.files:
|
43 |
+
return jsonify({'error': 'No file part'}), 400
|
44 |
+
|
45 |
+
file = request.files['file']
|
46 |
+
if file.filename == '':
|
47 |
+
return jsonify({'error': 'No selected file'}), 400
|
48 |
+
|
49 |
+
if file:
|
50 |
+
item_id = 'shop_proj'
|
51 |
+
original_filename = file.filename
|
52 |
+
file_extension = os.path.splitext(original_filename)[1]
|
53 |
+
unique_filename = f"{uuid.uuid4()}{file_extension}"
|
54 |
+
|
55 |
+
metadata = {
|
56 |
+
'title': f'File2Link Upload - {original_filename}',
|
57 |
+
'mediatype': 'data',
|
58 |
+
'collection': 'opensource',
|
59 |
+
'description': f'Original filename: {original_filename}',
|
60 |
+
'creator': 'File2Link Uploader',
|
61 |
+
'date': datetime.now().strftime('%Y-%m-%d'),
|
62 |
+
'licenseurl': 'https://creativecommons.org/licenses/by/4.0/',
|
63 |
+
'subject': ['file2link', 'file sharing'],
|
64 |
+
'original_filename': original_filename
|
65 |
+
}
|
66 |
+
|
67 |
+
def generate_progress():
|
68 |
+
try:
|
69 |
+
file.seek(0)
|
70 |
+
r = ia.upload(
|
71 |
+
item_id,
|
72 |
+
files={unique_filename: file},
|
73 |
+
metadata=metadata,
|
74 |
+
access_key='PrJnoIKjNt4ul1Fr', # <-- HARDCODED
|
75 |
+
secret_key='S0tCXWb7fM43m44Y' # <-- HARDCODED
|
76 |
+
)
|
77 |
+
if r[0].status_code == 200:
|
78 |
+
access_url = f'https://archive.org/download/{item_id}/{unique_filename}'
|
79 |
+
return json.dumps({
|
80 |
+
'success': True,
|
81 |
+
'access_url': access_url,
|
82 |
+
'item_id': item_id,
|
83 |
+
'original_filename': original_filename,
|
84 |
+
'unique_filename': unique_filename,
|
85 |
+
'metadata': metadata
|
86 |
+
})
|
87 |
+
else:
|
88 |
+
return json.dumps({'error': 'Upload failed'})
|
89 |
+
except Exception as e:
|
90 |
+
return json.dumps({'error': str(e)})
|
91 |
+
|
92 |
+
return Response(generate_progress(), mimetype='application/json')
|