diff --git a/pybotx/models/attachments.py b/pybotx/models/attachments.py index 9882eca3..14cccce4 100644 --- a/pybotx/models/attachments.py +++ b/pybotx/models/attachments.py @@ -501,6 +501,12 @@ def encode_rfc2397(content: bytes, mimetype: str) -> str: return f"data:{mimetype};base64,{b64_content}" +def get_mimetype_by_filename(filename: str) -> str: + extension = filename.rsplit(".", 1)[-1].lower() + + return EXTENSIONS_TO_MIMETYPES.get(extension, DEFAULT_MIMETYPE) + + class BotXAPIAttachment(UnverifiedPayloadBaseModel): file_name: str data: str @@ -512,10 +518,7 @@ def from_file_attachment( ) -> "BotXAPIAttachment": assert attachment.content is not None - mimetype = EXTENSIONS_TO_MIMETYPES.get( - attachment.filename.split(".")[-1], - DEFAULT_MIMETYPE, - ) + mimetype = get_mimetype_by_filename(attachment.filename) return cls( file_name=attachment.filename, diff --git a/tests/test_attachments.py b/tests/test_attachments.py index a3a87b6d..f4066c80 100644 --- a/tests/test_attachments.py +++ b/tests/test_attachments.py @@ -21,10 +21,12 @@ AttachmentVideo, AttachmentVoice, BotAPIAttachment, + BotXAPIAttachment, Contact, IncomingAttachment, Link, Location, + OutgoingAttachment, convert_api_attachment_to_domain, ) @@ -81,6 +83,21 @@ async def default_handler(message: IncomingMessage, bot: Bot) -> None: assert read_content == b"Hello, world!\n" +async def test__botx_api_attachment__uppercase_file_extension_mimetype() -> None: + # - Arrange - + attachment = OutgoingAttachment( + content=b"Hello, world!", + filename="image.PNG", + ) + + # - Act - + api_attachment = BotXAPIAttachment.from_file_attachment(attachment) + + # - Assert - + assert api_attachment.file_name == "image.PNG" + assert api_attachment.data == "data:image/png;base64,SGVsbG8sIHdvcmxkIQ==" + + API_AND_DOMAIN_NON_FILE_ATTACHMENTS = ( ( {