|
25 | 25 | import re |
26 | 26 | import shutil |
27 | 27 | import sys |
28 | | -import tempfile |
29 | 28 | from concurrent.futures.thread import ThreadPoolExecutor |
30 | 29 | from hashlib import sha256 |
31 | 30 | from importlib import import_module |
@@ -726,32 +725,29 @@ def load_plugins(self): |
726 | 725 |
|
727 | 726 | async def handle_download(self, packet): |
728 | 727 | file_id, directory, file_name, in_memory, file_size, progress, progress_args = packet |
729 | | - file = BytesIO() if in_memory else tempfile.NamedTemporaryFile("wb", delete=False) |
730 | 728 |
|
| 729 | + os.makedirs(directory, exist_ok=True) |
| 730 | + temp_file_path = os.path.abspath(re.sub("\\\\", "/", os.path.join(directory, file_name))) + ".temp" |
| 731 | + file = BytesIO() if in_memory else open(temp_file_path, "wb") |
| 732 | + |
| 733 | + # noinspection PyBroadException |
731 | 734 | try: |
732 | 735 | async for chunk in self.get_file(file_id, file_size, 0, 0, progress, progress_args): |
733 | 736 | file.write(chunk) |
734 | | - except pyrogram.StopTransmission: |
| 737 | + except BaseException: |
735 | 738 | if not in_memory: |
736 | 739 | file.close() |
737 | | - os.remove(file.name) |
| 740 | + os.remove(temp_file_path) |
738 | 741 |
|
739 | 742 | return None |
740 | | - except asyncio.CancelledError: |
741 | | - if not in_memory: |
742 | | - file.close() |
743 | | - os.remove(file.name) |
744 | | - |
745 | | - raise asyncio.CancelledError() |
746 | 743 | else: |
747 | 744 | if in_memory: |
748 | 745 | file.name = file_name |
749 | 746 | return file |
750 | 747 | else: |
751 | | - file_path = os.path.abspath(re.sub("\\\\", "/", os.path.join(directory, file_name))) |
752 | | - os.makedirs(directory, exist_ok=True) |
753 | 748 | file.close() |
754 | | - shutil.move(file.name, file_path) |
| 749 | + file_path = os.path.splitext(temp_file_path)[0] |
| 750 | + shutil.move(temp_file_path, file_path) |
755 | 751 | return file_path |
756 | 752 |
|
757 | 753 | async def get_file( |
|
0 commit comments