Skip to content

fix(api): preserve inline image mime type on /file-preview (#40479) - #41266

Open
Taranum01 wants to merge 2 commits into
langgenius:mainfrom
Taranum01:fix/40479-image-preview-mime
Open

fix(api): preserve inline image mime type on /file-preview (#40479)#41266
Taranum01 wants to merge 2 commits into
langgenius:mainfrom
Taranum01:fix/40479-image-preview-mime

Conversation

@Taranum01

Copy link
Copy Markdown
Contributor

Closes #40479

Problem

/files/{id}/file-preview was forcing Content-Type: application/octet-stream on every response — including the inline previews that the logs UI uses to render user-uploaded and generated images. The browser received a generic binary blob, so every image preview in the logs (file uploads and generated outputs) showed up as a broken icon.

The intended behavior:

  • inline preview (as_attachment=False) → keep the upload's real mime type so the browser can render images, audio, video inline
  • download (as_attachment=True) → force application/octet-stream so the browser treats it as a file download

The previous code forced octet-stream unconditionally.

Fix

In api/controllers/files/image_preview.py, only override Content-Type when as_attachment=True. Inline previews now preserve upload_file.mime_type (image/png, image/jpeg, audio/mpeg, video/mp4, application/pdf, ...).

This matches the behavior described in PR #40397 (which was closed without merging) for the inline-preview side of the fix.

Verification

$ pytest api/tests/unit_tests/controllers/files/test_image_preview.py
9 passed

New regression fixture test_inline_image_preview_preserves_image_mime asserts the exact image/png case from the bug report. Updated test_basic_stream to assert inline previews no longer force application/octet-stream while test_as_attachment still verifies the download path keeps forcing it.

@dosubot dosubot Bot added the size:XS This PR changes 0-9 lines, ignoring generated files. label Aug 25, 2026
…s#40479)

Rebased onto upstream/main (HEAD c7520d7). Conflicts resolved:
- api/controllers/files/image_preview.py: kept upstream's _is_svg_content()
  + SVG-specific Content-Type/X-Content-Type-Options handling, AND kept
  my fix that only forces application/octet-stream when as_attachment=True
  (or is_svg=True, per upstream's broader condition). Inline previews still
  preserve the upload's real mime type so image/audio/video render in the
  logs UI.
- api/tests/unit_tests/controllers/files/test_image_preview.py: kept
  upstream's new test_inline_preview_uses_upload_file_mimetype() (which
  uses _upload_file() helper for a real PDF file with size=100) and
  relaxed my assertions to expect application/pdf rather than text/plain.
  Added my image/png regression fixture as an additional test case.

Issue langgenius#40479 is fixed: image previews no longer force octet-stream for
inline (as_attachment=False) responses.
@Taranum01
Taranum01 force-pushed the fix/40479-image-preview-mime branch from b84f734 to b6cd588 Compare August 25, 2026 20:23
@github-actions

github-actions Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 61.14% 61.13% -0.00%
Strict coverage 60.73% 60.73% -0.00%
Typed symbols 41,940 41,940 0
Untyped symbols 26,838 26,841 +3
Modules 3248 3248 0

@github-actions

github-actions Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-08-25 20:35:58.208155150 +0000
+++ /tmp/pyrefly_pr.txt	2026-08-25 20:35:48.386046645 +0000
@@ -3529,13 +3529,15 @@
 ERROR `SimpleNamespace` is not assignable to attribute `request` with type `Request` [bad-assignment]
    --> tests/unit_tests/controllers/files/test_image_preview.py:99:26
 ERROR `SimpleNamespace` is not assignable to attribute `request` with type `Request` [bad-assignment]
-   --> tests/unit_tests/controllers/files/test_image_preview.py:143:26
+   --> tests/unit_tests/controllers/files/test_image_preview.py:145:26
 ERROR `SimpleNamespace` is not assignable to attribute `request` with type `Request` [bad-assignment]
-   --> tests/unit_tests/controllers/files/test_image_preview.py:176:26
+   --> tests/unit_tests/controllers/files/test_image_preview.py:183:26
 ERROR `SimpleNamespace` is not assignable to attribute `request` with type `Request` [bad-assignment]
-   --> tests/unit_tests/controllers/files/test_image_preview.py:210:26
+   --> tests/unit_tests/controllers/files/test_image_preview.py:216:26
 ERROR `SimpleNamespace` is not assignable to attribute `request` with type `Request` [bad-assignment]
-   --> tests/unit_tests/controllers/files/test_image_preview.py:242:26
+   --> tests/unit_tests/controllers/files/test_image_preview.py:250:26
+ERROR `SimpleNamespace` is not assignable to attribute `request` with type `Request` [bad-assignment]
+   --> tests/unit_tests/controllers/files/test_image_preview.py:282:26
 ERROR No attribute `global_db` in module `controllers.files.tool_files` [missing-attribute]
   --> tests/unit_tests/controllers/files/test_tool_files.py:25:5
 ERROR `SimpleNamespace` is not assignable to attribute `request` with type `Request` [bad-assignment]

…yUploadFile (langgenius#40479)

Per Pyrefly type-check feedback on langgenius#41266: the regression test I added
referenced a DummyUploadFile class that doesn't exist in this module —
the upstream test file uses an _upload_file() helper instead. Switched
the call site to match.

The remaining Pyrefly errors (Request type vs SimpleNamespace) are
pre-existing in the test module and not introduced by this PR.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

In the logs, neither the user‑uploaded images nor the generated images can be previewed; all are broken.

1 participant