From 1e38c8f1fea66d388f68dfec73962c606e3f9ce7 Mon Sep 17 00:00:00 2001 From: Alisson Lauffer Date: Sun, 3 Dec 2023 23:51:51 -0300 Subject: [PATCH 1/2] fix(send-audio): fix a boolean instead of file name --- hydrogram/methods/messages/send_audio.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hydrogram/methods/messages/send_audio.py b/hydrogram/methods/messages/send_audio.py index d65747795..0f5c9667f 100644 --- a/hydrogram/methods/messages/send_audio.py +++ b/hydrogram/methods/messages/send_audio.py @@ -189,7 +189,7 @@ async def progress(current, total): duration=duration, performer=performer, title=title ), raw.types.DocumentAttributeFilename( - file_name=file_name or Path(audio).is_file() + file_name=file_name or Path(audio).name ), ], ) From a6c5630257bc94f9d01ece64281755c823d4946e Mon Sep 17 00:00:00 2001 From: Alisson Lauffer Date: Sun, 3 Dec 2023 23:53:01 -0300 Subject: [PATCH 2/2] fix(handle-download): prevent from closing BytesIO object --- hydrogram/client.py | 48 ++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/hydrogram/client.py b/hydrogram/client.py index d6000f270..1b5be4d61 100644 --- a/hydrogram/client.py +++ b/hydrogram/client.py @@ -855,31 +855,31 @@ async def handle_download(self, packet): None if in_memory else Path(directory).mkdir(parents=True, exist_ok=True) file_path = Path(directory).resolve() / file_name temp_file_path = file_path.with_suffix(".temp") - with BytesIO() if in_memory else Path(temp_file_path).open("wb") as file: - try: - async for chunk in self.get_file( - file_id, file_size, 0, 0, progress, progress_args - ): - file.write(chunk) - except BaseException as e: - if not in_memory: - file.close() - Path(temp_file_path).unlink() - - if isinstance(e, asyncio.CancelledError): - raise e - - if isinstance(e, hydrogram.errors.FloodWait): - raise e - - return None - else: - if in_memory: - file.name = file_name - return file + + file = BytesIO() if in_memory else Path(temp_file_path).open("wb") # noqa: SIM115 file is closed manually + + try: + async for chunk in self.get_file(file_id, file_size, 0, 0, progress, progress_args): + file.write(chunk) + except BaseException as e: + if not in_memory: file.close() - shutil.move(temp_file_path, file_path) - return file_path + Path(temp_file_path).unlink() + + if isinstance(e, asyncio.CancelledError): + raise e + + if isinstance(e, hydrogram.errors.FloodWait): + raise e + + return None + else: + if in_memory: + file.name = file_name + return file + file.close() + shutil.move(temp_file_path, file_path) + return file_path async def get_file( self,