"""静态文件路由""" from urllib.parse import unquote from flask import send_from_directory, redirect, abort, request from backend.access_log import log_page_load, log_demo_file def register_static_routes(app): """注册静态文件路由""" @app.route('/') def redir(): target = 'client/index.html' if request.query_string: target += '?' + request.query_string.decode() return redirect(target) @app.route('/client/') def send_static(path): """serves all files from ./client/ to ``/client/`` :param path: path from api call """ # 记录页面加载(排除静态资源) if path.endswith('.html'): log_page_load(path) return send_from_directory('client/dist/', path) @app.route('/demo/') def send_demo(path): """serves all demo files from the demo dir to ``/demo/`` :param path: path from api call """ from backend.app_context import get_data_dir data_dir = get_data_dir() # 记录demo文件加载 log_demo_file(path) # URL 解码路径(处理中文等特殊字符) try: decoded_path = unquote(path) return send_from_directory(str(data_dir), decoded_path) except Exception as e: # 如果解码失败,尝试直接使用原路径 try: return send_from_directory(str(data_dir), path) except Exception: abort(404)