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, 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 ), ], )