PDF 预览改用 inline 直连,支持 Range 与 304 缓存

新增 /files/inline;桌面面板与窄窗口统一用 iframe 直连,不再 fetch 整包转 blob;下载接口保持 attachment/no-store 不变。
This commit is contained in:
2026-10-26 11:16:26 +08:00
parent 774d09a07a
commit dbd67ec273
2 changed files with 48 additions and 3 deletions
+20 -3
View File
@@ -31,6 +31,12 @@ function pvPreviewUrl(rel, w) {
'/files/preview?path=' + encodeURIComponent(rel) + '&w=' + w;
}
// PDF 预览直连:让内建查看器自己发 Range 请求,不必先 fetch 整包转 blob。
function pvPdfUrl(rel) {
return '/api/sessions/' + encodeURIComponent(state.sessionId) +
'/files/inline?path=' + encodeURIComponent(rel);
}
function pvKind(name) {
const low = String(name).toLowerCase();
if (/\.html?$/i.test(low)) return 'html';
@@ -60,9 +66,9 @@ function pvImgGroup(name) {
// 刷新/新窗口两个钮对**所有**预览类型统一开放:
// refresh → pvState.reload(各类型都注入「按同一路径重开」,与地址条回车同一条路)
// popup → pvState.url(office=预览网关;图片=inline 预览直链;文本/HTML/PDF=blob URL;
// 不支持的类型=下载地址)
// 下载接口是 attachment,直接导航只会触发下载;图片走 /files/preview(inline)可直接开新窗口。
// popup → pvState.url(office=预览网关;图片/PDF=inline 直链;
// 文本/HTML=blob URL;不支持的类型=下载地址)
// 下载接口是 attachment,直接导航只会触发下载;图片/PDF 走各自的 inline 路由可直接开新窗口。
function pvToolbar(o) {
document.getElementById('pv-refresh').classList.toggle('hidden', !o.refresh);
document.getElementById('pv-popup').classList.toggle('hidden', !o.popup);
@@ -254,6 +260,16 @@ async function openFilePreview(name, sizeText) {
}
return;
}
// PDF 直连 inline 路由:桌面面板与窄窗口(全屏 overlay)都让内建查看器按 Range 取字节,
// 不再 fetch 整包转 blob。show() 会按 desk 自动落到右侧面板或 #fs-viewer。
if (kind === 'pdf') {
const url = pvPdfUrl(name);
const fr = document.createElement('iframe');
fr.className = desk ? 'pv-frame' : 'fs-html-frame';
fr.title = String(name); fr.src = url;
show(fr, Object.assign({}, tbAll, { url }));
return;
}
let r;
try {
r = await fetch('/api/sessions/' + state.sessionId + '/files/download?path=' + encodeURIComponent(name));
@@ -389,6 +405,7 @@ async function pvRenderLegacy(name, r, my) {
pvOverlayShow(iframe, { bar: true, name: name, source: html }); // 全宽沉浸,源码可看/可复制
if (my !== pvSeq) return;
} else if (/\.pdf$/i.test(name)) {
// 正常路径已在 openFilePreview 用 /files/inline 直连;这里仅作旧路径兜底。
const u = URL.createObjectURL(await r.blob());
if (state.lastPdfBlobUrl) { try { URL.revokeObjectURL(state.lastPdfBlobUrl); } catch (_) {} }
state.lastPdfBlobUrl = u;
+28
View File
@@ -215,6 +215,34 @@ router.get('/:id/files/preview', async function (req, res) {
}
});
// ---------- PDF 预览(inline,供 iframe 直连) ----------
// 现有下载接口是 attachment + no-store:前端只能 fetch 整包再转 blob,查看器要等整包下完才出现。
// 这里单独给预览一条 inline 路由,保留 sendFile 的 Range 支持,让内建查看器自己按需取字节;
// ETag/Last-Modified 让重复打开走 304,不再重下。下载按钮仍走原 /files/download,语义不变。
router.get('/:id/files/inline', function (req, res) {
try {
const rel = String(req.query.path || '');
if (!rel) return res.status(400).json({ error: '缺少 path 参数' });
if (!/\.pdf$/i.test(rel)) return res.status(400).json({ error: '不是 PDF' });
const abs = resolveInTask(engine.taskDirOf(req.params.id), rel);
const st = fs.statSync(abs);
if (!st.isFile()) return res.status(400).json({ error: '不是文件' });
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-Disposition', 'inline');
res.setHeader('Cache-Control', 'private, max-age=0, must-revalidate');
res.sendFile(abs, {
cacheControl: false,
dotfiles: 'allow',
etag: true,
lastModified: true,
acceptRanges: true,
});
} catch (err) {
res.status(404).json({ error: '文件不存在' });
}
});
// ---------- 打包下载(选中项含文件夹时,后端把整个选择打成 zip 流式返回) ----------
// 纯 Node 实现(STORE 不压缩 + 手拼 zip 结构):任务目录里多是媒体/生成产物,
// 压缩收益低;两遍读文件(先算 CRC 再磁盘流发送),内存占用与总大小无关。