下载改为带签名 token 的临时链接 + window.location.href 原生下载:新增 GET /:id/files/download-token(HMAC 签名,5 分钟有效,密钥随进程随机),download 路由对带 t 参数请求验签;前端去掉 fetch+blob/dataURL 方案;JS 版本号升至 v=20260831d
This commit is contained in:
@@ -111,6 +111,6 @@
|
|||||||
<div class="toast hidden" id="toast"></div>
|
<div class="toast hidden" id="toast"></div>
|
||||||
|
|
||||||
<!-- 发版时更新 v 参数,避免移动端浏览器缓存旧 JS -->
|
<!-- 发版时更新 v 参数,避免移动端浏览器缓存旧 JS -->
|
||||||
<script src="/js/app.js?v=20260831c"></script>
|
<script src="/js/app.js?v=20260831d"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -359,40 +359,14 @@ function updatePhaseUI(d) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ---------- 文件 ----------
|
// ---------- 文件 ----------
|
||||||
// 下载:fetch + Blob 绕过浏览器原生下载(移动端 Chrome 对 Basic 认证下的
|
// 下载:先换一枚短时效签名链接,再 window.location.href 交给浏览器原生下载。
|
||||||
// Content-Disposition 下载处理有 bug,会卡在“等待中”)。
|
// (fetch+blob 在移动端浏览器会被改名 uuid.bin;原生导航下载按服务端
|
||||||
// 同源 fetch 会自动携带浏览器缓存的 Basic 认证头,无需在 JS 里处理密码。
|
// Content-Disposition 保留原名,同源自动带 Basic 认证头。)
|
||||||
async function downloadFile(name) {
|
async function downloadFile(name) {
|
||||||
if (!state.sessionId) return;
|
if (!state.sessionId) return;
|
||||||
toast('下载中…');
|
|
||||||
try {
|
try {
|
||||||
const url = '/api/sessions/' + state.sessionId + '/files/download?path=' + encodeURIComponent(name);
|
const r = await req('GET', '/api/sessions/' + state.sessionId + '/files/download-token?path=' + encodeURIComponent(name));
|
||||||
const res = await fetch(url);
|
window.location.href = r.url;
|
||||||
if (!res.ok) throw new Error('HTTP ' + res.status);
|
|
||||||
let blob = await res.blob();
|
|
||||||
// 抹掉 blob 的 MIME:响应常为 octet-stream,部分移动端浏览器内核会按类型给下载名追加 .bin,抹掉后按原名保存
|
|
||||||
try { blob = new Blob([blob], { type: '' }); } catch (_) {}
|
|
||||||
// 用 data: URL 触发下载:移动端 Chrome(约 114 起)对 blob: object URL 常忽略 download 属性,
|
|
||||||
// 文件名会兜底成 uuid.bin;data: URL 不走该回归逻辑,能正常保留原文件名。
|
|
||||||
let href;
|
|
||||||
try {
|
|
||||||
href = await new Promise(function (resolve, reject) {
|
|
||||||
const r = new FileReader();
|
|
||||||
r.onload = function () { resolve(String(r.result)); };
|
|
||||||
r.onerror = function () { reject(new Error('blob 转 data URL 失败')); };
|
|
||||||
r.readAsDataURL(blob);
|
|
||||||
});
|
|
||||||
} catch (_) {
|
|
||||||
// 兜底:回到 object URL(电脑端正常)
|
|
||||||
href = URL.createObjectURL(blob);
|
|
||||||
setTimeout(function () { URL.revokeObjectURL(href); }, 3000);
|
|
||||||
}
|
|
||||||
const a = document.createElement('a');
|
|
||||||
a.href = href;
|
|
||||||
a.download = name.split('/').pop() || 'download';
|
|
||||||
document.body.appendChild(a);
|
|
||||||
a.click();
|
|
||||||
setTimeout(function () { a.remove(); }, 1000);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
toast('下载失败:' + e.message);
|
toast('下载失败:' + e.message);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ const express = require('express');
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const multer = require('multer');
|
const multer = require('multer');
|
||||||
|
const crypto = require('crypto');
|
||||||
const engine = require('../engine');
|
const engine = require('../engine');
|
||||||
const { listTaskFiles } = require('./sessions');
|
const { listTaskFiles } = require('./sessions');
|
||||||
const { resolveInTask } = require('../tools/executor');
|
const { resolveInTask } = require('../tools/executor');
|
||||||
@@ -70,6 +71,45 @@ router.get('/:id/files', function (req, res) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ---------- 临时下载链接(带签名 token,供浏览器原生下载使用) ----------
|
||||||
|
// 移动端浏览器 fetch+blob 下载会丢失文件名(uuid.bin);改为短时效签名链接,
|
||||||
|
// 前端拿到后 window.location.href 导航下载,走原生下载流程。
|
||||||
|
// 密钥随进程启动随机生成,重启后旧 token 自动失效。
|
||||||
|
const DOWNLOAD_TOKEN_TTL_MS = 5 * 60 * 1000;
|
||||||
|
const downloadTokenSecret = crypto.randomBytes(32).toString('hex');
|
||||||
|
|
||||||
|
function signDownloadToken(sessionId, rel, expires) {
|
||||||
|
return crypto.createHmac('sha256', downloadTokenSecret)
|
||||||
|
.update(sessionId + '\n' + rel + '\n' + expires).digest('hex');
|
||||||
|
}
|
||||||
|
|
||||||
|
function validDownloadToken(sessionId, rel, expires, t) {
|
||||||
|
const exp = Number(expires);
|
||||||
|
if (!t || !Number.isFinite(exp) || exp < Date.now()) return false;
|
||||||
|
const expected = signDownloadToken(sessionId, rel, exp);
|
||||||
|
try {
|
||||||
|
return crypto.timingSafeEqual(Buffer.from(String(t), 'utf8'), Buffer.from(expected, 'utf8'));
|
||||||
|
} catch (_) { return false; }
|
||||||
|
}
|
||||||
|
|
||||||
|
// 签发临时下载链接(需正常认证;拿到的链接 5 分钟内有效)
|
||||||
|
router.get('/:id/files/download-token', function (req, res) {
|
||||||
|
try {
|
||||||
|
const rel = String(req.query.path || '');
|
||||||
|
if (!rel) return res.status(400).json({ error: '缺少 path 参数' });
|
||||||
|
const abs = resolveInTask(engine.taskDirOf(req.params.id), rel);
|
||||||
|
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);
|
||||||
|
res.json({
|
||||||
|
url: '/api/sessions/' + encodeURIComponent(req.params.id) + '/files/download?path='
|
||||||
|
+ encodeURIComponent(rel) + '&expires=' + expires + '&t=' + t,
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
res.status(404).json({ error: '文件不存在' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// 下载统一响应:禁用一切缓存协商。
|
// 下载统一响应:禁用一切缓存协商。
|
||||||
// 默认的 ETag/Last-Modified + Cache-Control: public, max-age=0 会诱导移动端浏览器
|
// 默认的 ETag/Last-Modified + Cache-Control: public, max-age=0 会诱导移动端浏览器
|
||||||
// 先发条件请求拿 304 空体,下载管理器随后卡在“等待中”(桌面 Safari/curl 不受影响)。
|
// 先发条件请求拿 304 空体,下载管理器随后卡在“等待中”(桌面 Safari/curl 不受影响)。
|
||||||
@@ -85,6 +125,10 @@ router.get('/:id/files/download', function (req, res) {
|
|||||||
try {
|
try {
|
||||||
const rel = String(req.query.path || '');
|
const rel = String(req.query.path || '');
|
||||||
if (!rel) return res.status(400).json({ error: '缺少 path 参数' });
|
if (!rel) return res.status(400).json({ error: '缺少 path 参数' });
|
||||||
|
// 带 token 的请求须先验签(会话/路径/时效任一不符即拒绝)
|
||||||
|
if (req.query.t && !validDownloadToken(req.params.id, rel, req.query.expires, String(req.query.t))) {
|
||||||
|
return res.status(403).json({ error: '下载链接已过期或无效' });
|
||||||
|
}
|
||||||
const abs = resolveInTask(engine.taskDirOf(req.params.id), rel);
|
const abs = resolveInTask(engine.taskDirOf(req.params.id), rel);
|
||||||
const st = fs.statSync(abs);
|
const st = fs.statSync(abs);
|
||||||
if (!st.isFile()) return res.status(400).json({ error: '不是文件' });
|
if (!st.isFile()) return res.status(400).json({ error: '不是文件' });
|
||||||
|
|||||||
Reference in New Issue
Block a user