任务文件支持选择文件夹打包下载:目录头勾选整选,选中项含文件夹时后端打包 zip 流式下载
- index.html:目录行加勾选框(整文件夹作为一个选中项,d: 前缀存储);全选/勾选状态按目录树折叠归一化(effectiveSelection);批量下载含文件夹时走 zip-token 签名直链,纯文件仍逐文件下载;批量删除支持目录整体删除 - routes/files.js:纯 Node 手拼 zip(STORE 不压缩 + CRC32,两遍读文件磁盘流发送,不整包进内存);新增 GET /:id/files/zip-token 签发 5 分钟打包下载链接、GET /:id/files/zip[-name/*] 流式返回 - auth.js:签名下载白名单认 zip 两种 URL 形态 - style.css:目录头勾选框选中态样式
This commit is contained in:
+3
-2
@@ -80,10 +80,11 @@ function isPublicPath(p) {
|
||||
}
|
||||
|
||||
// 带签名 token 的临时下载链接(files.js 自己验签),此处放行由路由层拒绝伪造请求。
|
||||
// 两种 URL 形态都要认:/files/download?path=… 与加固形态 /files/download-name/<名字>?path=…;
|
||||
// URL 形态都要认:/files/download?path=…、/files/download-name/<名字>?path=…,
|
||||
// 及打包下载 /files/zip?paths=…、/files/zip-name/<名字>?paths=…;
|
||||
// 只认旧形态会让 APK 系统下载器(不带 cookie、依赖 token)对新链接拿 401。
|
||||
function isSignedDownload(req) {
|
||||
return req.method === 'GET' && /\/files\/download(-name\/.+)?$/.test(req.path) && !!req.query.t;
|
||||
return req.method === 'GET' && /\/files\/(download(-name\/.+)?|zip(-name\/.+)?)$/.test(req.path) && !!req.query.t;
|
||||
}
|
||||
|
||||
// cfg.auth = { users: [{user, password}], secret?, ttl_ms? }
|
||||
|
||||
@@ -521,6 +521,13 @@ footer { flex: none; padding: 8px 12px calc(10px + env(safe-area-inset-bottom, 0
|
||||
}
|
||||
.files-panel .file-row.selected .file-check::after { opacity: 1; }
|
||||
|
||||
/* 目录头勾选框:复用 .file-check 圆形样式;选中态直接看 aria-checked(目录不在 .file-row 内) */
|
||||
.files-panel .file-dir-head .file-check { margin-top: 0; }
|
||||
.files-panel .file-dir-head .file-check[aria-checked="true"] {
|
||||
background: var(--active); border-color: var(--active);
|
||||
}
|
||||
.files-panel .file-dir-head .file-check[aria-checked="true"]::after { opacity: 1; }
|
||||
|
||||
/* 底部"全选"复选框:复用 .file-check 的尺寸与对勾伪元素(通过同时加 file-check class),
|
||||
区别是这里用方角、与行内圆区分开。状态由 data-state 决定,不进 .file-row。 */
|
||||
.files-panel .files-panel-check {
|
||||
|
||||
+160
-33
@@ -1826,18 +1826,96 @@
|
||||
$('attach-btn').addEventListener('click', toggleFilesPanel);
|
||||
$('files-mask').addEventListener('click', () => setFilesPanel(false)); // 点遮罩收起
|
||||
|
||||
// 任务文件面板多选状态:Set 保留当前选中的文件路径;filesAllPaths 保留当前面板所有路径
|
||||
// 任务文件面板多选状态:Set 保留当前选中的文件路径;filesAllPaths 保留当前面板所有路径。
|
||||
// 文件夹作为整体选中项存储(键加 d: 前缀),下载时由后端打包成 zip。
|
||||
const DIR_SEL_PREFIX = 'd:';
|
||||
const fileSelected = new Set();
|
||||
let filesAllPaths = [];
|
||||
let filesTreeRoot = null; // 最近一次渲染的目录树(供全选折叠/有效性计算)
|
||||
|
||||
const dirSelKey = p => DIR_SEL_PREFIX + p;
|
||||
const isDirSelKey = k => k.startsWith(DIR_SEL_PREFIX);
|
||||
const selDirPath = k => k.slice(DIR_SEL_PREFIX.length);
|
||||
// 文件是否被整体勾选的祖先目录覆盖
|
||||
function isPathInDirSel(p) {
|
||||
for (const k of fileSelected) {
|
||||
if (isDirSelKey(k) && p.startsWith(selDirPath(k) + '/')) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
// 文件是否被选中:自身被勾,或其某个祖先目录被整体勾选
|
||||
function isFileEffectivelySelected(p) {
|
||||
return fileSelected.has(p) || isPathInDirSel(p);
|
||||
}
|
||||
// 目录节点内所有文件的相对路径(递归)
|
||||
function filesUnderDir(node) {
|
||||
let out = node.files.map(f => f.filename);
|
||||
for (const d of node.dirs.values()) out = out.concat(filesUnderDir(d));
|
||||
return out;
|
||||
}
|
||||
// 树中全部目录节点(父在前),附各自包含的文件列表
|
||||
function collectDirNodes(node, out) {
|
||||
out = out || [];
|
||||
node = node || filesTreeRoot;
|
||||
if (!node) return out;
|
||||
for (const d of node.dirs.values()) {
|
||||
out.push({ path: d.path, files: filesUnderDir(d) });
|
||||
collectDirNodes(d, out);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
// 把选中集归一化为执行列表 [{path, isDir}]:
|
||||
// 1) 去掉已被整体勾选目录覆盖的单项文件;
|
||||
// 2) 目录内文件全部选中时折叠为目录项(父目录优先,避免重复打包)。
|
||||
function effectiveSelection() {
|
||||
const dirs = []; const files = [];
|
||||
for (const k of fileSelected) {
|
||||
if (isDirSelKey(k)) dirs.push(selDirPath(k)); else files.push(k);
|
||||
}
|
||||
const covered = p => dirs.some(d => p.startsWith(d + '/'));
|
||||
const kept = files.filter(p => !covered(p));
|
||||
for (const dn of collectDirNodes()) { // 父在前
|
||||
if (dirs.includes(dn.path)) continue;
|
||||
if (dirs.some(d => dn.path.startsWith(d + '/'))) continue; // 祖先已选
|
||||
if (dn.files.length && dn.files.every(p => fileSelected.has(p) || covered(p))) {
|
||||
for (let i = dirs.length - 1; i >= 0; i--) if (dirs[i].startsWith(dn.path + '/')) dirs.splice(i, 1);
|
||||
dirs.push(dn.path);
|
||||
for (let i = kept.length - 1; i >= 0; i--) if (kept[i].startsWith(dn.path + '/')) kept.splice(i, 1);
|
||||
}
|
||||
}
|
||||
return dirs.map(path => ({ path, isDir: true })).concat(kept.map(path => ({ path, isDir: false })));
|
||||
}
|
||||
|
||||
// 重算面板内(或指定范围内)文件行/目录勾选框的视觉状态:统一按选中集推导,
|
||||
// 避免各处手写 DOM 同步漏掉嵌套目录。目录勾选态 = 显式整选,或内部文件全部被覆盖。
|
||||
function refreshFileSelStates(scope) {
|
||||
scope = scope || $('files-list');
|
||||
scope.querySelectorAll('.file-row').forEach(r => {
|
||||
const p = r.dataset.path;
|
||||
const sel = !!p && isFileEffectivelySelected(p);
|
||||
r.classList.toggle('selected', sel);
|
||||
const ck = r.querySelector('.file-check');
|
||||
if (ck) ck.setAttribute('aria-checked', sel ? 'true' : 'false');
|
||||
});
|
||||
scope.querySelectorAll('.file-dir').forEach(dEl => {
|
||||
const ck = dEl.querySelector(':scope > .file-dir-head > .file-check');
|
||||
if (!ck) return;
|
||||
if (fileSelected.has(dirSelKey(dEl.dataset.dir))) { ck.setAttribute('aria-checked', 'true'); return; }
|
||||
const rows = Array.prototype.slice.call(dEl.querySelectorAll('.file-row'));
|
||||
ck.setAttribute('aria-checked',
|
||||
(rows.length > 0 && rows.every(r => isFileEffectivelySelected(r.dataset.path || ''))) ? 'true' : 'false');
|
||||
});
|
||||
}
|
||||
|
||||
function updateFilesFooter() {
|
||||
const n = fileSelected.size;
|
||||
const total = filesAllPaths.length;
|
||||
const effCount = effectiveSelection().length;
|
||||
const countEl = $('files-count');
|
||||
const dlBtn = $('files-bulk-download');
|
||||
const delBtn = $('files-bulk-delete');
|
||||
const togBtn = $('files-toggle-all');
|
||||
if (countEl) countEl.textContent = n ? '已选择 ' + n + ' 个文件' : '未选择文件';
|
||||
if (countEl) countEl.textContent = effCount ? '已选择 ' + effCount + ' 项' : '未选择文件';
|
||||
if (dlBtn) dlBtn.disabled = n === 0;
|
||||
if (delBtn) delBtn.disabled = n === 0;
|
||||
// 全选按钮:没有文件时禁用;全部选中=打勾态;部分选中=减号态;都未选=加号态
|
||||
@@ -1847,10 +1925,10 @@
|
||||
togBtn.setAttribute('aria-disabled', disabled ? 'true' : 'false');
|
||||
togBtn.tabIndex = disabled ? -1 : 0;
|
||||
let state = 'none';
|
||||
if (n === total && total > 0) state = 'all';
|
||||
if (total > 0 && filesAllPaths.every(isFileEffectivelySelected)) state = 'all';
|
||||
else if (n > 0) state = 'some';
|
||||
togBtn.dataset.state = state;
|
||||
togBtn.setAttribute('aria-checked', n === total && total > 0 ? 'true' : (n > 0 ? 'mixed' : 'false'));
|
||||
togBtn.setAttribute('aria-checked', state === 'all' ? 'true' : (n > 0 ? 'mixed' : 'false'));
|
||||
togBtn.title = state === 'all' ? '取消全选' : '全选';
|
||||
togBtn.setAttribute('aria-label', state === 'all' ? '取消全选' : '全选');
|
||||
}
|
||||
@@ -1862,20 +1940,13 @@
|
||||
if (total === 0) return;
|
||||
const togBtn = $('files-toggle-all');
|
||||
if (togBtn && togBtn.getAttribute('aria-disabled') === 'true') return;
|
||||
if (fileSelected.size === total) {
|
||||
if (filesAllPaths.every(p => fileSelected.has(p))) {
|
||||
fileSelected.clear();
|
||||
} else {
|
||||
for (const p of filesAllPaths) fileSelected.add(p);
|
||||
}
|
||||
// 同步每行的视觉状态
|
||||
const rows = $('files-list').querySelectorAll('.file-row');
|
||||
rows.forEach(r => {
|
||||
const p = r.dataset.path;
|
||||
const sel = fileSelected.has(p);
|
||||
r.classList.toggle('selected', sel);
|
||||
const ck = r.querySelector('.file-check');
|
||||
if (ck) ck.setAttribute('aria-checked', sel ? 'true' : 'false');
|
||||
});
|
||||
// 同步文件行与目录头勾选框的视觉状态
|
||||
refreshFileSelStates();
|
||||
updateFilesFooter();
|
||||
}
|
||||
|
||||
@@ -1925,7 +1996,7 @@
|
||||
// 缩进:每层加深一点,封顶避免深层目录把文件名挤出屏幕
|
||||
function filesIndent(depth) { return 8 + Math.min(depth, 3) * 14; }
|
||||
|
||||
function fileRowHtml(f, depth) {
|
||||
function fileRowHtml(f, depth, dirSel) {
|
||||
// 预览策略:md/txt/html + 图片在 fs-viewer 预览(pdf 不预)
|
||||
const low = f.filename.toLowerCase();
|
||||
const fsPv = /\.(md|txt|html?|png|jpe?g|gif|webp|svg)$/.test(low);
|
||||
@@ -1934,8 +2005,9 @@
|
||||
: '<svg class="file-ico doc" viewBox="0 0 24 24" aria-label="可下载"><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"></path><polyline points="14 2 14 8 20 8"></polyline></svg>';
|
||||
const name = esc(String(f.filename).split('/').pop() || f.filename);
|
||||
const ftime = fmtTime(f.ctime || f.mtime);
|
||||
return '<div class="file-row' + (fsPv ? ' clickable' : '') + '" data-path="' + esc(f.filename) + '" data-preview="' + (fsPv ? '1' : '0') + '" style="padding-left:' + filesIndent(depth) + 'px">' +
|
||||
'<span class="file-check" role="checkbox" aria-checked="false" aria-label="选择"></span>' +
|
||||
const sel = !!dirSel || isFileEffectivelySelected(f.filename);
|
||||
return '<div class="file-row' + (fsPv ? ' clickable' : '') + (sel ? ' selected' : '') + '" data-path="' + esc(f.filename) + '" data-preview="' + (fsPv ? '1' : '0') + '" style="padding-left:' + filesIndent(depth) + 'px">' +
|
||||
'<span class="file-check" role="checkbox" aria-checked="' + (sel ? 'true' : 'false') + '" aria-label="选择"></span>' +
|
||||
'<div class="file-main">' +
|
||||
'<div class="file-name">' + name + '</div>' +
|
||||
'<div class="file-meta">' + ico + '<span class="file-size">' + fmtSize(f.size) + '</span>' +
|
||||
@@ -1944,20 +2016,26 @@
|
||||
'</div>';
|
||||
}
|
||||
|
||||
function dirNodeHtml(node, depth) {
|
||||
// 目录头带勾选框:整个文件夹作为一个选中项(下载时后端打包成 zip);点其余区域仍为折叠/展开。
|
||||
// dirSel 为真时子孙目录/文件跟随父目录显示选中态。
|
||||
function dirNodeHtml(node, depth, dirSel) {
|
||||
const dirs = sortDirsByName(Array.from(node.dirs.values()));
|
||||
sortFilesByTime(node.files);
|
||||
const collapsed = filesCollapsedDirs.has(node.path);
|
||||
const under = filesUnderDir(node);
|
||||
const checked = !!dirSel || fileSelected.has(dirSelKey(node.path)) ||
|
||||
(under.length > 0 && under.every(isFileEffectivelySelected));
|
||||
return '<div class="file-dir' + (collapsed ? ' collapsed' : '') + '" data-dir="' + esc(node.path) + '">' +
|
||||
'<div class="file-dir-head" role="button" aria-expanded="' + (collapsed ? 'false' : 'true') + '" style="padding-left:' + filesIndent(depth) + 'px">' +
|
||||
'<span class="file-check" role="checkbox" aria-checked="' + (checked ? 'true' : 'false') + '" aria-label="选择文件夹"></span>' +
|
||||
'<svg class="file-dir-arrow" viewBox="0 0 24 24"><polyline points="9 18 15 12 9 6"></polyline></svg>' +
|
||||
'<svg class="file-dir-ico" viewBox="0 0 24 24"><path d="M22 19a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h5l2 3h9a2 2 0 0 1 2 2z"></path></svg>' +
|
||||
'<span class="file-dir-name">' + esc(node.name) + '</span>' +
|
||||
'<span class="file-dir-count">' + countDirFiles(node) + ' 个文件</span>' +
|
||||
'</div>' +
|
||||
'<div class="file-dir-body">' +
|
||||
dirs.map(function (d) { return dirNodeHtml(d, depth + 1); }).join('') +
|
||||
node.files.map(function (f) { return fileRowHtml(f, depth + 1); }).join('') +
|
||||
dirs.map(function (d) { return dirNodeHtml(d, depth + 1, checked); }).join('') +
|
||||
node.files.map(function (f) { return fileRowHtml(f, depth + 1, checked); }).join('') +
|
||||
'</div>' +
|
||||
'</div>';
|
||||
}
|
||||
@@ -1973,6 +2051,7 @@
|
||||
// 重新拉取前清空旧选择,避免脏数据遗留
|
||||
fileSelected.clear();
|
||||
filesAllPaths = [];
|
||||
filesTreeRoot = null;
|
||||
updateFilesFooter();
|
||||
try {
|
||||
const files = await req('GET', '/api/sessions/' + state.sessionId + '/files');
|
||||
@@ -1983,7 +2062,8 @@
|
||||
}
|
||||
filesAllPaths = files.map(f => f.filename);
|
||||
// 按目录分组渲染(后端已按时间倒序,组内再排一次防旧排序缓存)
|
||||
el.innerHTML = filesTreeHtml(buildFilesTree(files));
|
||||
filesTreeRoot = buildFilesTree(files);
|
||||
el.innerHTML = filesTreeHtml(filesTreeRoot);
|
||||
// 重新渲染后刷新底部计数与按钮状态
|
||||
updateFilesFooter();
|
||||
} catch (e) { toast('读取文件失败:' + e.message); }
|
||||
@@ -2075,19 +2155,23 @@
|
||||
} catch (e) { toast('删除失败:' + e.message); }
|
||||
}
|
||||
// 列表事件委托:
|
||||
// 1) 点击目录头:折叠/展开该目录(记住状态,刷新列表后保持);
|
||||
// 1) 点击目录头勾选框:整体选中/取消该文件夹(下载时后端打包成 zip);点目录头其余区域:折叠/展开该目录(记住状态,刷新列表后保持);
|
||||
// 2) 点击勾选框:仅选中状态,不触发预览/下载;
|
||||
// 3) 一旦处于多选模式(Set 非空),整行点击仅切换选中状态,不再预览/下载;
|
||||
// 4) 未进入多选模式时,点击文件名/图标根据 row.dataset.preview 决定预览或下载。
|
||||
$('files-list').addEventListener('click', e => {
|
||||
// 目录头:切换折叠/展开
|
||||
// 目录头:勾选框切换选中,其余区域切换折叠/展开
|
||||
const head = e.target.closest('.file-dir-head');
|
||||
if (head) {
|
||||
const dirEl = head.closest('.file-dir');
|
||||
if (!dirEl) return;
|
||||
const dp = dirEl.dataset.dir;
|
||||
if (dp && e.target.closest('.file-check')) {
|
||||
toggleDirSelect(dp, dirEl);
|
||||
return;
|
||||
}
|
||||
const collapsed = dirEl.classList.toggle('collapsed');
|
||||
head.setAttribute('aria-expanded', collapsed ? 'false' : 'true');
|
||||
const dp = dirEl.dataset.dir;
|
||||
if (dp) { if (collapsed) filesCollapsedDirs.add(dp); else filesCollapsedDirs.delete(dp); }
|
||||
return;
|
||||
}
|
||||
@@ -2098,18 +2182,59 @@
|
||||
const isCheck = !!e.target.closest('.file-check');
|
||||
// 多选模式下:任何点击(仅选框/文件名/其他)都只切换选中
|
||||
if (isCheck || fileSelected.size > 0) {
|
||||
if (fileSelected.has(path)) { fileSelected.delete(path); row.classList.remove('selected'); }
|
||||
else { fileSelected.add(path); row.classList.add('selected'); }
|
||||
const ck = row.querySelector('.file-check');
|
||||
if (ck) ck.setAttribute('aria-checked', fileSelected.has(path) ? 'true' : 'false');
|
||||
// 已被整体勾选文件夹覆盖的行:点选无效,选中与否由文件夹勾选框决定
|
||||
if (isPathInDirSel(path)) return;
|
||||
if (fileSelected.has(path)) { fileSelected.delete(path); }
|
||||
else { fileSelected.add(path); }
|
||||
// 同步本行与所在目录链的勾选态(目录可能因内部全选/取消全选而变)
|
||||
refreshFileSelStates();
|
||||
updateFilesFooter();
|
||||
return;
|
||||
}
|
||||
// 默认模式:依据是否可预览选择预览/下载
|
||||
// 默认模式:依据是否可预览选择预览/下载
|
||||
if (row.dataset.preview === '1') previewInFs(path);
|
||||
else downloadFile(path);
|
||||
});
|
||||
|
||||
// 整体选中/取消一个文件夹:目录作为单条选中项(d: 前缀),内部行视觉态由 refreshFileSelStates 重算。
|
||||
// 整体勾选时取消内部单项/子目录勾选(已被覆盖,避免重复计入);取消勾选时子项恢复未选。
|
||||
function toggleDirSelect(dirPath, dirEl) {
|
||||
const key = dirSelKey(dirPath);
|
||||
const on = !fileSelected.has(key);
|
||||
if (on) {
|
||||
fileSelected.add(key);
|
||||
// 整体勾选后,内部单项文件/子目录勾选都被覆盖,直接移除避免重复计入
|
||||
const prefix = dirPath + '/';
|
||||
for (const k of Array.from(fileSelected)) {
|
||||
if (k === key) continue;
|
||||
const p = isDirSelKey(k) ? selDirPath(k) : k;
|
||||
if (p.startsWith(prefix)) fileSelected.delete(k);
|
||||
}
|
||||
} else {
|
||||
fileSelected.delete(key);
|
||||
}
|
||||
const ck = dirEl.querySelector(':scope > .file-dir-head > .file-check');
|
||||
if (ck) ck.setAttribute('aria-checked', on ? 'true' : 'false');
|
||||
// 子孙目录/文件跟随父目录选中态;取消时按“自身被选或被其他祖先目录覆盖”重新计算
|
||||
refreshFileSelStates(dirEl);
|
||||
updateFilesFooter();
|
||||
}
|
||||
|
||||
// 批量下载:选中项含文件夹(含全选后整目录被覆盖的情况)时,整个选择交给后端打包成
|
||||
// 一个 zip 一次下载;纯文件选择保持逐文件直链下载。zip 同样走签名直链(与单文件同链路)。
|
||||
async function bulkZipDownload(paths) {
|
||||
try {
|
||||
const r = await req('GET', '/api/sessions/' + state.sessionId + '/files/zip-token?paths=' + encodeURIComponent(JSON.stringify(paths)));
|
||||
if (!r || !r.url) throw new Error('未拿到打包链接');
|
||||
const a = document.createElement('a');
|
||||
a.href = r.url;
|
||||
a.download = r.name || 'archive.zip';
|
||||
a.style.display = 'none';
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
setTimeout(() => { try { a.remove(); } catch (_) {} }, 1000);
|
||||
} catch (e) { toast('打包下载失败:' + e.message); }
|
||||
}
|
||||
// 批量下载:先把所有 download-token 拿完,再用服务器直链逐个触发下载。
|
||||
// 两个历史坑:
|
||||
// 1) 边拿 token 边 location.href:首个下载导航会打断后续 fetch(第 2 个报失败);
|
||||
@@ -2119,7 +2244,9 @@
|
||||
// → 回到服务器原始直链(文件名跟 Content-Disposition),与单文件下载同一条链路。
|
||||
async function bulkDownload() {
|
||||
if (!state.sessionId || fileSelected.size === 0) return;
|
||||
const names = Array.from(fileSelected);
|
||||
const eff = effectiveSelection();
|
||||
if (eff.some(x => x.isDir)) { bulkZipDownload(eff.map(x => x.path)); return; }
|
||||
const names = eff.map(x => x.path);
|
||||
// 第一阶段:全部 token 先拿到手(此时还没发生任何导航,fetch 不会被打断)
|
||||
const items = [];
|
||||
for (const name of names) {
|
||||
@@ -2144,8 +2271,8 @@
|
||||
}
|
||||
async function bulkDelete() {
|
||||
if (!state.sessionId || fileSelected.size === 0) return;
|
||||
const names = Array.from(fileSelected);
|
||||
if (!await confirmModal('批量删除', '确认删除选中的 ' + names.length + ' 个文件?', '删除', true)) return;
|
||||
const names = effectiveSelection().map(x => x.path); // 目录整体删除(后端 rm -r,含子项)
|
||||
if (!await confirmModal('批量删除', '确认删除选中的 ' + names.length + ' 项(文件夹会连同内部文件一起删除)?', '删除', true)) return;
|
||||
let ok = 0, fail = 0;
|
||||
for (const name of names) {
|
||||
try {
|
||||
@@ -2153,7 +2280,7 @@
|
||||
ok++;
|
||||
} catch (e) { fail++; toast('删除失败:' + name + ' ' + e.message); }
|
||||
}
|
||||
if (ok) toast('已删除 ' + ok + ' 个文件');
|
||||
if (ok) toast('已删除 ' + ok + ' 项');
|
||||
loadFiles(); // 重新拉取并重置选中
|
||||
}
|
||||
$('files-bulk-download').addEventListener('click', bulkDownload);
|
||||
|
||||
@@ -179,6 +179,248 @@ router.get('/:id/files/download', serveDownload);
|
||||
// 供解析不了 Content-Disposition 的下载器兜底;path 参数与验签逻辑完全一致。
|
||||
router.get('/:id/files/download-name/*', serveDownload);
|
||||
|
||||
// ---------- 打包下载(选中项含文件夹时,后端把整个选择打成 zip 流式返回) ----------
|
||||
// 纯 Node 实现(STORE 不压缩 + 手拼 zip 结构):任务目录里多是媒体/生成产物,
|
||||
// 压缩收益低;两遍读文件(先算 CRC 再磁盘流发送),内存占用与总大小无关。
|
||||
|
||||
const CRC32_TABLE = (function () {
|
||||
const t = new Int32Array(256);
|
||||
for (let n = 0; n < 256; n++) {
|
||||
let c = n;
|
||||
for (let k = 0; k < 8; k++) c = (c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1);
|
||||
t[n] = c;
|
||||
}
|
||||
return t;
|
||||
})();
|
||||
function crc32Update(c, buf) {
|
||||
for (let i = 0; i < buf.length; i++) c = (c >>> 8) ^ CRC32_TABLE[(c ^ buf[i]) & 0xFF];
|
||||
return c;
|
||||
}
|
||||
function crc32Final(c) { return (c ^ 0xFFFFFFFF) >>> 0; }
|
||||
|
||||
// 文件修改时间 → zip 的 DOS time/date 两个 16 位字段
|
||||
function dosDateTime(ms) {
|
||||
const d = new Date(ms || Date.now());
|
||||
return {
|
||||
time: (d.getHours() << 11) | (d.getMinutes() << 5) | (d.getSeconds() >> 1),
|
||||
date: (Math.max(d.getFullYear() - 1980, 0) << 9) | ((d.getMonth() + 1) << 5) | d.getDate(),
|
||||
};
|
||||
}
|
||||
|
||||
// zip 条目一律以任务目录为根(rel 用正斜杠),保留所选文件夹内部层级
|
||||
function collectZipEntries(dir, paths) {
|
||||
const entries = [];
|
||||
const seen = new Set();
|
||||
const addFile = function (abs, rel, st) {
|
||||
if (seen.has(rel)) return;
|
||||
seen.add(rel);
|
||||
if (st.size >= 0xFFFFFFFF) {
|
||||
throw Object.assign(new Error('文件超过 4GB,无法打包:' + rel), { status_code: 400 });
|
||||
}
|
||||
entries.push({ abs, rel, size: st.size, mtime: st.mtimeMs });
|
||||
};
|
||||
const walk = function (absDir, relDir) {
|
||||
let list;
|
||||
try { list = fs.readdirSync(absDir, { withFileTypes: true }); } catch (_) { return; }
|
||||
for (const e of list) {
|
||||
const abs = path.join(absDir, e.name);
|
||||
const rel = relDir ? relDir + '/' + e.name : e.name;
|
||||
let st;
|
||||
try { st = fs.statSync(abs); } catch (_) { continue; }
|
||||
if (st.isDirectory()) walk(abs, rel);
|
||||
else if (st.isFile()) addFile(abs, rel, st);
|
||||
}
|
||||
};
|
||||
for (const p of paths) {
|
||||
const abs = resolveInTask(dir, p);
|
||||
const st = fs.statSync(abs);
|
||||
const rel = path.relative(dir, abs).split(path.sep).join('/');
|
||||
if (st.isFile()) addFile(abs, rel, st);
|
||||
else if (st.isDirectory()) walk(abs, rel);
|
||||
}
|
||||
if (!entries.length) throw Object.assign(new Error('没有可打包的文件'), { status_code: 400 });
|
||||
if (entries.length > 65535) throw Object.assign(new Error('文件过多,无法打包'), { status_code: 400 });
|
||||
return entries;
|
||||
}
|
||||
|
||||
function zipLocalHeader(e) {
|
||||
const name = Buffer.from(e.rel, 'utf8');
|
||||
const b = Buffer.alloc(30);
|
||||
b.writeUInt32LE(0x04034b50, 0);
|
||||
b.writeUInt16LE(20, 4); // version needed
|
||||
b.writeUInt16LE(0x0800, 6); // flags: UTF-8 文件名
|
||||
b.writeUInt16LE(0, 8); // method: store
|
||||
const dt = dosDateTime(e.mtime);
|
||||
b.writeUInt16LE(dt.time, 10); b.writeUInt16LE(dt.date, 12);
|
||||
b.writeUInt32LE(e.crc, 14);
|
||||
b.writeUInt32LE(e.size, 18); b.writeUInt32LE(e.size, 22);
|
||||
b.writeUInt16LE(name.length, 26);
|
||||
b.writeUInt16LE(0, 28);
|
||||
return Buffer.concat([b, name]);
|
||||
}
|
||||
|
||||
function zipCentralEntry(e) {
|
||||
const name = Buffer.from(e.rel, 'utf8');
|
||||
const b = Buffer.alloc(46);
|
||||
b.writeUInt32LE(0x02014b50, 0);
|
||||
b.writeUInt16LE(20, 4); b.writeUInt16LE(20, 6);
|
||||
b.writeUInt16LE(0x0800, 8); b.writeUInt16LE(0, 10);
|
||||
const dt = dosDateTime(e.mtime);
|
||||
b.writeUInt16LE(dt.time, 12); b.writeUInt16LE(dt.date, 14);
|
||||
b.writeUInt32LE(e.crc, 16);
|
||||
b.writeUInt32LE(e.size, 20); b.writeUInt32LE(e.size, 24);
|
||||
b.writeUInt16LE(name.length, 28);
|
||||
b.writeUInt16LE(0, 30); b.writeUInt16LE(0, 32); // extra / comment 长度
|
||||
b.writeUInt16LE(0, 34); b.writeUInt16LE(0, 36); // disk start / internal attrs
|
||||
b.writeUInt32LE(0, 38); // external attrs
|
||||
b.writeUInt32LE(e.offset, 42); // local header 偏移
|
||||
return Buffer.concat([b, name]);
|
||||
}
|
||||
|
||||
function zipEocd(count, cdSize, cdOffset) {
|
||||
const b = Buffer.alloc(22);
|
||||
b.writeUInt32LE(0x06054b50, 0);
|
||||
b.writeUInt16LE(0, 4); b.writeUInt16LE(0, 6);
|
||||
b.writeUInt16LE(count, 8); b.writeUInt16LE(count, 10);
|
||||
b.writeUInt32LE(cdSize, 12); b.writeUInt32LE(cdOffset, 16);
|
||||
return b;
|
||||
}
|
||||
|
||||
function writeChunk(res, buf) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
res.write(buf, function (err) { err ? reject(err) : resolve(); });
|
||||
});
|
||||
}
|
||||
function pipeFileToRes(res, abs) {
|
||||
return new Promise(function (resolve, reject) {
|
||||
const s = fs.createReadStream(abs);
|
||||
s.on('error', reject);
|
||||
s.on('end', resolve);
|
||||
s.pipe(res, { end: false });
|
||||
});
|
||||
}
|
||||
|
||||
// 流式写出 zip:每个条目两遍读(第一遍算 CRC,第二遍发送),不整包进内存
|
||||
async function hashFileForZip(abs) {
|
||||
let c = 0xFFFFFFFF;
|
||||
await new Promise(function (resolve, reject) {
|
||||
const s = fs.createReadStream(abs);
|
||||
s.on('data', function (buf) { c = crc32Update(c, buf); });
|
||||
s.on('error', reject);
|
||||
s.on('end', resolve);
|
||||
});
|
||||
return crc32Final(c);
|
||||
}
|
||||
|
||||
async function streamZip(res, entries) {
|
||||
for (const e of entries) e.crc = await hashFileForZip(e.abs);
|
||||
let offset = 0;
|
||||
const central = [];
|
||||
for (const e of entries) {
|
||||
const head = zipLocalHeader(e);
|
||||
e.offset = offset;
|
||||
await writeChunk(res, head); offset += head.length;
|
||||
await pipeFileToRes(res, e.abs); offset += e.size;
|
||||
central.push(zipCentralEntry(e));
|
||||
}
|
||||
let cdSize = 0;
|
||||
for (const b of central) { await writeChunk(res, b); cdSize += b.length; }
|
||||
await writeChunk(res, zipEocd(entries.length, cdSize, offset));
|
||||
}
|
||||
|
||||
// paths 参数:URL 编码的 JSON 字符串数组(可含文件与文件夹相对路径)
|
||||
function parseZipPaths(raw) {
|
||||
let arr;
|
||||
try { arr = JSON.parse(String(raw || '')); } catch (_) { arr = null; }
|
||||
if (!Array.isArray(arr) || !arr.length || arr.length > 500 ||
|
||||
!arr.every(p => typeof p === 'string' && !!p.trim())) {
|
||||
throw Object.assign(new Error('paths 参数无效'), { status_code: 400 });
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
|
||||
// 打包名:单个文件夹 → <名字>.zip;多项 → task-files-<时间戳>.zip
|
||||
function zipNameFor(paths) {
|
||||
if (paths.length === 1) {
|
||||
const leaf = path.basename(String(paths[0]).replace(/\/+$/, '')) || 'archive';
|
||||
const noExt = leaf.replace(/\.[^.]+$/, '');
|
||||
return (noExt || leaf) + '.zip';
|
||||
}
|
||||
const d = new Date();
|
||||
const pad = function (n) { return String(n).padStart(2, '0'); };
|
||||
return 'task-files-' + d.getFullYear() + pad(d.getMonth() + 1) + pad(d.getDate()) +
|
||||
'-' + pad(d.getHours()) + pad(d.getMinutes()) + pad(d.getSeconds()) + '.zip';
|
||||
}
|
||||
|
||||
function signZipToken(sessionId, pathsRaw, expires) {
|
||||
return crypto.createHmac('sha256', downloadTokenSecret)
|
||||
.update(sessionId + '\nzip\n' + pathsRaw + '\n' + expires).digest('hex');
|
||||
}
|
||||
function validZipToken(sessionId, pathsRaw, expires, t) {
|
||||
const exp = Number(expires);
|
||||
if (!t || !Number.isFinite(exp) || exp < Date.now()) return false;
|
||||
const expected = signZipToken(sessionId, pathsRaw, exp);
|
||||
try {
|
||||
return crypto.timingSafeEqual(Buffer.from(String(t), 'utf8'), Buffer.from(expected, 'utf8'));
|
||||
} catch (_) { return false; }
|
||||
}
|
||||
|
||||
// 签发打包下载链接(需正常认证;与单文件下载同一条 token 链路)
|
||||
router.get('/:id/files/zip-token', function (req, res) {
|
||||
try {
|
||||
const raw = String(req.query.paths || '');
|
||||
const paths = parseZipPaths(raw);
|
||||
const dir = engine.taskDirOf(req.params.id);
|
||||
for (const p of paths) fs.statSync(resolveInTask(dir, p)); // 先验路径安全与存在性
|
||||
const expires = Date.now() + DOWNLOAD_TOKEN_TTL_MS;
|
||||
const t = signZipToken(req.params.id, raw, expires);
|
||||
const name = zipNameFor(paths);
|
||||
// URL 末段带上 zip 名,供解析不了 Content-Disposition 的下载器兜底(同 download-name)
|
||||
res.json({
|
||||
name,
|
||||
url: '/api/sessions/' + encodeURIComponent(req.params.id) + '/files/zip-name/'
|
||||
+ encodeURIComponent(name) + '?paths='
|
||||
+ encodeURIComponent(raw) + '&expires=' + expires + '&t=' + t,
|
||||
});
|
||||
} catch (err) {
|
||||
res.status(err.status_code || 404).json({ error: String(err && err.message || err) });
|
||||
}
|
||||
});
|
||||
|
||||
// zip 流式下载核心(token 验签逻辑与 serveDownload 一致;无 token 时走 cookie 认证)
|
||||
async function serveZip(req, res) {
|
||||
try {
|
||||
const raw = String(req.query.paths || '');
|
||||
if (req.query.t && !validZipToken(req.params.id, raw, req.query.expires, String(req.query.t))) {
|
||||
return res.status(403).json({ error: '下载链接已过期或无效' });
|
||||
}
|
||||
const paths = parseZipPaths(raw);
|
||||
const dir = engine.taskDirOf(req.params.id);
|
||||
const entries = collectZipEntries(dir, paths);
|
||||
const name = zipNameFor(paths);
|
||||
log.info('file', (req.user || '-') + ' ' + req.params.id + ' 打包下载 ' + name +
|
||||
'(' + paths.length + ' 项 / ' + entries.length + ' 个文件)');
|
||||
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate');
|
||||
res.setHeader('Pragma', 'no-cache');
|
||||
res.setHeader('Expires', '0');
|
||||
res.setHeader('Content-Type', 'application/zip');
|
||||
res.setHeader('Content-Disposition', downloadDisposition(name));
|
||||
await streamZip(res, entries);
|
||||
res.end();
|
||||
} catch (err) {
|
||||
if (!res.headersSent) {
|
||||
res.status(err.status_code || 500).json({ error: String(err && err.message || err) });
|
||||
} else {
|
||||
log.error('file', 'zip 流中断', err);
|
||||
try { res.destroy(); } catch (_) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
router.get('/:id/files/zip', function (req, res) { serveZip(req, res); });
|
||||
// 加固形式:URL 末段带 zip 文件名;paths 与验签逻辑与 /zip 完全一致
|
||||
router.get('/:id/files/zip-name/*', function (req, res) { serveZip(req, res); });
|
||||
|
||||
router.delete('/:id/files', function (req, res) {
|
||||
try {
|
||||
const rel = String(req.query.path || '');
|
||||
|
||||
@@ -369,7 +369,7 @@ stateDiagram-v2
|
||||
|---|---|---|
|
||||
| `/api/sessions` | `routes/sessions.js` | `POST /` 创建、`GET /` 列表(分页 + 搜索 + user 隔离)、`GET /:id` 详情、`PATCH /:id` 改名、`DELETE /:id?delete_dir=...`、`POST /:id/bind` |
|
||||
| `/api/sessions/:id/messages` | `routes/messages.js` | `POST /` 发消息(`delivered:'live'\|'queued'`)、`POST /start` "开始执行" 模板消息、`POST /stop`、`POST /release`(held→completed)、`DELETE /messages`(清空 + ?keep=summary)、`POST /summarize`(压缩)、`DELETE /messages/:mid`、`GET /stream` SSE 事件流 |
|
||||
| `/api/sessions/:id/files` | `routes/files.js` | `POST /` 上传(multer,50MB/文件,文件名清洗 sanitizeName)、`GET /` 列表(递归)、`GET /download[-name/...]` 签名下载(5 分钟 TTL)、`DELETE /` 删文件 |
|
||||
| `/api/sessions/:id/files` | `routes/files.js` | `POST /` 上传(multer,50MB/文件,文件名清洗 sanitizeName)、`GET /` 列表(递归)、`GET /download[-name/...]` 签名下载(5 分钟 TTL)、`GET /zip-token` + `GET /zip[-name/...]` 选中项(含文件夹)打包 zip 流式下载(纯 Node STORE,两遍读:先 CRC 后磁盘流发送)、`DELETE /` 删文件(目录递归删) |
|
||||
| `/api/skills` | `routes/skills.js` | 技能列表/创建/重命名/删除 + 技能内文件 CRUD + 上传(`?overwrite=true` 覆盖) |
|
||||
| `/api/config` | `routes/config.js` | `GET /` 原文+生效(脱敏)、`POST /validate`、`PUT /` 整文件保存、`PATCH /` 单项修改(path/value/op[delete\|append]) |
|
||||
| `/api/platforms` | `routes/platforms.js` | `GET /balance?pi=&ki=` 余额 |
|
||||
@@ -378,7 +378,7 @@ stateDiagram-v2
|
||||
| `/api/health` | `server.js` | 健康检查 |
|
||||
| `/api/login` `/api/login_restore` `/api/logout` `/api/password` `/api/me` | `auth.js` | 认证路由(认证前) |
|
||||
| `/login` `/logout` | `auth.js` | 登录/登出页 |
|
||||
| `/files/download[-name/...]?t=` | `auth.js` 白名单 | 带签名 token 的下载(认证中免 cookie) |
|
||||
| `/files/download[-name/...]?t=` `/files/zip[-name/...]?t=` | `auth.js` 白名单 | 带签名 token 的下载/打包下载(认证中免 cookie) |
|
||||
|
||||
**SSE 事件**(`/api/sessions/:id/stream`,由 `bus.emit` 触发,前端订阅):
|
||||
|
||||
|
||||
Reference in New Issue
Block a user