Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

Guard missing storage blobs on preview and image endpoints (#15366)

kpdev76968af0bab9
diff --git a/api/apps/restful_apis/document_api.py b/api/apps/restful_apis/document_api.py
index cc4786cad6..a639c5e7b8 100644
--- a/api/apps/restful_apis/document_api.py
+++ b/api/apps/restful_apis/document_api.py
@@ -1924,6 +1924,8 @@ async def get(doc_id):
b, n = File2DocumentService.get_storage_address(doc_id=doc_id)
data = await thread_pool_exec(settings.STORAGE_IMPL.get, b, n)
+ if not data:
+ return get_data_error_result(message="This file is empty.")
response = await make_response(data)
ext = re.search(r"\.([^.]+)$", doc.name.lower())
diff --git a/test/testcases/test_web_api/test_document_app/test_document_metadata.py b/test/testcases/test_web_api/test_document_app/test_document_metadata.py
index be852e514b..a8821c0d24 100644
--- a/test/testcases/test_web_api/test_document_app/test_document_metadata.py
+++ b/test/testcases/test_web_api/test_document_app/test_document_metadata.py
@@ -526,6 +526,24 @@ class TestDocumentMetadataUnit:
assert res["code"] == RetCode.DATA_ERROR
assert res["message"] == "Image not found."
+ @pytest.mark.p2
+ def test_get_preview_missing_blob_unit(self, document_app_module, monkeypatch):
+ module = document_app_module
+
+ async def fake_thread_pool_exec(*_args, **_kwargs):
+ return None
+
+ monkeypatch.setattr(module.DocumentService, "accessible", lambda _doc_id, _user_id: True)
+ monkeypatch.setattr(
+ module.DocumentService,
+ "get_by_id",
+ lambda _doc_id: (True, SimpleNamespace(name="report.pdf", type=module.FileType.OTHER.value)),
+ )
+ monkeypatch.setattr(module.File2DocumentService, "get_storage_address", lambda **_kwargs: ("bucket", "name"))
+ monkeypatch.setattr(module, "thread_pool_exec", fake_thread_pool_exec)
+ res = _run(module.get("doc1"))
+ assert res["code"] == RetCode.DATA_ERROR
+ assert res["message"] == "This file is empty."
@pytest.mark.skip(reason="Moved to /api/v1/documents/images/<image_id>")
def test_get_image_success_and_exception_unit(self, document_app_module, monkeypatch):