修复移动端浏览器下载卡住:下载响应禁用缓存协商(no-store + 去 ETag/Last-Modified,消除 304 空体卡死);前端下载改 fetch+Blob 绕过原生下载;修复内置 basic auth 的 Buffer.from 拼写
This commit is contained in:
@@ -359,6 +359,30 @@ function updatePhaseUI(d) {
|
||||
}
|
||||
|
||||
// ---------- 文件 ----------
|
||||
// 下载:fetch + Blob 绕过浏览器原生下载(移动端 Chrome 对 Basic 认证下的
|
||||
// Content-Disposition 下载处理有 bug,会卡在“等待中”)。
|
||||
// 同源 fetch 会自动携带浏览器缓存的 Basic 认证头,无需在 JS 里处理密码。
|
||||
async function downloadFile(name) {
|
||||
if (!state.sessionId) return;
|
||||
toast('下载中…');
|
||||
try {
|
||||
const url = '/api/sessions/' + state.sessionId + '/files/download?path=' + encodeURIComponent(name);
|
||||
const res = await fetch(url);
|
||||
if (!res.ok) throw new Error('HTTP ' + res.status);
|
||||
const blob = await res.blob();
|
||||
const dlUrl = URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
a.href = dlUrl;
|
||||
a.download = name.split('/').pop() || 'download';
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
setTimeout(function () { URL.revokeObjectURL(dlUrl); }, 1000);
|
||||
} catch (e) {
|
||||
toast('下载失败:' + e.message);
|
||||
}
|
||||
}
|
||||
|
||||
async function loadFiles() {
|
||||
if (!state.sessionId) return;
|
||||
try {
|
||||
@@ -369,10 +393,9 @@ async function loadFiles() {
|
||||
return;
|
||||
}
|
||||
el.innerHTML = files.map(f => {
|
||||
const url = '/api/sessions/' + state.sessionId + '/files/download?path=' + encodeURIComponent(f.filename);
|
||||
return '<div class="file-row"><span class="file-name">' + esc(f.filename) + '</span>' +
|
||||
'<span class="file-size">' + fmtSize(f.size) + '</span>' +
|
||||
'<a class="file-act" href="' + url + '">下载</a>' +
|
||||
'<button class="file-act" onclick="ui.downloadFile(\'' + esc(f.filename) + '\')">下载</button>' +
|
||||
'<button class="file-act del" onclick="ui.deleteFile(\'' + esc(f.filename).replace(/'/g, '%27') + '\')">删</button></div>';
|
||||
}).join('');
|
||||
} catch (e) { toast('读取文件失败:' + e.message); }
|
||||
@@ -380,6 +403,7 @@ async function loadFiles() {
|
||||
|
||||
// ---------- 主逻辑 ----------
|
||||
const ui = {
|
||||
downloadFile(name) { return downloadFile(name); },
|
||||
openCreate() { $('create-modal').classList.remove('hidden'); $('new-title').focus(); },
|
||||
closeCreate() { $('create-modal').classList.add('hidden'); },
|
||||
async createSession() {
|
||||
|
||||
@@ -70,6 +70,16 @@ router.get('/:id/files', function (req, res) {
|
||||
}
|
||||
});
|
||||
|
||||
// 下载统一响应:禁用一切缓存协商。
|
||||
// 默认的 ETag/Last-Modified + Cache-Control: public, max-age=0 会诱导移动端浏览器
|
||||
// 先发条件请求拿 304 空体,下载管理器随后卡在“等待中”(桌面 Safari/curl 不受影响)。
|
||||
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 });
|
||||
}
|
||||
|
||||
// 下载(支持子目录路径,前端用 ?path=xxx 传入相对路径)
|
||||
router.get('/:id/files/download', function (req, res) {
|
||||
try {
|
||||
@@ -78,7 +88,7 @@ router.get('/:id/files/download', function (req, res) {
|
||||
const abs = resolveInTask(engine.taskDirOf(req.params.id), rel);
|
||||
const st = fs.statSync(abs);
|
||||
if (!st.isFile()) return res.status(400).json({ error: '不是文件' });
|
||||
res.download(abs, path.basename(abs));
|
||||
sendDownload(res, abs);
|
||||
} catch (err) {
|
||||
res.status(404).json({ error: '文件不存在' });
|
||||
}
|
||||
@@ -110,7 +120,7 @@ router.get('/:id/files/*', function (req, res) {
|
||||
const abs = resolveInTask(engine.taskDirOf(req.params.id), rel);
|
||||
const st = fs.statSync(abs);
|
||||
if (!st.isFile()) return res.status(400).json({ error: '不是文件' });
|
||||
res.download(abs, path.basename(abs));
|
||||
sendDownload(res, abs);
|
||||
} catch (err) {
|
||||
res.status(404).json({ error: '文件不存在' });
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ app.use(express.json({ limit: '2mb' }));
|
||||
|
||||
// 可选 basic auth(若前置 nginx 已做,可不启用)
|
||||
if (cfg.auth && cfg.auth.user) {
|
||||
const expected = 'Basic ' + Buffer.From(cfg.auth.user + ':' + cfg.auth.password).toString('base64');
|
||||
const expected = 'Basic ' + Buffer.from(cfg.auth.user + ':' + cfg.auth.password).toString('base64');
|
||||
app.use((req, res, next) => {
|
||||
if (req.headers.authorization === expected) return next();
|
||||
res.set('WWW-Authenticate', 'Basic realm="agent"');
|
||||
|
||||
Reference in New Issue
Block a user