lexlepty commited on
Commit
841d4e1
·
verified ·
1 Parent(s): 8b68885

Upload 7 files

Browse files
Files changed (2) hide show
  1. LICENSE +21 -0
  2. app.py +295 -246
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) 2025 zhanghxiao
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
app.py CHANGED
@@ -1,246 +1,295 @@
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)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import (
2
+ Flask,
3
+ json,
4
+ render_template,
5
+ request,
6
+ jsonify,
7
+ redirect,
8
+ url_for,
9
+ send_file,
10
+ Response
11
+ )
12
+ from io import BytesIO
13
+ import urllib.parse
14
+ from functools import wraps
15
+ import requests
16
+ import hashlib
17
+ import os
18
+ from config import Config
19
+ from models import Database
20
+ from utils import get_file_type, format_file_size
21
+ from huggingface_hub import HfApi
22
+
23
+ # 初始化 HuggingFace API
24
+ api = HfApi(token=Config.HF_TOKEN)
25
+ app = Flask(__name__)
26
+ app.config['SECRET_KEY'] = Config.SECRET_KEY
27
+ db = Database()
28
+
29
+ def require_auth(f):
30
+ @wraps(f)
31
+ def decorated(*args, **kwargs):
32
+ if not Config.REQUIRE_LOGIN:
33
+ return f(*args, **kwargs)
34
+ if not request.cookies.get('authenticated'):
35
+ if request.is_json:
36
+ return jsonify({'error': 'Unauthorized'}), 401
37
+ return redirect(url_for('login'))
38
+ return f(*args, **kwargs)
39
+ return decorated
40
+
41
+ @app.route('/login', methods=['GET', 'POST'])
42
+ def login():
43
+ if request.method == 'POST':
44
+ if request.form.get('password') == Config.ACCESS_PASSWORD:
45
+ response = jsonify({'success': True})
46
+ response.set_cookie('authenticated', 'true', secure=True, httponly=True)
47
+ return response
48
+ return jsonify({'error': 'Invalid password'}), 401
49
+ return render_template('login.html')
50
+
51
+ @app.route('/logout')
52
+ def logout():
53
+ response = redirect(url_for('login'))
54
+ response.delete_cookie('authenticated')
55
+ return response
56
+
57
+ @app.route('/')
58
+ @require_auth
59
+ def index():
60
+ return render_template('index.html')
61
+
62
+ @app.route('/api/files/list/')
63
+ @app.route('/api/files/list/<path:directory>')
64
+ @require_auth
65
+ def list_files(directory=''):
66
+ try:
67
+ url = f"https://huggingface.co/api/datasets/{Config.HF_DATASET_ID}/tree/{Config.HF_BRANCH}"
68
+ if directory:
69
+ url = f"{url}/{directory}"
70
+
71
+ response = requests.get(
72
+ url,
73
+ headers={'Authorization': f'Bearer {Config.HF_TOKEN}'}
74
+ )
75
+ if not response.ok:
76
+ return jsonify({'error': 'Failed to fetch files', 'details': response.text}), response.status_code
77
+
78
+ files = response.json()
79
+ for file in files:
80
+ if file['type'] == 'file':
81
+ file['file_type'] = get_file_type(file['path'])
82
+ file['size_formatted'] = format_file_size(file['size'])
83
+ # 添加预览和下载URL
84
+ file['preview_url'] = f"/api/files/preview/{file['path']}"
85
+ file['download_url'] = f"/api/files/download/{file['path']}"
86
+
87
+ return jsonify(files)
88
+ except Exception as e:
89
+ return jsonify({'error': str(e)}), 500
90
+
91
+ @app.route('/api/files/preview/<path:filepath>')
92
+ @require_auth
93
+ def preview_file(filepath):
94
+ try:
95
+ file_type = get_file_type(filepath)
96
+ if file_type not in ['image', 'video', 'document']:
97
+ return jsonify({'error': 'File type not supported for preview'}), 400
98
+
99
+ url = f"https://{Config.PROXY_DOMAIN}/datasets/{Config.HF_DATASET_ID}/resolve/{Config.HF_BRANCH}/{filepath}"
100
+ response = requests.get(
101
+ url,
102
+ headers={'Authorization': f'Bearer {Config.HF_TOKEN}'},
103
+ stream=True
104
+ )
105
+
106
+ if response.ok:
107
+ def generate():
108
+ for chunk in response.iter_content(chunk_size=8192):
109
+ yield chunk
110
+
111
+ return Response(
112
+ generate(),
113
+ mimetype=response.headers.get('content-type', 'application/octet-stream'),
114
+ direct_passthrough=True
115
+ )
116
+
117
+ return jsonify({'error': 'Failed to fetch file'}), response.status_code
118
+ except Exception as e:
119
+ return jsonify({'error': str(e)}), 500
120
+
121
+ @app.route('/api/files/download/<path:filepath>')
122
+ @require_auth
123
+ def download_file(filepath):
124
+ try:
125
+ url = f"https://{Config.PROXY_DOMAIN}/datasets/{Config.HF_DATASET_ID}/resolve/{Config.HF_BRANCH}/{filepath}"
126
+
127
+ # 获取文件基本信息
128
+ response = requests.get(
129
+ url,
130
+ headers={'Authorization': f'Bearer {Config.HF_TOKEN}'},
131
+ stream=True
132
+ )
133
+
134
+ if response.ok:
135
+ filename = os.path.basename(filepath)
136
+ encoded_filename = urllib.parse.quote(filename.encode('utf-8'))
137
+
138
+ # 直接流式传输文件内容
139
+ return Response(
140
+ response.iter_content(chunk_size=1048576),
141
+ headers={
142
+ 'Content-Disposition': f'attachment; filename*=UTF-8\'\'{encoded_filename}',
143
+ 'Content-Type': response.headers.get('content-type', 'application/octet-stream'),
144
+ 'Content-Length': response.headers.get('content-length'),
145
+ 'Accept-Ranges': 'bytes'
146
+ }
147
+ )
148
+
149
+ return jsonify({'error': 'File not found'}), 404
150
+
151
+ except Exception as e:
152
+ return jsonify({'error': str(e)}), 500
153
+ @app.route('/api/files/upload', methods=['POST'])
154
+ @require_auth
155
+ def upload_file():
156
+ if 'file' not in request.files:
157
+ return jsonify({'error': 'No file provided'}), 400
158
+
159
+ file = request.files['file']
160
+ current_path = request.form.get('path', '').strip('/')
161
+
162
+ try:
163
+ file_content = file.read()
164
+ file.seek(0)
165
+
166
+ original_name = file.filename
167
+ stored_name = original_name
168
+ full_path = os.path.join(current_path, stored_name).replace("\\", "/")
169
+
170
+ response = api.upload_file(
171
+ path_or_fileobj=file_content,
172
+ path_in_repo=full_path,
173
+ repo_id=Config.HF_DATASET_ID,
174
+ repo_type="dataset",
175
+ token=Config.HF_TOKEN
176
+ )
177
+
178
+ if response:
179
+ with db.conn.cursor() as cursor:
180
+ cursor.execute("""
181
+ INSERT INTO files (
182
+ original_name, stored_name, file_path,
183
+ file_type, file_size
184
+ ) VALUES (%s, %s, %s, %s, %s)
185
+ """, (
186
+ original_name,
187
+ stored_name,
188
+ full_path,
189
+ get_file_type(original_name),
190
+ len(file_content)
191
+ ))
192
+ db.conn.commit()
193
+
194
+ return jsonify({'success': True})
195
+
196
+ return jsonify({'error': 'Upload failed'}), 500
197
+
198
+ except Exception as e:
199
+ return jsonify({'error': str(e)}), 500
200
+
201
+ @app.route('/api/files/search')
202
+ @require_auth
203
+ def search_files():
204
+ keyword = request.args.get('keyword', '')
205
+ if not keyword:
206
+ return jsonify([])
207
+
208
+ try:
209
+ files = db.search_files(keyword)
210
+ return jsonify([{
211
+ 'name': f['original_name'],
212
+ 'path': f['file_path'],
213
+ 'type': get_file_type(f['file_path']),
214
+ 'size': format_file_size(f['file_size']),
215
+ 'created_at': f['created_at'].strftime('%Y-%m-%d %H:%M:%S')
216
+ } for f in files])
217
+ except Exception as e:
218
+ return jsonify({'error': str(e)}), 500
219
+
220
+ @app.route('/api/files/delete/<path:filepath>', methods=['DELETE'])
221
+ @require_auth
222
+ def delete_file(filepath):
223
+ try:
224
+ # Initialize HuggingFace API
225
+ api = HfApi(token=Config.HF_TOKEN)
226
+
227
+ # Delete file from HuggingFace Hub
228
+ api.delete_file(
229
+ path_in_repo=filepath,
230
+ repo_id=Config.HF_DATASET_ID,
231
+ repo_type="dataset"
232
+ )
233
+
234
+ # Delete file record from database
235
+ with db.conn.cursor() as cursor:
236
+ cursor.execute(
237
+ "DELETE FROM files WHERE file_path = %s",
238
+ [filepath]
239
+ )
240
+ db.conn.commit()
241
+
242
+ return jsonify({'success': True})
243
+
244
+ except Exception as e:
245
+ return jsonify({'error': str(e)}), 500
246
+
247
+ @app.route('/api/files/create_folder', methods=['POST'])
248
+ @require_auth
249
+ def create_folder():
250
+ try:
251
+ data = request.json
252
+ path = data.get('path', '/')
253
+ name = data.get('name')
254
+
255
+ if not name:
256
+ return jsonify({'error': 'Folder name is required'}), 400
257
+
258
+ full_path = os.path.join(path, name, '.keep').replace("\\", "/")
259
+
260
+ # 使用 HuggingFace API 创建文件夹
261
+ api = HfApi(token=Config.HF_TOKEN)
262
+ response = api.upload_file(
263
+ path_or_fileobj=b'', # 空内容
264
+ path_in_repo=full_path,
265
+ repo_id=Config.HF_DATASET_ID,
266
+ repo_type="dataset",
267
+ token=Config.HF_TOKEN
268
+ )
269
+
270
+ if response:
271
+ # 记录到数据库
272
+ with db.conn.cursor() as cursor:
273
+ cursor.execute("""
274
+ INSERT INTO files (
275
+ original_name, stored_name, file_path,
276
+ file_type, file_size
277
+ ) VALUES (%s, %s, %s, %s, %s)
278
+ """, (
279
+ '.keep',
280
+ '.keep',
281
+ full_path,
282
+ 'directory',
283
+ 0
284
+ ))
285
+ db.conn.commit()
286
+
287
+ return jsonify({'message': 'Folder created successfully'})
288
+
289
+ return jsonify({'error': 'Failed to create folder'}), 500
290
+
291
+ except Exception as e:
292
+ return jsonify({'error': str(e)}), 500
293
+
294
+ if __name__ == '__main__':
295
+ app.run(host='0.0.0.0', port=5000, debug=True)