From 76968af0bab92147388d511487ba1d4d98df589e Mon Sep 17 00:00:00 2001 From: kpdev <156195510+kiannidev@users.noreply.github.com> Date: Tue, 2 Jun 2026 20:33:03 -0700 Subject: [PATCH] Guard missing storage blobs on preview and image endpoints (#15366) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes [#15365](https://github.com/infiniflow/ragflow/issues/15365) — `get_document_image()` and document preview call `make_response(None)` when storage returns no bytes, causing HTTP 500. --- api/apps/restful_apis/document_api.py | 2 ++ .../test_document_metadata.py | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/api/apps/restful_apis/document_api.py b/api/apps/restful_apis/document_api.py index cc4786cad..a639c5e7b 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 be852e514..a8821c0d2 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/") def test_get_image_success_and_exception_unit(self, document_app_module, monkeypatch):