mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-05 00:40:13 +08:00
feat(knowledge): choose which KB folder an upload batch lands in
The last piece of #866. #899 stopped a linked folder's subdirectories from collapsing, and same-named documents no longer overwrite or vanish — but the reporter's actual workflow still could not rebuild his tree. He added one subtree at a time, and a browser folder pick reports each file's path relative to the chosen directory: `应用开发/钉钉CLI/` arrives as `钉钉CLI/...`, with no way to say it belongs under `应用开发/`. Every batch piled up at the KB root, and the ancestor was not missing data — it was never in the payload. Uploads now take an optional destination folder, composed ahead of each file's own relative path. The picker offers the KB's existing folders (and its root, the default) once files are staged, so batching a large tree is a matter of picking where each part goes. It runs through the same _sanitize_rel_subdir guard as a directory upload's own path, so a crafted value cannot escape raw/. Listing folders is best-effort: if that call fails the picker simply does not appear and the batch goes to the root exactly as before. Linked-folder sync keeps its own root-relative staging and does not take a destination prefix yet — that needs the prefix persisted per linked folder in metadata, which is a separate change.
This commit is contained in:
@@ -1168,6 +1168,80 @@ def test_upload_allows_same_filename_in_different_folders(monkeypatch, tmp_path:
|
||||
assert (manager.base_dir / "kb" / "raw" / "ModuleB" / "note.txt").is_file()
|
||||
|
||||
|
||||
def test_upload_places_a_batch_under_dest_subdir(monkeypatch, tmp_path: Path) -> None:
|
||||
"""A folder pick reports paths relative to the chosen directory, so its
|
||||
ancestors never reach the server. dest_subdir re-attaches the batch where
|
||||
it belongs instead of stacking every batch at the KB root (#866)."""
|
||||
manager = _ready_kb_manager(tmp_path)
|
||||
monkeypatch.setattr(knowledge_router_module, "get_kb_manager", lambda: manager)
|
||||
monkeypatch.setattr(knowledge_router_module, "_kb_base_dir", tmp_path / "knowledge_bases")
|
||||
|
||||
async def _noop_upload_task(*_args, **_kwargs):
|
||||
return None
|
||||
|
||||
monkeypatch.setattr(knowledge_router_module, "run_upload_processing_task", _noop_upload_task)
|
||||
|
||||
with TestClient(_build_app()) as client:
|
||||
response = client.post(
|
||||
"/api/v1/knowledge/kb/upload",
|
||||
files=[("files", ("README.txt", b"cli", "text/plain"))],
|
||||
data={
|
||||
"rel_paths": "DingTalkCLI/README.txt",
|
||||
"dest_subdir": "AppDev",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
raw = manager.base_dir / "kb" / "raw"
|
||||
assert (raw / "AppDev" / "DingTalkCLI" / "README.txt").is_file()
|
||||
assert not (raw / "DingTalkCLI").exists()
|
||||
|
||||
|
||||
def test_upload_dest_subdir_refuses_traversal(monkeypatch, tmp_path: Path) -> None:
|
||||
"""dest_subdir is caller-supplied, so it goes through the same guard as a
|
||||
directory upload's own relative path — it can never escape raw/."""
|
||||
manager = _ready_kb_manager(tmp_path)
|
||||
monkeypatch.setattr(knowledge_router_module, "get_kb_manager", lambda: manager)
|
||||
monkeypatch.setattr(knowledge_router_module, "_kb_base_dir", tmp_path / "knowledge_bases")
|
||||
|
||||
async def _noop_upload_task(*_args, **_kwargs):
|
||||
return None
|
||||
|
||||
monkeypatch.setattr(knowledge_router_module, "run_upload_processing_task", _noop_upload_task)
|
||||
|
||||
with TestClient(_build_app()) as client:
|
||||
response = client.post(
|
||||
"/api/v1/knowledge/kb/upload",
|
||||
files=[("files", ("note.txt", b"hi", "text/plain"))],
|
||||
data={"dest_subdir": "../../escaped"},
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
assert not (manager.base_dir.parent / "escaped").exists()
|
||||
|
||||
|
||||
def test_upload_without_dest_subdir_is_unchanged(monkeypatch, tmp_path: Path) -> None:
|
||||
"""The parameter is optional; omitting it keeps the previous placement."""
|
||||
manager = _ready_kb_manager(tmp_path)
|
||||
monkeypatch.setattr(knowledge_router_module, "get_kb_manager", lambda: manager)
|
||||
monkeypatch.setattr(knowledge_router_module, "_kb_base_dir", tmp_path / "knowledge_bases")
|
||||
|
||||
async def _noop_upload_task(*_args, **_kwargs):
|
||||
return None
|
||||
|
||||
monkeypatch.setattr(knowledge_router_module, "run_upload_processing_task", _noop_upload_task)
|
||||
|
||||
with TestClient(_build_app()) as client:
|
||||
response = client.post(
|
||||
"/api/v1/knowledge/kb/upload",
|
||||
files=[("files", ("note.txt", b"hi", "text/plain"))],
|
||||
data={"rel_paths": "Folder/note.txt"},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert (manager.base_dir / "kb" / "raw" / "Folder" / "note.txt").is_file()
|
||||
|
||||
|
||||
def test_move_file_into_folder(monkeypatch, tmp_path: Path) -> None:
|
||||
manager = _ready_kb_manager(tmp_path)
|
||||
raw = manager.base_dir / "kb" / "raw"
|
||||
|
||||
Reference in New Issue
Block a user