InfoRadar / backend /api /static.py
dqy08's picture
支持更大语义模型(4B/8B)
744cf20
"""静态文件路由"""
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/<path:path>')
def send_static(path):
"""serves all files from ./client/ to ``/client/<path:path>``
:param path: path from api call
"""
# 记录页面加载(排除静态资源)
if path.endswith('.html'):
log_page_load(path)
return send_from_directory('client/dist/', path)
@app.route('/demo/<path:path>')
def send_demo(path):
"""serves all demo files from the demo dir to ``/demo/<path:path>``
: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)