File size: 1,592 Bytes
e5d8d3a
 
744cf20
f18b50b
e5d8d3a
 
 
 
 
 
 
db3736c
 
 
 
e5d8d3a
 
 
 
 
 
 
f18b50b
 
 
e5d8d3a
 
033070f
 
 
e5d8d3a
 
 
 
 
 
f18b50b
 
 
e5d8d3a
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
"""静态文件路由"""
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)