fix(api): set SDK document download Content-Type from filename (#15112) (#15113)

## Summary

- Infer `Content-Type` from the stored document filename on SDK download
routes.
- Covers `GET /api/v1/datasets/<dataset_id>/documents/<document_id>` and
`GET /api/v1/documents/<document_id>`.
- Aligns with REST preview/download via `CONTENT_TYPE_MAP`.

## Test plan

- [x] `pytest
test/testcases/test_http_api/test_file_management_within_dataset/test_doc_sdk_routes_unit.py::TestDocRoutesUnit::test_download_mimetype_from_filename`
- [x] Manual: `curl -sSI` on SDK dataset document download for a PDF;
expect `Content-Type: application/pdf`

Fixes #15112.
This commit is contained in:
kpdev
2026-06-04 19:08:53 -07:00
committed by GitHub
parent 794c1f4b25
commit bd49fd70aa
4 changed files with 60 additions and 11 deletions

View File

@@ -1956,6 +1956,16 @@ async def get(doc_id):
except Exception as e:
return server_error_response(e)
def _mimetype_for_document(doc) -> str:
match = re.search(r"\.([^.]+)$", (doc.name or "").lower())
if not match:
return "application/octet-stream"
ext = match.group(1)
fallback_prefix = "image" if doc.type == FileType.VISUAL.value else "application"
return CONTENT_TYPE_MAP.get(ext, f"{fallback_prefix}/{ext}")
@manager.route("/datasets/<dataset_id>/documents/<document_id>", methods=["GET"]) # noqa: F821
@login_required
async def download(dataset_id, document_id):
@@ -2010,7 +2020,7 @@ async def download(dataset_id, document_id):
file,
as_attachment=True,
attachment_filename=doc[0].name,
mimetype="application/octet-stream", # Set a default MIME type
mimetype=_mimetype_for_document(doc[0]),
)
@manager.route("/documents/<document_id>", methods=["GET"]) # noqa: F821
@@ -2067,5 +2077,5 @@ async def download_document(document_id):
file,
as_attachment=True,
attachment_filename=doc[0].name,
mimetype="application/octet-stream", # Set a default MIME type
mimetype=_mimetype_for_document(doc[0]),
)