Download backend/api/static.py from dqy08/InfoLens: direct link, hf CLI and curl.
- Browser
- Download file 4.26 kB
-
https://huggingface.co/spaces/dqy08/InfoLens/resolve/main/backend/api/static.py
- Command line
-
hf download hf://spaces/dqy08/InfoLens/backend/api/static.py
-
curl -L -o static.py https://huggingface.co/spaces/dqy08/InfoLens/resolve/main/backend/api/static.py
4.26 kB
| """静态文件路由""" | |
| import mimetypes | |
| import re | |
| from pathlib import Path | |
| from urllib.parse import unquote | |
| from flask import Response, redirect, abort, request | |
| from werkzeug.utils import safe_join | |
| from backend.platform.access_log import log_cached_demo, log_json_demo, log_page_load | |
| _RANGE_RE = re.compile(r"^bytes=(\d*)-(\d*)$", re.ASCII) | |
| def _parse_byte_range(range_header: str, file_size: int) -> tuple[int, int] | None: | |
| m = _RANGE_RE.match(range_header.strip()) | |
| if not m: | |
| return None | |
| start_s, end_s = m.group(1), m.group(2) | |
| if not start_s and not end_s: | |
| return None | |
| if not start_s: | |
| suffix = int(end_s) | |
| if suffix <= 0: | |
| return None | |
| start = max(0, file_size - suffix) | |
| end = file_size - 1 | |
| else: | |
| start = int(start_s) | |
| end = int(end_s) if end_s else file_size - 1 | |
| if start >= file_size: | |
| return None | |
| end = min(end, file_size - 1) | |
| if start > end: | |
| return None | |
| return start, end | |
| def _read_static_file(directory: str, path: str) -> Response: | |
| """读取静态文件并返回 Response,避免 send_from_directory 在 ASGI/a2wsgi 下 | |
| 流式传输导致的 Content-Length 不匹配(RuntimeError: Response content shorter than Content-Length)。 | |
| 支持 Range,供 <video> 等 seek 使用。 | |
| """ | |
| base = Path(directory).resolve() | |
| safe_path = safe_join(str(base), path) | |
| if safe_path is None: | |
| abort(404) | |
| full_path = Path(safe_path) | |
| if not full_path.is_file() or not str(full_path.resolve()).startswith(str(base)): | |
| abort(404) | |
| file_size = full_path.stat().st_size | |
| mimetype, _ = mimetypes.guess_type(path) | |
| mimetype = mimetype or "application/octet-stream" | |
| common_headers = {"Accept-Ranges": "bytes"} | |
| range_header = request.headers.get("Range") | |
| if range_header: | |
| parsed = _parse_byte_range(range_header, file_size) | |
| if parsed is None: | |
| return Response( | |
| status=416, | |
| headers={ | |
| **common_headers, | |
| "Content-Range": f"bytes */{file_size}", | |
| }, | |
| ) | |
| start, end = parsed | |
| length = end - start + 1 | |
| with full_path.open("rb") as f: | |
| f.seek(start) | |
| content = f.read(length) | |
| return Response( | |
| content, | |
| status=206, | |
| mimetype=mimetype, | |
| headers={ | |
| **common_headers, | |
| "Content-Length": str(len(content)), | |
| "Content-Range": f"bytes {start}-{end}/{file_size}", | |
| }, | |
| ) | |
| content = full_path.read_bytes() | |
| return Response( | |
| content, | |
| mimetype=mimetype, | |
| headers={**common_headers, "Content-Length": str(len(content))}, | |
| ) | |
| def register_static_routes(app): | |
| """注册静态文件路由""" | |
| def redir(): | |
| target = 'client/index.html' | |
| if request.query_string: | |
| target += '?' + request.query_string.decode() | |
| return redirect(target) | |
| def send_static(path): | |
| """serves all files from ./client/dist/ to ``/client/<path:path>``""" | |
| if path == 'gen_attribute.html': | |
| target = 'causal_flow.html' | |
| if request.query_string: | |
| target += '?' + request.query_string.decode() | |
| return redirect(f'/client/{target}', code=301) | |
| if path.endswith('.html'): | |
| log_page_load(path) | |
| if path.endswith('.json'): | |
| log_cached_demo(path) | |
| return _read_static_file('client/dist', path) | |
| def send_demo(path): | |
| """serves demo files:普通用户仅 public;有效 admin 可用 --dir(含不公开)""" | |
| from backend.platform.app_context import get_demo_directory | |
| data_dir = get_demo_directory(create=False) | |
| log_json_demo(path) | |
| try: | |
| decoded_path = unquote(path) | |
| return _read_static_file(str(data_dir), decoded_path) | |
| except Exception: | |
| try: | |
| return _read_static_file(str(data_dir), path) | |
| except Exception: | |
| abort(404) | |