fix: 修复中文文件名下载(手工Content-Disposition + URL带真实文件名兜底)
This commit is contained in:
@@ -115,8 +115,11 @@ router.get('/:id/files/download-token', function (req, res) {
|
||||
if (!fs.statSync(abs).isFile()) return res.status(400).json({ error: '不是文件' });
|
||||
const expires = Date.now() + DOWNLOAD_TOKEN_TTL_MS;
|
||||
const t = signDownloadToken(req.params.id, rel, expires);
|
||||
// 真实文件名放进 URL 路径段:AOSP DownloadManager 解析不了 Content-Disposition 时
|
||||
// 会退回“从 URL 猜名字”,这样兜底也能拿到正确名字。token 仍按 query 里的 path 计算。
|
||||
res.json({
|
||||
url: '/api/sessions/' + encodeURIComponent(req.params.id) + '/files/download?path='
|
||||
url: '/api/sessions/' + encodeURIComponent(req.params.id) + '/files/download-name/'
|
||||
+ encodeURIComponent(path.basename(rel)) + '?path='
|
||||
+ encodeURIComponent(rel) + '&expires=' + expires + '&t=' + t,
|
||||
});
|
||||
} catch (err) {
|
||||
@@ -124,18 +127,35 @@ router.get('/:id/files/download-token', function (req, res) {
|
||||
}
|
||||
});
|
||||
|
||||
// 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.download(abs, path.basename(abs), { acceptRanges: true, lastModified: false, etag: false, maxAge: 0 });
|
||||
res.setHeader('Content-Disposition', downloadDisposition(path.basename(abs)));
|
||||
res.sendFile(abs, { lastModified: false, etag: false, maxAge: 0 });
|
||||
}
|
||||
|
||||
// 下载(支持子目录路径,前端用 ?path=xxx 传入相对路径)
|
||||
router.get('/:id/files/download', function (req, res) {
|
||||
// 下载核心(支持子目录路径,前端用 ?path=xxx 传入相对路径)
|
||||
function serveDownload(req, res) {
|
||||
try {
|
||||
const rel = String(req.query.path || '');
|
||||
if (!rel) return res.status(400).json({ error: '缺少 path 参数' });
|
||||
@@ -150,7 +170,14 @@ router.get('/:id/files/download', function (req, res) {
|
||||
} catch (err) {
|
||||
res.status(404).json({ error: '文件不存在' });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 下载(经典形式;URL 末段是固定的 “download”,猜名兜底会拿到 download.pdf)
|
||||
router.get('/:id/files/download', serveDownload);
|
||||
|
||||
// 加固形式:URL 路径末段带上真实文件名(download-name/<名字>?path=...),
|
||||
// 供解析不了 Content-Disposition 的下载器兜底;path 参数与验签逻辑完全一致。
|
||||
router.get('/:id/files/download-name/*', serveDownload);
|
||||
|
||||
router.delete('/:id/files', function (req, res) {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user