Skip to content

fix(api): infer /documents/{id}/download Content-Type from filename when ext is omitted (#15052) - #15053

Merged
KevinHuSh merged 1 commit into
infiniflow:mainfrom
kiannidev:fix/15052-download-content-type
May 21, 2026
Merged

KevinHuSh merged 1 commit into
infiniflow:mainfrom
kiannidev:fix/15052-download-content-type

Conversation

@kiannidev

@kiannidev kiannidev commented May 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Align GET /api/v1/documents/<doc_id>/download with /preview: resolve extension and MIME type from the stored document name when the ext query parameter is omitted, instead of defaulting to markdown.
  • When ?ext= is present, behavior stays the same as before (explicit extension / Content-Type mapping).
  • Enforce the same access + document lookup pattern as preview (accessible + get_by_id).
  • Extend unit tests for the no-ext PDF filename case.

Test plan

  • uv run pytest test/testcases/test_web_api/test_document_app/test_document_metadata.py::TestDocumentMetadataUnit::test_download_attachment_success_and_exception_unit
  • Optional: curl -sSI against /api/v1/documents/<pdf_doc_id>/download without ext and confirm Content-Type: application/pdf

Fixes #15052.

…tted

Align GET /documents/<id>/download with /preview: require access + load
document, then derive extension and MIME from doc.name if the `ext` query
parameter is absent. Keeps explicit ?ext= behavior for backward compatibility.

Fixes infiniflow#15052.
@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label May 20, 2026
@coderabbitai

coderabbitai Bot commented May 20, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The download endpoint now gates access via DocumentService.accessible() authorization check and removes the hardcoded ext="markdown" default. When ext is omitted, content-type is inferred from the document's stored filename instead. Tests validate the authorization path and the filename-based content-type inference.

Changes

Document download authorization and content-type inference

Layer / File(s) Summary
Authorization guard and content-type inference in download endpoint
api/apps/restful_apis/document_api.py
Download endpoint adds authorization check via DocumentService.accessible(doc_id, user_id) before retrieval. Content-type derivation removes the hardcoded ext="markdown" default: uses query param ext when present, otherwise infers extension from doc.name and computes content_type using CONTENT_TYPE_MAP and fallback prefix. Storage read and response creation remain after the new derivation logic.
Test updates for authorization and content-type inference
test/testcases/test_web_api/test_document_app/test_document_metadata.py
Test stubs DocumentService.get_by_id to return a document with known filename. New sub-case verifies that omitting ext query parameter causes the endpoint to derive MIME type and extension from stored filename (e.g., Annual report.PDF → application/pdf).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • infiniflow/ragflow#14625: Both PRs add authorization gating with DocumentService.accessible(doc_id, current_user.id) to the download endpoint.
  • infiniflow/ragflow#14927: Both PRs modify the same download_attachment endpoint in api/apps/restful_apis/document_api.py.

Suggested labels

🐞 bug, size:M

Suggested reviewers

  • wangq8

Poem

🐇 A rabbit hops through the docs with care,
No more defaulting to markdown everywhere!
When ext is missing, the filename appears—
Content-Type wisdom that calms all fears. 📋✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: inferring Content-Type from filename when ext is omitted for the /download endpoint.
Linked Issues check ✅ Passed The PR correctly implements the requirements from #15052: removes markdown default, infers MIME from stored filename when ext is omitted, and aligns with preview endpoint behavior.
Out of Scope Changes check ✅ Passed All changes directly address #15052: API endpoint fix, authorization check alignment, and test updates for the no-ext case. No unrelated modifications.
Description check ✅ Passed The pull request description clearly explains the problem, solution, and testing approach, matching the template's required structure.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@KevinHuSh KevinHuSh added the ci Continue Integration label May 21, 2026
@KevinHuSh
KevinHuSh marked this pull request as draft May 21, 2026 05:06
@KevinHuSh
KevinHuSh marked this pull request as ready for review May 21, 2026 05:06
@codecov

codecov Bot commented May 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.12%. Comparing base (b28e134) to head (52945dc).
⚠️ Report is 13 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #15053   +/-   ##
=======================================
  Coverage   93.12%   93.12%           
=======================================
  Files          10       10           
  Lines         713      713           
  Branches      116      116           
=======================================
  Hits          664      664           
  Misses         29       29           
  Partials       20       20           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@KevinHuSh
KevinHuSh merged commit 889b5f0 into infiniflow:main May 21, 2026
1 check passed
JinHai-CN pushed a commit that referenced this pull request May 21, 2026
…hen ext is omitted (#15052) (#15053)

## Summary

- Align **GET `/api/v1/documents/<doc_id>/download`** with
**`/preview`**: resolve extension and MIME type from the stored document
name when the **`ext` query parameter is omitted**, instead of
defaulting to `markdown`.
- When **`?ext=`** is present, behavior stays the same as before
(explicit extension / `Content-Type` mapping).
- Enforce the same access + document lookup pattern as preview
(**`accessible`** + **`get_by_id`**).
- Extend unit tests for the no-`ext` PDF filename case.

## Test plan

- [x] `uv run pytest
test/testcases/test_web_api/test_document_app/test_document_metadata.py::TestDocumentMetadataUnit::test_download_attachment_success_and_exception_unit`
- [x] Optional: `curl -sSI` against
`/api/v1/documents/<pdf_doc_id>/download` without `ext` and confirm
`Content-Type: application/pdf`

Fixes #15052.
wangq8 pushed a commit that referenced this pull request May 22, 2026
geversite pushed a commit to geversite/ragflow that referenced this pull request Jul 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: /documents/{doc_id}/download sets Content-Type from default ext=markdown when ext is omitted

2 participants