Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions astrbot/dashboard/services/chat_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,13 @@ def __init__(
self.webchat_img_dir = os.path.join(get_astrbot_data_path(), "webchat", "imgs")
os.makedirs(self.attachments_dir, exist_ok=True)

self.supported_imgs = ["jpg", "jpeg", "png", "gif", "webp"]
self.supported_img_mime_types = {
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".png": "image/png",
".gif": "image/gif",
".webp": "image/webp",
}
self.conv_mgr = core_lifecycle.conversation_manager
self.platform_history_mgr = core_lifecycle.platform_message_history_manager
self.umop_config_router = core_lifecycle.umop_config_router
Expand Down Expand Up @@ -557,8 +563,8 @@ async def resolve_webchat_file(
filename_ext = file_path.suffix.lower()
if filename_ext == ".wav":
return str(file_path), "audio/wav"
if filename_ext[1:] in self.supported_imgs:
return str(file_path), "image/jpeg"
if filename_ext in self.supported_img_mime_types:
return str(file_path), self.supported_img_mime_types[filename_ext]
return str(file_path), None

async def resolve_webchat_file_from_dashboard_query(
Expand Down
38 changes: 38 additions & 0 deletions tests/unit/test_webchat_upload_image_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,41 @@ async def insert_attachment(self, path, type, mime_type):
assert fake_db.inserted["type"] == "image"
assert (tmp_path / result["filename"]).exists()
assert not (tmp_path / "pasted.png").exists()


@pytest.mark.parametrize(
("filename", "expected_mime_type"),
[
("photo.jpg", "image/jpeg"),
("photo.jpeg", "image/jpeg"),
("photo.png", "image/png"),
("photo.gif", "image/gif"),
("photo.webp", "image/webp"),
],
)
@pytest.mark.asyncio
async def test_resolve_webchat_file_uses_image_extension_mime_type(
tmp_path,
monkeypatch,
filename,
expected_mime_type,
):
monkeypatch.setattr(
"astrbot.dashboard.services.chat_service.get_astrbot_data_path",
lambda: str(tmp_path),
)
service = ChatService(
SimpleNamespace(),
SimpleNamespace(
conversation_manager=None,
platform_message_history_manager=None,
umop_config_router=None,
),
)
file_path = tmp_path / "attachments" / filename
file_path.write_bytes(b"image-bytes")

resolved_path, mime_type = await service.resolve_webchat_file(filename)

assert resolved_path == str(file_path.resolve(strict=False))
assert mime_type == expected_mime_type
Loading