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

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.
This commit is contained in:
kpdev
2026-06-02 20:33:03 -07:00
committed by GitHub
parent ff5971448b
commit 76968af0ba
2 changed files with 20 additions and 0 deletions

View File

@@ -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())

View File

@@ -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):