Skip to content

Commit

Permalink
fix: Change attachment voice extension (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
depostnykh committed Feb 2, 2024
1 parent c57b791 commit 49d4082
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
11 changes: 10 additions & 1 deletion pybotx/models/attachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,13 @@ def convert_api_attachment_to_domain( # noqa: WPS212
attachment_type = cast(Literal[AttachmentTypes.VOICE], attachment_type)
api_attachment = cast(BotAPIAttachmentVoice, api_attachment)
content = decode_rfc2397(api_attachment.data.content)
attachment_extension = get_attachment_extension_from_encoded_content(
api_attachment.data.content,
)

return AttachmentVoice(
type=attachment_type,
filename="record.mp3",
filename=f"record.{attachment_extension}",
size=len(content),
is_async_file=False,
content=content,
Expand Down Expand Up @@ -317,6 +320,12 @@ def convert_api_attachment_to_domain( # noqa: WPS212
raise NotImplementedError(f"Unsupported attachment type: {attachment_type}")


def get_attachment_extension_from_encoded_content(
encoded_content: str,
) -> str:
return encoded_content.split(";")[0].split("/")[1]


def decode_rfc2397(encoded_content: str) -> bytes:
# "data:image/gif;base64,aGVsbG8=" -> b"hello"
if not encoded_content:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pybotx"
version = "0.62.0"
version = "0.62.1"
description = "A python library for interacting with eXpress BotX API"
authors = [
"Sidnev Nikolay <[email protected]>",
Expand Down
19 changes: 18 additions & 1 deletion tests/test_attachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ async def default_handler(message: IncomingMessage, bot: Bot) -> None:
(
{
"data": {
"content": "data:audio/mpeg3;base64,SGVsbG8sIHdvcmxkIQo=",
"content": "data:audio/mp3;base64,SGVsbG8sIHdvcmxkIQo=",
"duration": 10,
},
"type": "voice",
Expand All @@ -250,6 +250,23 @@ async def default_handler(message: IncomingMessage, bot: Bot) -> None:
duration=10,
),
),
(
{
"data": {
"content": "data:audio/m4a;base64,SGVsbG8sIHdvcmxkIQo=",
"duration": 10,
},
"type": "voice",
},
AttachmentVoice(
type=AttachmentTypes.VOICE,
filename="record.m4a",
size=len(b"Hello, world!\n"),
is_async_file=False,
content=b"Hello, world!\n",
duration=10,
),
),
)


Expand Down

0 comments on commit 49d4082

Please sign in to comment.