Files
agent/app/server/download-send.js
T
kicer 3f4ab51ea5 公网临时链接改为不透明短 ID(/p/<22字符>),不再暴露文件名/会话名/签名
- public-files.js(新): 内存表 id → (会话, 相对路径, 过期时间);mint 顺手清过期项并兜底上限
- routes/public-files.js(新): GET /p/<id> 匿名取用(查表 → resolveInTask 复核 → sendDownload),未命中/过期一律 404
- routes/public-link.js: 不再签 HMAC,改为 mint 短 ID,返回 http://<IP>:8090/p/<id>(约 50 字符、纯 ASCII)
  动因:原 URL 把文件名写了两遍再挂 64 位签名,共 300+ 字符;它会被模型抄进工具参数、并长期留在会话历史每轮重发
- download-send.js(新): 从 files.js 抽出 sendDownload/downloadDisposition,供浏览器下载与公网取用共用
- server.js: /p 挂在 auth.middleware 之前(匿名可达);浏览器下载那套签名链接保持不动
- docs: DEPLOY.md 4.1 / ARCHITECTURE.md(文件树、路由表、约束)/ README.md 同步
2026-09-15 21:27:43 +08:00

35 lines
1.9 KiB
JavaScript
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.
// 单文件下载响应的公共实现:任务文件的浏览器下载(routes/files.js)与公网临时文件
// (routes/public-files.js)共用,避免两处各写一套文件名/缓存策略。
'use strict';
const path = require('path');
// Content-Disposition 手工构造,绕开 content-disposition 包的 ASCII 兜底:
// 该包会把中文名逐字替换成 ?(???.pdf),且 filename= 后紧跟 filename*= 会让
// 老 AOSP DownloadManager 的正则解析失败而改从 URL 猜名(download.pdf)。
// 这里传统 filename= 直接放百分号编码的 UTF-8:朴素解析器解码后能得到真名,
// 标准客户端照旧优先 filename*(RFC 5987)。
function downloadDisposition(name) {
const legacy = encodeURIComponent(name);
// RFC 5987 attr-char 不含 ' ( ) ! *,ext-value 里需额外编码
const ext = legacy.replace(/['()!*]/g, function (c) {
return '%' + c.charCodeAt(0).toString(16).toUpperCase();
});
return 'attachment; filename="' + legacy + '"; filename*=UTF-8\'\'' + ext;
}
// 下载统一响应:禁用一切缓存协商。
// 默认的 ETag/Last-Modified + Cache-Control: public, max-age=0 会诱导移动端浏览器
// 先发条件请求拿 304 空体,下载管理器随后卡在“等待中”(桌面 Safari/curl 不受影响)。
// 不能用 res.download:它会用 content-disposition 包覆盖上面的文件名策略;
// res.sendFile 不设置 Content-Disposition,Content-Type 按扩展名自动出,Range 由 send 支持。
function sendDownload(res, abs) {
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate');
res.setHeader('Pragma', 'no-cache');
res.setHeader('Expires', '0');
res.setHeader('Content-Disposition', downloadDisposition(path.basename(abs)));
res.sendFile(abs, { lastModified: false, etag: false, maxAge: 0 });
}
module.exports = { downloadDisposition, sendDownload };