Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,246 +1,246 @@
|
|
1 |
-
from flask import (
|
2 |
-
Flask,
|
3 |
-
render_template,
|
4 |
-
request,
|
5 |
-
jsonify,
|
6 |
-
redirect,
|
7 |
-
url_for,
|
8 |
-
send_file
|
9 |
-
)
|
10 |
-
from io import BytesIO
|
11 |
-
import urllib.parse
|
12 |
-
from functools import wraps
|
13 |
-
import requests
|
14 |
-
import hashlib
|
15 |
-
import os
|
16 |
-
from config import Config
|
17 |
-
from models import Database
|
18 |
-
from utils import get_file_type, format_file_size
|
19 |
-
from huggingface_hub import HfApi
|
20 |
-
|
21 |
-
# 初始化 HuggingFace API
|
22 |
-
api = HfApi(token=Config.HF_TOKEN)
|
23 |
-
app = Flask(__name__)
|
24 |
-
app.config['SECRET_KEY'] = Config.SECRET_KEY
|
25 |
-
db = Database()
|
26 |
-
|
27 |
-
def require_auth(f):
|
28 |
-
@wraps(f)
|
29 |
-
def decorated(*args, **kwargs):
|
30 |
-
if not Config.REQUIRE_LOGIN:
|
31 |
-
return f(*args, **kwargs)
|
32 |
-
if not request.cookies.get('authenticated'):
|
33 |
-
if request.is_json:
|
34 |
-
return jsonify({'error': 'Unauthorized'}), 401
|
35 |
-
return redirect(url_for('login'))
|
36 |
-
return f(*args, **kwargs)
|
37 |
-
return decorated
|
38 |
-
|
39 |
-
@app.route('/login', methods=['GET', 'POST'])
|
40 |
-
def login():
|
41 |
-
if request.method == 'POST':
|
42 |
-
if request.form.get('password') == Config.ACCESS_PASSWORD:
|
43 |
-
response = jsonify({'success': True})
|
44 |
-
response.set_cookie('authenticated', 'true', secure=True, httponly=True)
|
45 |
-
return response
|
46 |
-
return jsonify({'error': 'Invalid password'}), 401
|
47 |
-
return render_template('login.html')
|
48 |
-
|
49 |
-
@app.route('/logout')
|
50 |
-
def logout():
|
51 |
-
response = redirect(url_for('login'))
|
52 |
-
response.delete_cookie('authenticated')
|
53 |
-
return response
|
54 |
-
|
55 |
-
@app.route('/')
|
56 |
-
@require_auth
|
57 |
-
def index():
|
58 |
-
return render_template('index.html')
|
59 |
-
|
60 |
-
@app.route('/api/files/list/')
|
61 |
-
@app.route('/api/files/list/<path:directory>')
|
62 |
-
@require_auth
|
63 |
-
def list_files(directory=''):
|
64 |
-
try:
|
65 |
-
url = f"https://huggingface.co/api/datasets/{Config.HF_DATASET_ID}/tree/{Config.HF_BRANCH}"
|
66 |
-
if directory:
|
67 |
-
url = f"{url}/{directory}"
|
68 |
-
|
69 |
-
response = requests.get(
|
70 |
-
url,
|
71 |
-
headers={'Authorization': f'Bearer {Config.HF_TOKEN}'}
|
72 |
-
)
|
73 |
-
if not response.ok:
|
74 |
-
return jsonify({'error': 'Failed to fetch files', 'details': response.text}), response.status_code
|
75 |
-
|
76 |
-
files = response.json()
|
77 |
-
for file in files:
|
78 |
-
if file['type'] == 'file':
|
79 |
-
file['file_type'] = get_file_type(file['path'])
|
80 |
-
file['size_formatted'] = format_file_size(file['size'])
|
81 |
-
# 添加预览和下载URL
|
82 |
-
file['preview_url'] = f"/api/files/preview/{file['path']}"
|
83 |
-
file['download_url'] = f"/api/files/download/{file['path']}"
|
84 |
-
|
85 |
-
return jsonify(files)
|
86 |
-
except Exception as e:
|
87 |
-
return jsonify({'error': str(e)}), 500
|
88 |
-
|
89 |
-
@app.route('/api/files/preview/<path:filepath>')
|
90 |
-
@require_auth
|
91 |
-
def preview_file(filepath):
|
92 |
-
try:
|
93 |
-
file_type = get_file_type(filepath)
|
94 |
-
if file_type not in ['image', 'video', 'document']:
|
95 |
-
return jsonify({'error': 'File type not supported for preview'}), 400
|
96 |
-
|
97 |
-
url = f"https://{Config.PROXY_DOMAIN}/datasets/{Config.HF_DATASET_ID}/resolve/{Config.HF_BRANCH}/{filepath}"
|
98 |
-
response = requests.get(
|
99 |
-
url,
|
100 |
-
headers={'Authorization': f'Bearer {Config.HF_TOKEN}'},
|
101 |
-
stream=True
|
102 |
-
)
|
103 |
-
|
104 |
-
if response.ok:
|
105 |
-
# 创建文件的内存缓存
|
106 |
-
file_data = BytesIO(response.content)
|
107 |
-
|
108 |
-
# 根据文件类型返回适当的响应
|
109 |
-
return send_file(
|
110 |
-
file_data,
|
111 |
-
mimetype=response.headers.get('content-type', 'application/octet-stream'),
|
112 |
-
conditional=True # 启用条件请求支持
|
113 |
-
)
|
114 |
-
|
115 |
-
return jsonify({'error': 'Failed to fetch file'}), response.status_code
|
116 |
-
except Exception as e:
|
117 |
-
return jsonify({'error': str(e)}), 500
|
118 |
-
|
119 |
-
|
120 |
-
@app.route('/api/files/download/<path:filepath>')
|
121 |
-
@require_auth
|
122 |
-
def download_file(filepath):
|
123 |
-
try:
|
124 |
-
url = f"https://{Config.PROXY_DOMAIN}/datasets/{Config.HF_DATASET_ID}/resolve/{Config.HF_BRANCH}/{filepath}"
|
125 |
-
response = requests.get(
|
126 |
-
url,
|
127 |
-
headers={'Authorization': f'Bearer {Config.HF_TOKEN}'},
|
128 |
-
stream=True
|
129 |
-
)
|
130 |
-
|
131 |
-
if response.ok:
|
132 |
-
# 创建内存文件对象
|
133 |
-
file_obj = BytesIO(response.content)
|
134 |
-
|
135 |
-
# 获取文件名并进行编码
|
136 |
-
filename = os.path.basename(filepath)
|
137 |
-
encoded_filename = urllib.parse.quote(filename.encode('utf-8'))
|
138 |
-
|
139 |
-
# 使用 send_file 返回文件
|
140 |
-
return send_file(
|
141 |
-
file_obj,
|
142 |
-
download_name=filename,
|
143 |
-
as_attachment=True,
|
144 |
-
mimetype=response.headers.get('content-type', 'application/octet-stream')
|
145 |
-
)
|
146 |
-
|
147 |
-
return jsonify({'error': 'File not found'}), 404
|
148 |
-
except Exception as e:
|
149 |
-
return jsonify({'error': str(e)}), 500
|
150 |
-
|
151 |
-
@app.route('/api/files/upload', methods=['POST'])
|
152 |
-
@require_auth
|
153 |
-
def upload_file():
|
154 |
-
if 'file' not in request.files:
|
155 |
-
return jsonify({'error': 'No file provided'}), 400
|
156 |
-
|
157 |
-
file = request.files['file']
|
158 |
-
current_path = request.form.get('path', '').strip('/')
|
159 |
-
|
160 |
-
try:
|
161 |
-
file_content = file.read()
|
162 |
-
file.seek(0)
|
163 |
-
|
164 |
-
original_name = file.filename
|
165 |
-
stored_name = original_name
|
166 |
-
full_path = os.path.join(current_path, stored_name).replace("\\", "/")
|
167 |
-
|
168 |
-
response = api.upload_file(
|
169 |
-
path_or_fileobj=file_content,
|
170 |
-
path_in_repo=full_path,
|
171 |
-
repo_id=Config.HF_DATASET_ID,
|
172 |
-
repo_type="dataset",
|
173 |
-
token=Config.HF_TOKEN
|
174 |
-
)
|
175 |
-
|
176 |
-
if response:
|
177 |
-
with db.conn.cursor() as cursor:
|
178 |
-
cursor.execute("""
|
179 |
-
INSERT INTO files (
|
180 |
-
original_name, stored_name, file_path,
|
181 |
-
file_type, file_size
|
182 |
-
) VALUES (%s, %s, %s, %s, %s)
|
183 |
-
""", (
|
184 |
-
original_name,
|
185 |
-
stored_name,
|
186 |
-
full_path,
|
187 |
-
get_file_type(original_name),
|
188 |
-
len(file_content)
|
189 |
-
))
|
190 |
-
db.conn.commit()
|
191 |
-
|
192 |
-
return jsonify({'success': True})
|
193 |
-
|
194 |
-
return jsonify({'error': 'Upload failed'}), 500
|
195 |
-
|
196 |
-
except Exception as e:
|
197 |
-
return jsonify({'error': str(e)}), 500
|
198 |
-
|
199 |
-
@app.route('/api/files/search')
|
200 |
-
@require_auth
|
201 |
-
def search_files():
|
202 |
-
keyword = request.args.get('keyword', '')
|
203 |
-
if not keyword:
|
204 |
-
return jsonify([])
|
205 |
-
|
206 |
-
try:
|
207 |
-
files = db.search_files(keyword)
|
208 |
-
return jsonify([{
|
209 |
-
'name': f['original_name'],
|
210 |
-
'path': f['file_path'],
|
211 |
-
'type': get_file_type(f['file_path']),
|
212 |
-
'size': format_file_size(f['file_size']),
|
213 |
-
'created_at': f['created_at'].strftime('%Y-%m-%d %H:%M:%S')
|
214 |
-
} for f in files])
|
215 |
-
except Exception as e:
|
216 |
-
return jsonify({'error': str(e)}), 500
|
217 |
-
|
218 |
-
@app.route('/api/files/delete/<path:filepath>', methods=['DELETE'])
|
219 |
-
@require_auth
|
220 |
-
def delete_file(filepath):
|
221 |
-
try:
|
222 |
-
# Initialize HuggingFace API
|
223 |
-
api = HfApi(token=Config.HF_TOKEN)
|
224 |
-
|
225 |
-
# Delete file from HuggingFace Hub
|
226 |
-
api.delete_file(
|
227 |
-
path_in_repo=filepath,
|
228 |
-
repo_id=Config.HF_DATASET_ID,
|
229 |
-
repo_type="dataset"
|
230 |
-
)
|
231 |
-
|
232 |
-
# Delete file record from database
|
233 |
-
with db.conn.cursor() as cursor:
|
234 |
-
cursor.execute(
|
235 |
-
"DELETE FROM files WHERE file_path = %s",
|
236 |
-
[filepath]
|
237 |
-
)
|
238 |
-
db.conn.commit()
|
239 |
-
|
240 |
-
return jsonify({'success': True})
|
241 |
-
|
242 |
-
except Exception as e:
|
243 |
-
return jsonify({'error': str(e)}), 500
|
244 |
-
|
245 |
-
if __name__ == '__main__':
|
246 |
-
app.run(host='0.0.0.0', port=
|
|
|
1 |
+
from flask import (
|
2 |
+
Flask,
|
3 |
+
render_template,
|
4 |
+
request,
|
5 |
+
jsonify,
|
6 |
+
redirect,
|
7 |
+
url_for,
|
8 |
+
send_file
|
9 |
+
)
|
10 |
+
from io import BytesIO
|
11 |
+
import urllib.parse
|
12 |
+
from functools import wraps
|
13 |
+
import requests
|
14 |
+
import hashlib
|
15 |
+
import os
|
16 |
+
from config import Config
|
17 |
+
from models import Database
|
18 |
+
from utils import get_file_type, format_file_size
|
19 |
+
from huggingface_hub import HfApi
|
20 |
+
|
21 |
+
# 初始化 HuggingFace API
|
22 |
+
api = HfApi(token=Config.HF_TOKEN)
|
23 |
+
app = Flask(__name__)
|
24 |
+
app.config['SECRET_KEY'] = Config.SECRET_KEY
|
25 |
+
db = Database()
|
26 |
+
|
27 |
+
def require_auth(f):
|
28 |
+
@wraps(f)
|
29 |
+
def decorated(*args, **kwargs):
|
30 |
+
if not Config.REQUIRE_LOGIN:
|
31 |
+
return f(*args, **kwargs)
|
32 |
+
if not request.cookies.get('authenticated'):
|
33 |
+
if request.is_json:
|
34 |
+
return jsonify({'error': 'Unauthorized'}), 401
|
35 |
+
return redirect(url_for('login'))
|
36 |
+
return f(*args, **kwargs)
|
37 |
+
return decorated
|
38 |
+
|
39 |
+
@app.route('/login', methods=['GET', 'POST'])
|
40 |
+
def login():
|
41 |
+
if request.method == 'POST':
|
42 |
+
if request.form.get('password') == Config.ACCESS_PASSWORD:
|
43 |
+
response = jsonify({'success': True})
|
44 |
+
response.set_cookie('authenticated', 'true', secure=True, httponly=True)
|
45 |
+
return response
|
46 |
+
return jsonify({'error': 'Invalid password'}), 401
|
47 |
+
return render_template('login.html')
|
48 |
+
|
49 |
+
@app.route('/logout')
|
50 |
+
def logout():
|
51 |
+
response = redirect(url_for('login'))
|
52 |
+
response.delete_cookie('authenticated')
|
53 |
+
return response
|
54 |
+
|
55 |
+
@app.route('/')
|
56 |
+
@require_auth
|
57 |
+
def index():
|
58 |
+
return render_template('index.html')
|
59 |
+
|
60 |
+
@app.route('/api/files/list/')
|
61 |
+
@app.route('/api/files/list/<path:directory>')
|
62 |
+
@require_auth
|
63 |
+
def list_files(directory=''):
|
64 |
+
try:
|
65 |
+
url = f"https://huggingface.co/api/datasets/{Config.HF_DATASET_ID}/tree/{Config.HF_BRANCH}"
|
66 |
+
if directory:
|
67 |
+
url = f"{url}/{directory}"
|
68 |
+
|
69 |
+
response = requests.get(
|
70 |
+
url,
|
71 |
+
headers={'Authorization': f'Bearer {Config.HF_TOKEN}'}
|
72 |
+
)
|
73 |
+
if not response.ok:
|
74 |
+
return jsonify({'error': 'Failed to fetch files', 'details': response.text}), response.status_code
|
75 |
+
|
76 |
+
files = response.json()
|
77 |
+
for file in files:
|
78 |
+
if file['type'] == 'file':
|
79 |
+
file['file_type'] = get_file_type(file['path'])
|
80 |
+
file['size_formatted'] = format_file_size(file['size'])
|
81 |
+
# 添加预览和下载URL
|
82 |
+
file['preview_url'] = f"/api/files/preview/{file['path']}"
|
83 |
+
file['download_url'] = f"/api/files/download/{file['path']}"
|
84 |
+
|
85 |
+
return jsonify(files)
|
86 |
+
except Exception as e:
|
87 |
+
return jsonify({'error': str(e)}), 500
|
88 |
+
|
89 |
+
@app.route('/api/files/preview/<path:filepath>')
|
90 |
+
@require_auth
|
91 |
+
def preview_file(filepath):
|
92 |
+
try:
|
93 |
+
file_type = get_file_type(filepath)
|
94 |
+
if file_type not in ['image', 'video', 'document']:
|
95 |
+
return jsonify({'error': 'File type not supported for preview'}), 400
|
96 |
+
|
97 |
+
url = f"https://{Config.PROXY_DOMAIN}/datasets/{Config.HF_DATASET_ID}/resolve/{Config.HF_BRANCH}/{filepath}"
|
98 |
+
response = requests.get(
|
99 |
+
url,
|
100 |
+
headers={'Authorization': f'Bearer {Config.HF_TOKEN}'},
|
101 |
+
stream=True
|
102 |
+
)
|
103 |
+
|
104 |
+
if response.ok:
|
105 |
+
# 创建文件的内存缓存
|
106 |
+
file_data = BytesIO(response.content)
|
107 |
+
|
108 |
+
# 根据文件类型返回适当的响应
|
109 |
+
return send_file(
|
110 |
+
file_data,
|
111 |
+
mimetype=response.headers.get('content-type', 'application/octet-stream'),
|
112 |
+
conditional=True # 启用条件请求支持
|
113 |
+
)
|
114 |
+
|
115 |
+
return jsonify({'error': 'Failed to fetch file'}), response.status_code
|
116 |
+
except Exception as e:
|
117 |
+
return jsonify({'error': str(e)}), 500
|
118 |
+
|
119 |
+
|
120 |
+
@app.route('/api/files/download/<path:filepath>')
|
121 |
+
@require_auth
|
122 |
+
def download_file(filepath):
|
123 |
+
try:
|
124 |
+
url = f"https://{Config.PROXY_DOMAIN}/datasets/{Config.HF_DATASET_ID}/resolve/{Config.HF_BRANCH}/{filepath}"
|
125 |
+
response = requests.get(
|
126 |
+
url,
|
127 |
+
headers={'Authorization': f'Bearer {Config.HF_TOKEN}'},
|
128 |
+
stream=True
|
129 |
+
)
|
130 |
+
|
131 |
+
if response.ok:
|
132 |
+
# 创建内存文件对象
|
133 |
+
file_obj = BytesIO(response.content)
|
134 |
+
|
135 |
+
# 获取文件名并进行编码
|
136 |
+
filename = os.path.basename(filepath)
|
137 |
+
encoded_filename = urllib.parse.quote(filename.encode('utf-8'))
|
138 |
+
|
139 |
+
# 使用 send_file 返回文件
|
140 |
+
return send_file(
|
141 |
+
file_obj,
|
142 |
+
download_name=filename,
|
143 |
+
as_attachment=True,
|
144 |
+
mimetype=response.headers.get('content-type', 'application/octet-stream')
|
145 |
+
)
|
146 |
+
|
147 |
+
return jsonify({'error': 'File not found'}), 404
|
148 |
+
except Exception as e:
|
149 |
+
return jsonify({'error': str(e)}), 500
|
150 |
+
|
151 |
+
@app.route('/api/files/upload', methods=['POST'])
|
152 |
+
@require_auth
|
153 |
+
def upload_file():
|
154 |
+
if 'file' not in request.files:
|
155 |
+
return jsonify({'error': 'No file provided'}), 400
|
156 |
+
|
157 |
+
file = request.files['file']
|
158 |
+
current_path = request.form.get('path', '').strip('/')
|
159 |
+
|
160 |
+
try:
|
161 |
+
file_content = file.read()
|
162 |
+
file.seek(0)
|
163 |
+
|
164 |
+
original_name = file.filename
|
165 |
+
stored_name = original_name
|
166 |
+
full_path = os.path.join(current_path, stored_name).replace("\\", "/")
|
167 |
+
|
168 |
+
response = api.upload_file(
|
169 |
+
path_or_fileobj=file_content,
|
170 |
+
path_in_repo=full_path,
|
171 |
+
repo_id=Config.HF_DATASET_ID,
|
172 |
+
repo_type="dataset",
|
173 |
+
token=Config.HF_TOKEN
|
174 |
+
)
|
175 |
+
|
176 |
+
if response:
|
177 |
+
with db.conn.cursor() as cursor:
|
178 |
+
cursor.execute("""
|
179 |
+
INSERT INTO files (
|
180 |
+
original_name, stored_name, file_path,
|
181 |
+
file_type, file_size
|
182 |
+
) VALUES (%s, %s, %s, %s, %s)
|
183 |
+
""", (
|
184 |
+
original_name,
|
185 |
+
stored_name,
|
186 |
+
full_path,
|
187 |
+
get_file_type(original_name),
|
188 |
+
len(file_content)
|
189 |
+
))
|
190 |
+
db.conn.commit()
|
191 |
+
|
192 |
+
return jsonify({'success': True})
|
193 |
+
|
194 |
+
return jsonify({'error': 'Upload failed'}), 500
|
195 |
+
|
196 |
+
except Exception as e:
|
197 |
+
return jsonify({'error': str(e)}), 500
|
198 |
+
|
199 |
+
@app.route('/api/files/search')
|
200 |
+
@require_auth
|
201 |
+
def search_files():
|
202 |
+
keyword = request.args.get('keyword', '')
|
203 |
+
if not keyword:
|
204 |
+
return jsonify([])
|
205 |
+
|
206 |
+
try:
|
207 |
+
files = db.search_files(keyword)
|
208 |
+
return jsonify([{
|
209 |
+
'name': f['original_name'],
|
210 |
+
'path': f['file_path'],
|
211 |
+
'type': get_file_type(f['file_path']),
|
212 |
+
'size': format_file_size(f['file_size']),
|
213 |
+
'created_at': f['created_at'].strftime('%Y-%m-%d %H:%M:%S')
|
214 |
+
} for f in files])
|
215 |
+
except Exception as e:
|
216 |
+
return jsonify({'error': str(e)}), 500
|
217 |
+
|
218 |
+
@app.route('/api/files/delete/<path:filepath>', methods=['DELETE'])
|
219 |
+
@require_auth
|
220 |
+
def delete_file(filepath):
|
221 |
+
try:
|
222 |
+
# Initialize HuggingFace API
|
223 |
+
api = HfApi(token=Config.HF_TOKEN)
|
224 |
+
|
225 |
+
# Delete file from HuggingFace Hub
|
226 |
+
api.delete_file(
|
227 |
+
path_in_repo=filepath,
|
228 |
+
repo_id=Config.HF_DATASET_ID,
|
229 |
+
repo_type="dataset"
|
230 |
+
)
|
231 |
+
|
232 |
+
# Delete file record from database
|
233 |
+
with db.conn.cursor() as cursor:
|
234 |
+
cursor.execute(
|
235 |
+
"DELETE FROM files WHERE file_path = %s",
|
236 |
+
[filepath]
|
237 |
+
)
|
238 |
+
db.conn.commit()
|
239 |
+
|
240 |
+
return jsonify({'success': True})
|
241 |
+
|
242 |
+
except Exception as e:
|
243 |
+
return jsonify({'error': str(e)}), 500
|
244 |
+
|
245 |
+
if __name__ == '__main__':
|
246 |
+
app.run(host='0.0.0.0', port=7860, debug=True)
|