Files
ylt_diy/lib/api/download_api.py
user9994793890 f84aa8a4a9 refactor: 重构项目结构,按功能模块拆分文件
- 后端:将 app.py 拆分为 lib/api/ 目录下的多个蓝图模块
  - config_api.py: 配置管理 API
  - entity_api.py: 实体管理 API
  - field_api.py: 字段列表 API
  - report_api.py: 日报生成和历史记录 API
  - init_api.py: 数据库初始化 API
  - download_api.py: 文件下载 API
- 前端:将 index.html 拆分为多个文件
  - static/css/style.css: CSS 样式
  - static/js/common.js: 通用工具函数
  - static/js/config.js: 配置管理
  - static/js/entity.js: 实体管理
  - static/js/report.js: 日报生成
  - static/js/history.js: 历史记录
- 更新 app.py 使用蓝图注册 API
- 创建 AGENTS.md 项目文档

Coze-Commit-Type: user
Coze-User-ID: 3722323274763196
Coze-Conversation-ID: 9894087
2026-07-13 15:43:03 +08:00

41 lines
1.5 KiB
Python

"""
文件下载 API
"""
import os
from flask import request, jsonify, send_from_directory
from . import download_bp
@download_bp.route('/download', methods=['GET'])
def download_file():
"""下载文件"""
try:
file_path = request.args.get('path')
print(f"[下载] 请求路径:{file_path}")
if not file_path:
return jsonify({'success': False, 'message': '文件路径不能为空'}), 400
# 安全检查:确保路径在 public 目录下
if not file_path.startswith('/reports/'):
return jsonify({'success': False, 'message': '无效的文件路径'}), 400
filename = file_path.replace('/reports/', '')
# 使用绝对路径
reports_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', '..', 'public', 'reports')
print(f"[下载] 文件目录:{reports_dir}")
print(f"[下载] 文件名:{filename}")
print(f"[下载] 文件是否存在:{os.path.exists(os.path.join(reports_dir, filename))}")
# 列出目录中的所有文件
if os.path.exists(reports_dir):
print(f"[下载] 目录中的文件:{os.listdir(reports_dir)}")
return send_from_directory(reports_dir, filename, as_attachment=True)
except Exception as e:
print(f"[下载] 错误:{str(e)}")
import traceback
traceback.print_exc()
return jsonify({'success': False, 'message': str(e)}), 500