Skip to content

fix: return correct WebChat image MIME types#9319

Open
VectorPeak wants to merge 2 commits into
AstrBotDevs:masterfrom
VectorPeak:fix/webchat-image-mime-types
Open

fix: return correct WebChat image MIME types#9319
VectorPeak wants to merge 2 commits into
AstrBotDevs:masterfrom
VectorPeak:fix/webchat-image-mime-types

Conversation

@VectorPeak

@VectorPeak VectorPeak commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Fixes WebChat file responses so PNG, GIF, and WebP files are served with their real image MIME types instead of always using image/jpeg.

Modifications / 改动点

What Problem This Solves

ChatService.resolve_webchat_file() treated every supported WebChat image extension as JPEG:

if filename_ext[1:] in self.supported_imgs:
    return str(file_path), "image/jpeg"

That meant files such as photo.png, photo.gif, and photo.webp could be resolved successfully but then passed to FileResponse(media_type="image/jpeg"). The file bytes and filename stayed unchanged, but the HTTP Content-Type no longer matched the actual image format.

Changes

  • Centralized the WebChat image extension-to-MIME mapping as WEBCHAT_IMAGE_MIME_TYPES, then reused that same mapping for runtime lookup and test parametrization.
  • Kept the existing .wav handling, basename normalization, attachments directory lookup, WebChat fallback image directory lookup, and path boundary checks unchanged.
  • Added a focused regression test that initializes ChatService and verifies each supported image extension resolves to the expected MIME type.

Possible call chain / impact

GET /api/v1/files/content?filename=photo.png
  -> ChatService.resolve_webchat_file(filename)
    -> filename_ext = ".png"
    -> return (file_path, "image/png")
  -> FileResponse(file_path, media_type="image/png")

GET /api/chat/get_file?filename=photo.webp
  -> ChatService.resolve_webchat_file_from_dashboard_query(filename)
    -> ChatService.resolve_webchat_file(filename)
    -> return (file_path, "image/webp")
  -> FileResponse(file_path, media_type="image/webp")

This only changes the MIME header selected for WebChat image file responses. It does not change upload behavior, authentication, filename sanitization, path traversal protection, attachment database records, audio handling, or unknown-file fallback behavior.

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

Evidence

Before this change, a minimal local reproduction showed a .png file being resolved as JPEG:

('C:\\Users\\MiaoXing\\AppData\\Local\\Temp\\tmp8cdx8o_v\\demo.png', 'image/jpeg')

After this change, the regression test covers all supported WebChat image extensions:

[
    ("photo.jpg", "image/jpeg"),
    ("photo.jpeg", "image/jpeg"),
    ("photo.png", "image/png"),
    ("photo.gif", "image/gif"),
    ("photo.webp", "image/webp"),
]

Validation

uv run pytest tests/unit/test_webchat_upload_image_format.py -q
......                                                                   [100%]
6 passed, 1 warning in 3.53s
uv run ruff check astrbot/dashboard/services/chat_service.py tests/unit/test_webchat_upload_image_format.py
All checks passed!
uv run ruff format --check astrbot/dashboard/services/chat_service.py tests/unit/test_webchat_upload_image_format.py
2 files already formatted
git diff --check
# no whitespace errors

Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👖 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”

  • 🤸 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确认没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😷 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Correct WebChat file resolution to return accurate MIME types for supported image formats and cover the behavior with tests.

Bug Fixes:

  • Serve WebChat image attachments with MIME types that match their actual file extensions instead of always using image/jpeg.

Tests:

  • Add a regression test ensuring each supported WebChat image extension resolves to its correct MIME type.

Co-authored-by: chatgpt-codex-connector[bot] <199175422+chatgpt-codex-connector[bot]@users.noreply.github.com>
@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Jul 18, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the ChatService to map supported image extensions to their specific MIME types using a dictionary, replacing the previous list-based approach. This ensures that resolve_webchat_file returns the correct MIME type (such as image/png or image/gif) instead of defaulting to image/jpeg for all supported image formats. A new parameterized unit test has also been added to verify this behavior. I have no feedback to provide on these changes.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • To avoid extension/MIME mismatches in future edits, consider centralizing the supported_img_mime_types mapping (e.g., as a module-level constant) and reusing it in the test parametrization instead of duplicating the list of extensions and MIME types.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- To avoid extension/MIME mismatches in future edits, consider centralizing the `supported_img_mime_types` mapping (e.g., as a module-level constant) and reusing it in the test parametrization instead of duplicating the list of extensions and MIME types.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Co-authored-by: chatgpt-codex-connector[bot] <199175422+chatgpt-codex-connector[bot]@users.noreply.github.com>
@VectorPeak

Copy link
Copy Markdown
Contributor Author

Addressed the Sourcery feedback in a54203ac.

  • Moved the WebChat image extension-to-MIME mapping to the module-level WEBCHAT_IMAGE_MIME_TYPES constant.
  • Updated resolve_webchat_file() to use that shared mapping directly.
  • Updated the regression test parametrization to reuse the same mapping, so future supported-extension changes do not drift between runtime code and tests.

Validation after the update:

uv run pytest tests/unit/test_webchat_upload_image_format.py -q
6 passed, 1 warning in 3.38s
uv run ruff check astrbot/dashboard/services/chat_service.py tests/unit/test_webchat_upload_image_format.py
All checks passed!
uv run ruff format --check astrbot/dashboard/services/chat_service.py tests/unit/test_webchat_upload_image_format.py
2 files already formatted
git diff --check
# no whitespace errors

Remote GitHub checks are passing as well, including format-check, CodeQL, unit tests, dashboard build, Sourcery review, and the full smoke-test matrix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant