feat(local): 文件树提升为桌面端通用入口,接管旧文件面板

桌面端(含服务器形态)用左栏懒加载文件树取代输入行右侧的旧「任务文件」浮层:
点文件进右侧预览,行内可下载 / 删除;local 形态额外提供「在 Finder 中显示」。

- routes/files.js:新增 GET /:id/tree(单层懒加载、忽略大目录、上限 500、
  字面 + realpath 双重越界校验),替代会同步递归整棵目录的 GET /:id/files
- routes/local.js:移除 /tree(上收为通用接口),只留 pick-dir / reveal / terminal
- local-files.js → files-tree.js:两种形态共用;local 能力靠 /api/local/info 探测,
  服务器形态自动隐藏 Finder/终端入口
- 树头部复用预览栏的 .pv-head / .pv-addr / .pv-actions / .icon-btn:
  左右并排时高度、图标尺寸与描边完全一致
- 树头不再显示会话标题(原样搬过来容易和会话列表重复),改为显示工作目录路径
- 桌面端隐藏 #files-btn;手机端保留旧面板
- 上传完成后同步刷新文件树
This commit is contained in:
2026-11-11 15:03:04 +08:00
parent 2d5467203b
commit 8ef4ec1ee4
8 changed files with 384 additions and 312 deletions
+66
View File
@@ -76,6 +76,72 @@ router.post('/:id/files', upload.array('files', 10), function (req, res) {
}
});
// ---------- 会话文件树(桌面端左栏唯一浏览入口) ----------
// 为什么不复用 GET /:id/files:那个接口同步递归整棵目录,会话里一旦有大项目 /
// node_modules,单线程 core 会被卡死(连 SSE 一起挂)。这里一律「一次一层」、
// 忽略大目录、限量返回,客户端展开才请求下一层。
const TREE_IGNORE_DIRS = new Set([
'node_modules', '.git', 'dist', 'build', 'out', 'target', '.venv', 'venv',
'__pycache__', '.next', '.cache', 'coverage', '.idea', '.vscode',
]);
const TREE_MAX = 500;
// 相对路径 → 会话根内的绝对路径。字面 + realpath 双重校验,防符号链接跳出会话目录。
function resolveTreeAbs(rootAbs, rel) {
const p = String(rel || '').replace(/^[\\/]+/, '');
const abs = p ? path.resolve(rootAbs, p) : rootAbs;
const inside = (root, x) => x === root || x.startsWith(root + path.sep);
if (!inside(rootAbs, abs)) throw new Error('路径越界');
let realAbs = abs, realRoot = rootAbs;
try { realAbs = fs.realpathSync(abs); } catch (_) {}
try { realRoot = fs.realpathSync(rootAbs); } catch (_) {}
if (!inside(realRoot, realAbs)) throw new Error('路径越界');
return abs;
}
router.get('/:id/tree', function (req, res) {
const session = engine.getSession(req.params.id);
if (!session) return res.status(404).json({ error: '会话不存在' });
const rootAbs = path.resolve(engine.taskDirOf(req.params.id));
const rel = String(req.query.dir || '').replace(/^[\\/]+/, '').replace(/\/+$/, '');
let abs;
try { abs = resolveTreeAbs(rootAbs, rel); }
catch (err) { return res.status(400).json({ error: err.message }); }
let st;
try { st = fs.statSync(abs); } catch (_) { return res.status(404).json({ error: '目录不存在' }); }
if (!st.isDirectory()) return res.status(400).json({ error: '不是目录' });
const showHidden = req.query.hidden === '1';
const entries = [];
try {
for (const e of fs.readdirSync(abs, { withFileTypes: true })) {
if (!showHidden && e.name.startsWith('.')) continue;
const childRel = rel ? rel + '/' + e.name : e.name;
if (e.isSymbolicLink()) { entries.push({ name: e.name, rel: childRel, type: 'link' }); continue; }
if (e.isDirectory()) {
entries.push({ name: e.name, rel: childRel, type: 'dir', ignored: TREE_IGNORE_DIRS.has(e.name) });
} else if (e.isFile()) {
let size = null;
try { size = fs.statSync(path.join(abs, e.name)).size; } catch (_) {}
entries.push({ name: e.name, rel: childRel, type: 'file', size: size });
}
}
} catch (err) {
return res.status(500).json({ error: String((err && err.message) || err) });
}
// 排序:正常目录 → 正常文件 → 被忽略的大目录(灰显沉底);同类按名称
entries.sort((a, b) =>
(a.ignored ? 1 : 0) - (b.ignored ? 1 : 0) ||
(a.type === 'dir' ? 0 : 1) - (b.type === 'dir' ? 0 : 1) ||
(a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
res.json({
root: rootAbs,
dir: rel,
total: entries.length,
capped: entries.length > TREE_MAX,
entries: entries.slice(0, TREE_MAX),
});
});
router.get('/:id/files', function (req, res) {
try {
const session = engine.getSession(req.params.id);