mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-18 13:47:21 +08:00
Refa: migrate document preview/download to RESTful API (#14633)
### What problem does this PR solve? migrate document preview/download to RESTful API ### Type of change - [x] Refactoring
This commit is contained in:
@@ -26,7 +26,6 @@ from utils.file_utils import create_txt_file
|
||||
HEADERS = {"Content-Type": "application/json"}
|
||||
|
||||
DATASETS_URL = f"/api/{VERSION}/datasets"
|
||||
DOCUMENT_APP_URL = f"/{VERSION}/document"
|
||||
CHUNK_APP_URL = f"/{VERSION}/chunk"
|
||||
CHUNK_API_URL = f"/api/{VERSION}/datasets/{{dataset_id}}/documents/{{document_id}}/chunks"
|
||||
# SESSION_WITH_CHAT_ASSISTANT_API_URL = "/api/v1/chats/{chat_id}/sessions"
|
||||
@@ -404,10 +403,33 @@ def document_infos(auth, dataset_id, params=None, payload=None, *, headers=HEADE
|
||||
|
||||
|
||||
def document_metadata_summary(auth, payload=None, *, headers=HEADERS, data=None):
|
||||
res = requests.post(url=f"{HOST_ADDRESS}{DOCUMENT_APP_URL}/metadata/summary", headers=headers, auth=auth, json=payload, data=data)
|
||||
dataset_id = (payload or {}).get("kb_id")
|
||||
doc_ids = (payload or {}).get("doc_ids")
|
||||
if not dataset_id:
|
||||
return {"code": 101, "message": "KB ID is required"}
|
||||
params = {}
|
||||
if doc_ids:
|
||||
params["doc_ids"] = ",".join(doc_ids)
|
||||
res = requests.get(url=f"{HOST_ADDRESS}{DATASETS_URL}/{dataset_id}/metadata/summary", headers=headers, auth=auth, params=params, data=data)
|
||||
return res.json()
|
||||
|
||||
|
||||
def document_get(auth, document_id, *, headers=HEADERS, data=None):
|
||||
res = requests.get(url=f"{HOST_ADDRESS}/api/{VERSION}/documents/{document_id}/preview", headers=headers, auth=auth, data=data)
|
||||
return res
|
||||
|
||||
|
||||
def document_download(auth, attachment_id, *, ext="markdown", headers=HEADERS, data=None):
|
||||
res = requests.get(
|
||||
url=f"{HOST_ADDRESS}/api/{VERSION}/documents/{attachment_id}/download",
|
||||
headers=headers,
|
||||
auth=auth,
|
||||
params={"ext": ext},
|
||||
data=data,
|
||||
)
|
||||
return res
|
||||
|
||||
|
||||
def document_metadata_update(auth, dataset_id, payload=None, *, headers=HEADERS, data=None):
|
||||
"""New unified API for updating document metadata.
|
||||
|
||||
|
||||
@@ -126,11 +126,31 @@ def document_app_module(monkeypatch):
|
||||
monkeypatch.setitem(sys.modules, "xgboost", ModuleType("xgboost"))
|
||||
|
||||
stub_apps = ModuleType("api.apps")
|
||||
stub_apps.__path__ = [str(repo_root / "api" / "apps")]
|
||||
stub_apps.current_user = SimpleNamespace(id="user-1")
|
||||
stub_apps.login_required = lambda func: func
|
||||
monkeypatch.setitem(sys.modules, "api.apps", stub_apps)
|
||||
|
||||
module_path = repo_root / "api" / "apps" / "document_app.py"
|
||||
stub_apps_services = ModuleType("api.apps.services")
|
||||
stub_apps_services.__path__ = [str(repo_root / "api" / "apps" / "services")]
|
||||
monkeypatch.setitem(sys.modules, "api.apps.services", stub_apps_services)
|
||||
|
||||
document_api_service_mod = ModuleType("api.apps.services.document_api_service")
|
||||
document_api_service_mod.validate_document_update_fields = lambda *_args, **_kwargs: (None, None)
|
||||
document_api_service_mod.map_doc_keys = lambda doc: doc.to_dict() if hasattr(doc, "to_dict") else doc
|
||||
|
||||
def _map_doc_keys_with_run_status(doc, run_status="0"):
|
||||
payload = doc if isinstance(doc, dict) else doc.to_dict()
|
||||
return {**payload, "run": run_status}
|
||||
|
||||
document_api_service_mod.map_doc_keys_with_run_status = _map_doc_keys_with_run_status
|
||||
document_api_service_mod.update_document_name_only = lambda *_args, **_kwargs: None
|
||||
document_api_service_mod.update_chunk_method = lambda *_args, **_kwargs: None
|
||||
document_api_service_mod.update_document_status_only = lambda *_args, **_kwargs: None
|
||||
document_api_service_mod.reset_document_for_reparse = lambda *_args, **_kwargs: None
|
||||
monkeypatch.setitem(sys.modules, "api.apps.services.document_api_service", document_api_service_mod)
|
||||
|
||||
module_path = repo_root / "api" / "apps" / "restful_apis" / "document_api.py"
|
||||
spec = importlib.util.spec_from_file_location("test_document_app_unit", module_path)
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
module.manager = _DummyManager()
|
||||
|
||||
@@ -394,7 +394,7 @@ class TestDocumentMetadataUnit:
|
||||
"apply_safe_file_response_headers",
|
||||
lambda response, content_type, extension: response.headers.update({"content_type": content_type, "extension": extension}),
|
||||
)
|
||||
res = _run(module.download_attachment("att1"))
|
||||
res = _run(module.download_attachment(attachment_id="att1"))
|
||||
assert isinstance(res, _DummyResponse)
|
||||
assert res.data == b"attachment"
|
||||
assert res.headers["content_type"] == "application/abc"
|
||||
@@ -405,7 +405,7 @@ class TestDocumentMetadataUnit:
|
||||
|
||||
monkeypatch.setattr(module, "thread_pool_exec", raise_error)
|
||||
monkeypatch.setattr(module, "server_error_response", lambda e: {"code": 500, "message": str(e)})
|
||||
res = _run(module.download_attachment("att1"))
|
||||
res = _run(module.download_attachment(attachment_id="att1"))
|
||||
assert res["code"] == 500
|
||||
assert "download boom" in res["message"]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user