mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-07-04 09:39:32 +08:00
## Summary
MCP-wrapped agents could only force-download files looked up by
`doc_id`. This adds an explicit preview path and inline response headers
for previewable file types.
- **New** `GET /api/v1/agents/attachments/{attachment_id}/preview` —
inline preview for PDFs, images, and other safe types (pass `ext` and/or
`mime_type`)
- **Improved** `GET /api/v1/documents/{doc_id}/preview` — sets inline
disposition using the document filename
- **Improved** attachment download routing — resolves `mime_type` /
`ext` query params (no default `markdown`), supports
`disposition=inline`
- **DocGenerator output** — includes URL-encoded `preview_url` for MCP
clients
- **Legacy `/document/download/...` aliases** — still use download
semantics; MCP clients should call `/preview` explicitly
Fixes #15398
## Test plan
- [x] `pytest test/unit_test/api/utils/test_file_response_headers.py`
(6/6)
---------
Co-authored-by: MkDev11 <mkdev11@users.noreply.github.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Ling Qin <qinling0210@163.com>
65 lines
2.2 KiB
Python
65 lines
2.2 KiB
Python
#
|
|
# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
|
|
#
|
|
|
|
from urllib.parse import urlencode
|
|
|
|
import pytest
|
|
|
|
from api.utils import file_response as module
|
|
|
|
|
|
class _DummyHeaders(dict):
|
|
def set(self, key, value):
|
|
self[key] = value
|
|
|
|
|
|
class _DummyResponse:
|
|
def __init__(self):
|
|
self.headers = _DummyHeaders()
|
|
|
|
|
|
@pytest.mark.p2
|
|
def test_apply_preview_sets_inline_for_pdf():
|
|
response = _DummyResponse()
|
|
module.apply_preview_file_response_headers(response, "application/pdf", "pdf", "report.pdf")
|
|
assert response.headers["Content-Type"] == "application/pdf"
|
|
assert response.headers["Content-Disposition"] == 'inline; filename="report.pdf"'
|
|
|
|
|
|
@pytest.mark.p2
|
|
def test_apply_preview_forces_attachment_for_html():
|
|
response = _DummyResponse()
|
|
module.apply_preview_file_response_headers(response, "text/html", "html", "page.html")
|
|
assert response.headers["Content-Disposition"] == "attachment"
|
|
assert response.headers["X-Content-Type-Options"] == "nosniff"
|
|
|
|
|
|
@pytest.mark.p2
|
|
def test_apply_download_sets_attachment_for_pdf():
|
|
response = _DummyResponse()
|
|
module.apply_download_file_response_headers(response, "application/pdf", "pdf", "report.pdf")
|
|
assert response.headers["Content-Disposition"] == 'attachment; filename="report.pdf"'
|
|
|
|
|
|
@pytest.mark.p2
|
|
def test_resolve_attachment_content_type_prefers_mime_type():
|
|
content_type, ext = module.resolve_attachment_content_type(ext="md", mime_type="application/pdf")
|
|
assert content_type == "application/pdf"
|
|
assert ext == "pdf"
|
|
|
|
|
|
@pytest.mark.p2
|
|
def test_agent_attachment_preview_path_includes_query():
|
|
path = module.agent_attachment_preview_path("doc-1", ext="pdf", mime_type="application/pdf")
|
|
query = urlencode({"ext": "pdf", "mime_type": "application/pdf"})
|
|
assert path == f"/api/v1/agents/attachments/doc-1/preview?{query}"
|
|
|
|
|
|
@pytest.mark.p2
|
|
def test_agent_attachment_preview_path_encodes_svg_mime_type():
|
|
path = module.agent_attachment_preview_path("doc-1", ext="svg", mime_type="image/svg+xml")
|
|
query = urlencode({"ext": "svg", "mime_type": "image/svg+xml"})
|
|
assert path == f"/api/v1/agents/attachments/doc-1/preview?{query}"
|
|
assert "%2B" in path or "svg%2Bxml" in path
|