Files
xbcr/routes/download.py
T

41 lines
1.6 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
# -*- coding: utf-8 -*-
"""抓包工具安装包下载
安装包体积较大(约 25MB),放在项目根的 downloads/ 目录而不是 static/,
避免被 Flask 静态路由以外的缓存策略干扰,也便于单独替换升级。
公开访问:用户注册前就需要拿到工具,若加登录校验会形成「没工具拿不到凭据、
没凭据注册不了」的死循环。
"""
from pathlib import Path
from flask import Blueprint, jsonify, send_from_directory
bp = Blueprint("download", __name__)
# 磁盘上用 ASCII 文件名:中文与全角括号在部分 Linux 文件系统/nginx 配置下
# 容易出编码问题。对外的下载文件名由 download_name 还原。
DOWNLOADS_DIR = Path(__file__).parent.parent / "downloads"
CAPTURE_TOOL_FILE = "xbniu-capture-tool.exe"
CAPTURE_TOOL_NAME = "小白超人抓包工具(新版).exe"
@bp.route("/download/capture-tool.exe", methods=["GET", "HEAD"])
def download_capture_tool():
if not (DOWNLOADS_DIR / CAPTURE_TOOL_FILE).is_file():
return jsonify({"rtn_code": 404, "message": "安装包暂未上传"}), 404
# conditional=True 支持 Range 请求,大文件可断点续传/多线程下载
resp = send_from_directory(
DOWNLOADS_DIR,
CAPTURE_TOOL_FILE,
as_attachment=True,
download_name=CAPTURE_TOOL_NAME,
conditional=True,
max_age=3600,
)
# Flask 只在收到 Range 时才回 Content-Range,但下载管理器需要先看到
# Accept-Ranges 才敢发 Range,所以这里显式声明支持续传
resp.headers["Accept-Ranges"] = "bytes"
return resp