Fix link file to dataset check the duplicate name (#846) (#16975)

This commit is contained in:
Wang Qi
2026-07-16 16:03:27 +08:00
committed by GitHub
parent 8bb2cd0fb7
commit d8a6cacd0a
3 changed files with 51 additions and 3 deletions

View File

@@ -21,6 +21,7 @@ from pathlib import Path
from quart import request
from api.common.check_team_permission import check_file_team_permission, check_kb_team_permission
from api.db.services import duplicate_name
from api.db.services.file2document_service import File2DocumentService
from api.db.services.file_service import FileService
@@ -70,17 +71,18 @@ def _convert_files(file_ids, kb_ids, user_id, mode):
e, kb = KnowledgebaseService.get_by_id(kb_id)
if not e:
continue
filename = duplicate_name(DocumentService.query, name=file.name, kb_id=kb.id)
doc = DocumentService.insert(
{
"id": get_uuid(),
"kb_id": kb.id,
"parser_id": FileService.get_parser(file.type, file.name, kb.parser_id),
"parser_id": FileService.get_parser(file.type, filename, kb.parser_id),
"pipeline_id": kb.pipeline_id,
"parser_config": kb.parser_config,
"created_by": user_id,
"type": file.type,
"name": file.name,
"suffix": Path(file.name).suffix.lstrip("."),
"name": filename,
"suffix": Path(filename).suffix.lstrip("."),
"location": file.location,
"size": file.size,
}

View File

@@ -443,6 +443,7 @@ def _load_file2document_module(monkeypatch):
services_pkg = ModuleType("api.db.services")
services_pkg.__path__ = []
services_pkg.duplicate_name = lambda _query_func, **kwargs: "file(1).txt" if kwargs.get("name") == "file.txt" and kwargs.get("kb_id") == "kb-dup" else kwargs.get("name")
monkeypatch.setitem(sys.modules, "api.db.services", services_pkg)
common_pkg = ModuleType("api.common")
@@ -527,6 +528,10 @@ def _load_file2document_module(monkeypatch):
def remove_document(*_args, **_kwargs):
return True
@staticmethod
def query(*_args, **_kwargs):
return False
@staticmethod
def insert(_payload):
return SimpleNamespace(id="doc-1")
@@ -684,6 +689,24 @@ def test_convert_files_mode_add_and_replace_unit(monkeypatch):
assert deleted_file_links == ["f1", "f2"]
@pytest.mark.p2
def test_convert_files_renames_duplicate_document_name_unit(monkeypatch):
module = _load_file2document_module(monkeypatch)
inserted_docs = []
file = _DummyFile("f1", module.FileType.DOC.value, name="file.txt")
kb = SimpleNamespace(id="kb-dup", parser_id="naive", pipeline_id="p1", parser_config={})
monkeypatch.setattr(module.FileService, "get_by_id", lambda _file_id: (True, file))
monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (True, kb))
monkeypatch.setattr(module.DocumentService, "insert", lambda payload: inserted_docs.append(payload) or SimpleNamespace(id="doc-1"))
module._convert_files(["f1"], ["kb-dup"], "user-1", "add")
assert inserted_docs[0]["name"] == "file(1).txt"
assert inserted_docs[0]["suffix"] == "txt"
assert inserted_docs[0]["location"] == "loc"
def _load_file_api_service(monkeypatch):
LOGGER.debug("_load_file_api_service: entry")
repo_root = Path(__file__).resolve().parents[3]

View File

@@ -100,6 +100,7 @@ def _load_file2document_module(monkeypatch):
services_pkg = ModuleType("api.db.services")
services_pkg.__path__ = []
services_pkg.duplicate_name = lambda _query_func, **kwargs: "file(1).txt" if kwargs.get("name") == "file.txt" and kwargs.get("kb_id") == "kb-dup" else kwargs.get("name")
monkeypatch.setitem(sys.modules, "api.db.services", services_pkg)
common_pkg = ModuleType("api.common")
@@ -184,6 +185,10 @@ def _load_file2document_module(monkeypatch):
def remove_document(*_args, **_kwargs):
return True
@staticmethod
def query(*_args, **_kwargs):
return False
@staticmethod
def insert(_payload):
return SimpleNamespace(id="doc-1")
@@ -300,3 +305,21 @@ def test_convert_branch_matrix_unit(monkeypatch):
res = _run(module.convert())
assert res["code"] == 500
assert "convert boom" in res["message"]
@pytest.mark.p2
def test_convert_files_renames_duplicate_document_name_unit(monkeypatch):
module = _load_file2document_module(monkeypatch)
inserted_docs = []
file = _DummyFile("f1", module.FileType.DOC.value, name="file.txt")
kb = SimpleNamespace(id="kb-dup", parser_id="naive", pipeline_id="p1", parser_config={})
monkeypatch.setattr(module.FileService, "get_by_id", lambda _file_id: (True, file))
monkeypatch.setattr(module.KnowledgebaseService, "get_by_id", lambda _kb_id: (True, kb))
monkeypatch.setattr(module.DocumentService, "insert", lambda payload: inserted_docs.append(payload) or SimpleNamespace(id="doc-1"))
module._convert_files(["f1"], ["kb-dup"], "user-1", "add")
assert inserted_docs[0]["name"] == "file(1).txt"
assert inserted_docs[0]["suffix"] == "txt"
assert inserted_docs[0]["location"] == "loc"