格式化代码
This commit is contained in:
@@ -4,11 +4,13 @@ from loguru import logger
|
||||
|
||||
file_browser_bp = Blueprint('file_browser', __name__)
|
||||
|
||||
|
||||
@file_browser_bp.route('/file_browser')
|
||||
def file_browser():
|
||||
"""文件浏览器页面"""
|
||||
return render_template('file_browser.html')
|
||||
|
||||
|
||||
@file_browser_bp.route('/api/list_files')
|
||||
def list_files():
|
||||
"""获取指定目录下的文件列表"""
|
||||
@@ -18,14 +20,14 @@ def list_files():
|
||||
project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
# 构建完整路径
|
||||
full_path = os.path.join(project_root, path)
|
||||
|
||||
|
||||
# 安全检查:确保路径在项目根目录内
|
||||
if not os.path.abspath(full_path).startswith(project_root):
|
||||
return jsonify({"success": False, "message": "访问被拒绝:路径超出项目范围"})
|
||||
|
||||
|
||||
if not os.path.exists(full_path):
|
||||
return jsonify({"success": False, "message": "目录不存在"})
|
||||
|
||||
|
||||
# 需要隐藏的目录列表
|
||||
hidden_dirs = {
|
||||
'__pycache__',
|
||||
@@ -44,30 +46,30 @@ def list_files():
|
||||
'.eggs',
|
||||
'*.egg-info'
|
||||
}
|
||||
|
||||
|
||||
items = []
|
||||
for item in os.listdir(full_path):
|
||||
# 跳过隐藏文件和目录
|
||||
if item.startswith('.'):
|
||||
continue
|
||||
|
||||
|
||||
item_path = os.path.join(full_path, item)
|
||||
is_dir = os.path.isdir(item_path)
|
||||
|
||||
|
||||
# 跳过隐藏目录
|
||||
if is_dir and item in hidden_dirs:
|
||||
continue
|
||||
|
||||
|
||||
items.append({
|
||||
"name": item,
|
||||
"is_dir": is_dir,
|
||||
"size": os.path.getsize(item_path) if not is_dir else 0,
|
||||
"modified": os.path.getmtime(item_path)
|
||||
})
|
||||
|
||||
|
||||
# 对文件列表进行排序:目录在前,同类型按名称排序
|
||||
items.sort(key=lambda x: (not x["is_dir"], x["name"].lower()))
|
||||
|
||||
|
||||
return jsonify({
|
||||
"success": True,
|
||||
"data": {
|
||||
@@ -79,6 +81,7 @@ def list_files():
|
||||
logger.error(f"列出文件失败: {e}")
|
||||
return jsonify({"success": False, "message": str(e)})
|
||||
|
||||
|
||||
@file_browser_bp.route('/api/download_file')
|
||||
def download_file():
|
||||
"""下载指定文件"""
|
||||
@@ -88,17 +91,17 @@ def download_file():
|
||||
project_root = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||
# 构建完整路径
|
||||
full_path = os.path.join(project_root, path)
|
||||
|
||||
|
||||
# 安全检查:确保路径在项目根目录内
|
||||
if not os.path.abspath(full_path).startswith(project_root):
|
||||
return jsonify({"success": False, "message": "访问被拒绝:路径超出项目范围"})
|
||||
|
||||
|
||||
if not os.path.exists(full_path):
|
||||
return jsonify({"success": False, "message": "文件不存在"})
|
||||
|
||||
|
||||
if os.path.isdir(full_path):
|
||||
return jsonify({"success": False, "message": "不能下载目录"})
|
||||
|
||||
|
||||
return send_file(
|
||||
full_path,
|
||||
as_attachment=True,
|
||||
@@ -106,4 +109,4 @@ def download_file():
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"下载文件失败: {e}")
|
||||
return jsonify({"success": False, "message": str(e)})
|
||||
return jsonify({"success": False, "message": str(e)})
|
||||
|
||||
Reference in New Issue
Block a user