From 6f86d15de44ca53eface2272934f97e53eb0c2dc Mon Sep 17 00:00:00 2001 From: Denis Date: Sun, 27 Jul 2025 02:12:58 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20filename=20=D0=BA=20InputMediaBuffer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- maxapi/connection/base.py | 4 ++-- maxapi/types/input_media.py | 4 +++- maxapi/utils/message.py | 2 ++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/maxapi/connection/base.py b/maxapi/connection/base.py index c604c68..0e25d30 100644 --- a/maxapi/connection/base.py +++ b/maxapi/connection/base.py @@ -4,7 +4,6 @@ import os import mimetypes from typing import TYPE_CHECKING, Any, Optional -from uuid import uuid4 import aiofiles import puremagic @@ -156,6 +155,7 @@ class BaseConnection: async def upload_file_buffer( self, + filename: str, url: str, buffer: bytes, type: UploadType @@ -182,7 +182,7 @@ class BaseConnection: mime_type = f"{type.value}/*" ext = '' - basename = f'{uuid4()}{ext}' + basename = f'{filename}{ext}' form = FormData() form.add_field( diff --git a/maxapi/types/input_media.py b/maxapi/types/input_media.py index 5ec9b26..df01e94 100644 --- a/maxapi/types/input_media.py +++ b/maxapi/types/input_media.py @@ -68,13 +68,15 @@ class InputMediaBuffer: type (UploadType): Тип файла, определенный по содержимому. """ - def __init__(self, buffer: bytes): + def __init__(self, buffer: bytes, filename: str | None = None): """ Инициализирует объект медиафайла из буфера. Args: buffer (IO): Буфер с содержимым файла. + filename (str): Название файла (по умолчанию присваивается uuid4). """ + self.filename = filename self.buffer = buffer self.type = self.__detect_file_type(buffer) diff --git a/maxapi/utils/message.py b/maxapi/utils/message.py index aa0de48..0fbe13f 100644 --- a/maxapi/utils/message.py +++ b/maxapi/utils/message.py @@ -2,6 +2,7 @@ from __future__ import annotations from typing import TYPE_CHECKING from json import loads +from uuid import uuid4 from ..types.input_media import InputMedia, InputMediaBuffer from ..enums.upload_type import UploadType @@ -46,6 +47,7 @@ async def process_input_media( ) elif isinstance(att, InputMediaBuffer): upload_file_response = await base_connection.upload_file_buffer( + filename=att.filename or str(uuid4()), url=upload.url, buffer=att.buffer, type=att.type,