PPT/修复文件/fix_routes.py
2025-05-13 10:33:43 +08:00

117 lines
4.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# 检查服务器路由并修复问题
import os
import json
from flask import Flask, send_from_directory, jsonify, send_file
# 设置应用
app = Flask(__name__)
app.config['OUTPUT_FOLDER'] = '/app/output'
app.config['UPLOAD_FOLDER'] = '/app/uploads'
@app.route('/view_report/<task_id>')
def view_report(task_id):
"""提供报告预览页面"""
try:
task_output_dir = os.path.join(app.config['OUTPUT_FOLDER'], task_id)
html_path = os.path.join(task_output_dir, "summary.html")
if os.path.exists(html_path):
print(f"提供报告预览: {html_path}")
return send_file(html_path)
else:
# 查找是否有备用报告文件
backup_files = [f for f in os.listdir(task_output_dir) if f.endswith('.html')]
if backup_files:
alt_html_path = os.path.join(task_output_dir, backup_files[0])
print(f"提供备用报告预览: {alt_html_path}")
return send_file(alt_html_path)
else:
return "找不到报告文件", 404
except Exception as e:
print(f"提供报告预览时出错: {str(e)}")
return f"服务器错误: {str(e)}", 500
@app.route('/results/<job_id>/<filename>')
def get_result_file(job_id, filename):
"""提供访问结果文件的路由"""
try:
file_path = os.path.join(app.config['OUTPUT_FOLDER'], job_id, filename)
if os.path.exists(file_path):
return send_file(file_path)
else:
return f"文件不存在: {file_path}", 404
except Exception as e:
return f"访问文件出错: {str(e)}", 500
@app.route('/api/tasks')
def api_get_tasks():
"""获取所有任务列表"""
tasks = []
try:
# 遍历输出目录中的所有作业
for job_id in os.listdir(app.config['OUTPUT_FOLDER']):
job_dir = os.path.join(app.config['OUTPUT_FOLDER'], job_id)
if os.path.isdir(job_dir):
# 尝试读取结果文件
results_file = os.path.join(job_dir, "results.json")
# 默认任务信息
task = {
"id": job_id,
"status": "completed",
"progress": 100,
"result_path": f"/view_report/{job_id}",
"filename": f"视频_{job_id[:6]}",
"uploaded_at": "2024-05-01 12:00:00",
"size": 0,
"message": "处理完成"
}
# 尝试从原始视频文件获取信息
try:
# 检查是否有同名的视频文件
for filename in os.listdir(app.config['UPLOAD_FOLDER']):
if job_id in filename:
task["filename"] = filename
break
except Exception:
pass
# 尝试从结果文件获取更多信息
if os.path.exists(results_file):
try:
with open(results_file, 'r') as f:
results = json.load(f)
# 填充任务信息
if "video_info" in results and "filename" in results["video_info"]:
task["filename"] = results["video_info"]["filename"]
if "analysis" in results and "timestamp" in results["analysis"]:
task["uploaded_at"] = results["analysis"]["timestamp"]
except Exception as e:
print(f"解析结果文件出错: {str(e)}")
# 如果没有摘要文件检查目录中是否有HTML文件
summary_path = os.path.join(job_dir, "summary.html")
if not os.path.exists(summary_path):
html_files = [f for f in os.listdir(job_dir) if f.endswith('.html')]
if html_files:
# 使用第一个HTML文件作为摘要
task["result_path"] = f"/results/{job_id}/{html_files[0]}"
tasks.append(task)
except Exception as e:
print(f"获取任务列表出错: {str(e)}")
return jsonify({"tasks": tasks})
# 打印路由表
def print_routes():
print("\n当前应用路由表:")
for rule in app.url_map.iter_rules():
print(f"{rule.endpoint}: {rule.rule}")
if __name__ == "__main__":
print_routes()
app.run(debug=True, host='0.0.0.0', port=5000)