完善: CSRF安全修复 + 后台任务管理 + 压测全模块覆盖 + 文档
This commit is contained in:
+52
-13
@@ -25,7 +25,8 @@ class FileManager:
|
||||
items = []
|
||||
try:
|
||||
entries = os.listdir(target)
|
||||
entries.sort(key=lambda x: (not os.path.isdir(os.path.join(target, x)), x.lower()))
|
||||
# 按修改时间倒序排列(最新的在前)
|
||||
entries.sort(key=lambda x: os.path.getmtime(os.path.join(target, x)), reverse=True)
|
||||
except PermissionError:
|
||||
return [], subpath
|
||||
|
||||
@@ -50,9 +51,42 @@ class FileManager:
|
||||
'is_dir': is_dir,
|
||||
'size': size,
|
||||
'modified': modified,
|
||||
'ext': name.split('.')[-1].lower() if not is_dir and '.' in name else '',
|
||||
})
|
||||
return items, subpath
|
||||
|
||||
def search_files(self, keyword):
|
||||
"""搜索文件名包含关键词的文件"""
|
||||
if not keyword:
|
||||
return []
|
||||
keyword = keyword.lower()
|
||||
results = []
|
||||
for root, dirs, files in os.walk(self.upload_dir):
|
||||
for name in files:
|
||||
if keyword in name.lower():
|
||||
full = os.path.join(root, name)
|
||||
rel = os.path.relpath(full, self.upload_dir)
|
||||
stat = os.stat(full)
|
||||
modified = datetime.datetime.fromtimestamp(stat.st_mtime).strftime('%Y-%m-%d %H:%M')
|
||||
s = stat.st_size
|
||||
if s < 1024:
|
||||
size = '{}B'.format(s)
|
||||
elif s < 1024 * 1024:
|
||||
size = '{:.1f}KB'.format(s / 1024)
|
||||
else:
|
||||
size = '{:.1f}MB'.format(s / (1024 * 1024))
|
||||
results.append({
|
||||
'name': name,
|
||||
'path': rel,
|
||||
'is_dir': False,
|
||||
'size': size,
|
||||
'modified': modified,
|
||||
'ext': name.split('.')[-1].lower() if '.' in name else '',
|
||||
})
|
||||
# 按修改时间倒序
|
||||
results.sort(key=lambda x: x['modified'], reverse=True)
|
||||
return results
|
||||
|
||||
def parent_path(self, subpath):
|
||||
if not subpath:
|
||||
return ''
|
||||
@@ -108,8 +142,8 @@ def setup_file_routes(app, template_engine, upload_dir='uploads', get_admin_mode
|
||||
@app.route('/admin/files/', methods=['GET', 'POST'])
|
||||
async def file_browser(request):
|
||||
user = get_current_user(request)
|
||||
if not user:
|
||||
return HTTPResponse.redirect('/admin/login/')
|
||||
if not user or not user.is_admin():
|
||||
return HTTPResponse.error(403, '权限不足')
|
||||
request.current_user = user
|
||||
|
||||
if request.method == 'POST':
|
||||
@@ -120,8 +154,14 @@ def setup_file_routes(app, template_engine, upload_dir='uploads', get_admin_mode
|
||||
return HTTPResponse.redirect('/admin/files/?path={}'.format(subpath))
|
||||
|
||||
subpath = request.query_params.get('path', '')
|
||||
files, current_path = fm.list_files(subpath)
|
||||
parent = fm.parent_path(subpath)
|
||||
search_q = request.query_params.get('q', '').strip()
|
||||
if search_q:
|
||||
files = fm.search_files(search_q)
|
||||
current_path = '搜索: {}'.format(search_q)
|
||||
parent = ''
|
||||
else:
|
||||
files, current_path = fm.list_files(subpath)
|
||||
parent = fm.parent_path(subpath)
|
||||
|
||||
admin_models = {}
|
||||
if get_admin_models_func:
|
||||
@@ -134,28 +174,27 @@ def setup_file_routes(app, template_engine, upload_dir='uploads', get_admin_mode
|
||||
'files': files,
|
||||
'current_path': current_path,
|
||||
'parent_path': parent,
|
||||
'search_q': search_q,
|
||||
})
|
||||
return HTTPResponse(body=html)
|
||||
|
||||
@app.route('/admin/files/download/', methods=['GET'])
|
||||
async def file_download(request):
|
||||
user = get_current_user(request)
|
||||
if not user:
|
||||
return HTTPResponse.redirect('/admin/login/')
|
||||
if not user or not user.is_admin():
|
||||
return HTTPResponse.error(403, '权限不足')
|
||||
filepath = request.query_params.get('path', '')
|
||||
resp = fm.download(filepath)
|
||||
if resp is None:
|
||||
return HTTPResponse.error(404, '文件未找到')
|
||||
return resp
|
||||
|
||||
@app.route('/admin/files/delete/', methods=['GET'])
|
||||
@app.route('/admin/files/delete/', methods=['POST'])
|
||||
async def file_delete(request):
|
||||
user = get_current_user(request)
|
||||
if not user:
|
||||
return HTTPResponse.redirect('/admin/login/')
|
||||
if not user.is_admin():
|
||||
if not user or not user.is_admin():
|
||||
return HTTPResponse.error(403, '权限不足')
|
||||
filepath = request.query_params.get('path', '')
|
||||
subpath = request.query_params.get('dir', '')
|
||||
filepath = request.form.get('path', '')
|
||||
subpath = request.form.get('dir', '')
|
||||
fm.delete(filepath)
|
||||
return HTTPResponse.redirect('/admin/files/?path={}'.format(subpath))
|
||||
Reference in New Issue
Block a user