Files
SIRIUSandwizardchen 49e262147c fix(cli): preserve directory structure in doc upload --recursive (#3753)
* fix(cli): preserve directory structure in doc upload --recursive

`doc upload --recursive` passed an empty customFileName to
CreateKnowledgeFromFile, so every file landed in the knowledge base root
and the directory tree it was asked to walk was lost. The web UI's folder
upload does not have this problem -- it passes the browser-supplied
relative path, which the server splits into folder_path + file_name via
types.SplitKnowledgeRelativePath.

Pass the path relative to the walked directory instead. Files directly
under that directory still send "" so the request stays byte-identical to
the previous behaviour for the common flat-directory case; only nested
files gain a folder prefix.

- knowledgeRelativePath() converts to forward slashes (the server splits on
  "/", so Windows backslashes would not separate) and returns "" for paths
  outside the walked root rather than leaking a "../" chain into the folder
  tree.
- The test double previously discarded customFileName with `_,` -- which is
  why this could regress with the suite green. It now records the value and
  two tests cover the nested and flat cases.

* fix(cli): reject only the parent component in knowledgeRelativePath

Review feedback on the recursive-upload folder path:

- `strings.HasPrefix(rel, "..")` also rejected children whose name merely
  starts with dots, e.g. `..drafts/report.pdf`. Replaced with a
  component-wise check (`rel == ".."` or `".."+separator` prefix).
- Root-level files must send ""; the guard now returns "" for
  `filepath.Dir(rel) == "."` explicitly rather than relying on the
  accidental prefix match to encode that rule.

Covered by a `..drafts/report.pdf` case in
TestUploadRecursive_PreservesFolderStructure.

* test(cli): expect an empty name for root files in knowledgeRelativePath

knowledgeRelativePath returns "" for files directly under the walked
root, so the test must expect that. Also wrap an over-long line.

---------

Co-authored-by: wizardchen <wizardchen@tencent.com>
2026-10-08 13:13:57 +08:00
..
2026-07-06 22:53:01 +08:00