From 49d408234d04977ae86bd9bdebd86ee3d65b6cff Mon Sep 17 00:00:00 2001 From: depostnykh <78929155+depostnykh@users.noreply.github.com> Date: Fri, 2 Feb 2024 14:42:58 +0300 Subject: [PATCH] fix: Change attachment voice extension (#446) --- pybotx/models/attachments.py | 11 ++++++++++- pyproject.toml | 2 +- tests/test_attachments.py | 19 ++++++++++++++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/pybotx/models/attachments.py b/pybotx/models/attachments.py index 1a92488f..899fd9ce 100644 --- a/pybotx/models/attachments.py +++ b/pybotx/models/attachments.py @@ -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, @@ -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: diff --git a/pyproject.toml b/pyproject.toml index f49361e1..dd1b0e55 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 ", diff --git a/tests/test_attachments.py b/tests/test_attachments.py index f3fd15a3..cf56dfce 100644 --- a/tests/test_attachments.py +++ b/tests/test_attachments.py @@ -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", @@ -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, + ), + ), )