Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 7 additions & 4 deletions pybotx/models/attachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions tests/test_attachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
AttachmentVideo,
AttachmentVoice,
BotAPIAttachment,
BotXAPIAttachment,
Contact,
IncomingAttachment,
Link,
Location,
OutgoingAttachment,
convert_api_attachment_to_domain,
)

Expand Down Expand Up @@ -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 = (
(
{
Expand Down
Loading