Skip to content

feat(files): file management for the explorer — create, rename, delete, download, archives, and in-place editing - #899

Closed
vow132 wants to merge 5 commits into
agegr:mainfrom
vow132:pr-file-management
Closed

vow132 wants to merge 5 commits into
agegr:mainfrom
vow132:pr-file-management

Conversation

@vow132

@vow132 vow132 commented Sep 19, 2026

Copy link
Copy Markdown

Summary

The file explorer is currently read-only (browse, upload, preview). This PR turns it into a lightweight file manager while keeping the existing security model untouched:

  • Context menu on files and folders: open, download, new file, new folder, rename, delete, extract, compress
  • Inline rename and create (row-level inputs with Enter/Escape), toolbar buttons to create entries at the workspace root, deletion confirmations
  • In-place editing for text files in the viewer: monospace textarea, Ctrl/Cmd+S to save, save-state indicator, unsaved-changes guard
  • Archives: extract ZIP/tar/tar.gz/tar.bz2/tar.xz into an auto-numbered sibling folder; compress any file or folder to a sibling ZIP

API

POST /api/files/[...path] gains write, touch, mkdir, rename, delete, extract, and compress actions next to the existing upload/upload-check flow. touch/mkdir address the destination directory (like upload); the others address the entry itself.

Security model

Every action is authorized through the same guards the route already uses for reads and uploads — no new trust decisions:

  • allowed-roots lexical check plus realpath containment of the resolved parent, so a symlinked ancestor can never move the target outside the workspace
  • symlinks are refused for write/extract/compress (and deleted links are unlinked without following); real files/directories must realpath inside the roots
  • entry names are validated against traversal, separators, and control characters (same rules as upload names)
  • write replaces the file atomically (temp file + rename), preserves the original mode, enforces a 2 MB cap, and rejects binary content via a NUL-byte sniff
  • deletes of directories require an explicit recursive flag; extract destinations are auto-numbered (name, name-1, …) so nothing is ever overwritten
  • the same isApiRequestAllowed cross-site check that guards uploads covers all new actions

Dependency note

extract/compress shell out to bsdtar (libarchive), whose default extraction handling refuses traversal outside the destination. It is an optional runtime dependency: without it the two actions return a descriptive 500 and everything else keeps working, and the new unit tests skip automatically when it is missing.

Tests

  • new unit tests for the mutation primitives (create/rename/delete/write guards, symlink behavior, mode preservation, size cap) and the archive round-trip incl. Chinese filenames and outside-root rejection — 14 new cases; they skip gracefully when bsdtar is absent
  • full suite: npm run lint, npx tsc --noEmit, npm test (1135 tests) and npm run build all green on this branch

Docs

  • README feature lists updated (en / zh-CN / ja / ru)
  • AGENTS.md file map entry for the files route mentions the mutation actions
  • i18n strings added for en, zh-CN, zh-TW

Pi Web Custom added 5 commits September 20, 2026 02:05
New files.* keys cover the explorer context menu (open, download, new file,
new folder, rename, delete, extract, compress), the inline rename and create
inputs, the viewer edit mode, and delete confirmations for en, zh-CN, and
zh-TW.
POST /api/files/[...path] gains write, touch, mkdir, rename, delete, extract,
and compress actions next to the existing upload flow. Every action is
authorized through the same guards the route already uses for reads and
uploads: the allowed-roots lexical check plus realpath containment of the
resolved parent, so a symlinked ancestor can never move the target outside
the workspace.

- lib/file-mutations.ts: create/rename/delete/write primitives. Writes are
  atomic (temp file + rename) and preserve the original mode; binary files
  are rejected via a NUL-byte sniff; symlinks are never followed for writes.
- lib/archive-names.ts + lib/file-archives.ts: extract into an auto-numbered
  sibling directory and compress into a sibling ZIP via bsdtar (libarchive),
  whose default extraction handling refuses traversal outside the
  destination. bsdtar is an optional runtime dependency: without it the two
  actions return a descriptive 500 and the new tests skip automatically.
- Entry names are validated against traversal, separators, and control
  characters, matching the existing upload name rules.
Adds a context menu for files and folders (open, download, new file, new
folder, rename, delete, extract, compress) with inline rename and create
inputs, toolbar buttons for creating entries at the workspace root, and an
inline error surface for failed mutations. Every mutation refreshes the tree
and notifies AppShell through onFileMutated so Git status stays in sync.
Adds an edit mode to TextFileViewer: a monospace textarea drafts the file,
Ctrl/Cmd+S saves through the write action, and Tab inserts spaces. The file
is loaded in full before editing starts (the read endpoint chunks above
256KB), capped at 2MB to match the write endpoint; server-side binary
detection and the existing truncation guard keep media and oversized files
out of the editor. Saving refreshes the view and Git state.
Kano-u added a commit to Kano-u/pi-web that referenced this pull request Sep 26, 2026
@agegr

agegr commented Sep 30, 2026

Copy link
Copy Markdown
Owner

Thanks for the work on this — the security model (realpath containment, symlink handling, name validation) is carefully done.

We're going to close it, though, for scope reasons rather than code quality: Pi Web is meant to stay a thin web front end for pi, and editing and managing files is the agent's job (or the user's own editor/IDE). A built-in file manager and editor would add a large surface we'd have to keep maintaining (line endings, encodings, concurrent writes with the agent, archive tooling), which is beyond what we want this project to cover.

Appreciate the contribution!

@agegr agegr closed this Sep 30, 2026
Kano-u pushed a commit to Kano-u/pi-web that referenced this pull request Oct 2, 2026
agegr#967 / agegr#963 已由上游的 agegr#1009 / agegr#1008 落地,agegr#961 上游原样合并(逐字节相同),
agegr#951 已收敛到上游的 agegr#907,agegr#960 上下游都拒绝——这些都与上游一致,按台账规则删行。
agegr#899 上游关闭但本地保留,状态注明为永久分叉。
Kano-u pushed a commit to Kano-u/pi-web that referenced this pull request Oct 2, 2026
…件批量开关)

唯一冲突是 AGENTS.md 的 lib/ 文件表:在同一个位置,上游插入
file-tree-visibility.ts、我们插入 fold-panes.ts —— 两行都保留(上游那行
紧跟 file-paths.ts,我们的跟在后面,减少以后撞车)。

agegr#1014 对 app/api/files/[...path]/route.ts 的改动(type=list 用
getFileTreeVisibility 过滤)与我们的 agegr#899 文件管理自动合并成功,两段逻辑
互补:上游决定"列不列",我们决定"每个 dirent 怎么判是不是目录"。

验证:tsc/lint 通过;list-route + file-tree-visibility + SkillsConfig.bulk
共 18 个测试 15 通过 3 跳过(上游自己声明 win32 跳过);全量 npm test 仅剩
AGENTS.md 记录的 6 个已知失败;e2e 14 项全绿(含 1280px/390px 文件面板)。
Kano-u pushed a commit to Kano-u/pi-web that referenced this pull request Oct 2, 2026
6 处冲突没有一处需要二选一,两个功能都保留:

- app/api/files/[...path]/route.ts(1 处)
  并用一个 type 变量分派:allow-link 交给上游的 allowLinkedDirectory,
  其余交给我们的 handleFileMutation;上游的 const type 提到 try 顶部,
  我们的 mutationType 变量随之去掉
- components/FileExplorer.tsx(5 处)
  1) 模块级:我们的 ContextMenuTarget/RenameState/CreateState + mutateFileEntry
     与上游的 responseError 并列
  2) 上游的 OutsideLinkIcon + 我们的 CreateEntryInput,TreeNode 保持 export
     (上游测试直接 import 它)
  3) 列表行状态:我们的 isRenaming/isCreatingHere 与上游的
     allowedLinkTarget/allowingLink/pendingLinkTarget 并列
  4) 名称区:我们的重命名输入框与上游的"链接指向外部"图标并存
  5) "空目录"占位:两个排除条件合并为
     !loadError && !(creating && creating.parentDir === node.fullPath)

集成修复(不是冲突,是上游测试暴露的真问题):我们的 agegr#899 给 TreeNode
加了 renaming/creating 两个必填 prop,而上游的
FileExplorer.linked-directory.test.mjs 直接渲染 TreeNode 且不传它们,
于是 creating !== null && creating.parentDir 抛 TypeError。改为
creating?.parentDir 并把两个 prop 标为可选——TreeNode 本来就应该能在
"没有重命名/没有新建"时渲染。

验证:tsc/lint 通过;app/api/files/*.test.mjs + FileExplorer*.test.mjs +
linked-directory + file-access + path-security 共 32 个测试全过(含上游新增
的 11 个链接目录测试);全量 npm test 仅剩 AGENTS.md 记录的 6 个已知失败;
e2e 14 项全绿。
Kano-u pushed a commit to Kano-u/pi-web that referenced this pull request Oct 2, 2026
新增「Fork-only additions (not in upstream)」一节,把散落在文件表和设计章节里的
自有内容收成一处清单:agegr#899 文件管理、新建项目目录设置(agegr#892)、目录选择器排序、
固定卡片折叠、以及 fork 文档与 .gitattributes。

顺带补上文件表里漏掉的自有文件:lib/file-mutations.ts、lib/file-archives.ts、
lib/archive-names.ts、components/PinnedCard.tsx、components/ThinkingCard.tsx、
components/NewProjectDialog.tsx。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants