|
17 | 17 | # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. |
18 | 18 |
|
19 | 19 | import base64 |
20 | | -import binascii |
21 | 20 | import json |
22 | 21 | import logging |
23 | 22 | import math |
24 | 23 | import mimetypes |
25 | 24 | import os |
26 | 25 | import re |
27 | 26 | import shutil |
28 | | -import struct |
29 | 27 | import tempfile |
30 | 28 | import threading |
31 | 29 | import time |
|
49 | 47 | PhoneNumberUnoccupied, PhoneCodeInvalid, PhoneCodeHashEmpty, |
50 | 48 | PhoneCodeExpired, PhoneCodeEmpty, SessionPasswordNeeded, |
51 | 49 | PasswordHashInvalid, FloodWait, PeerIdInvalid, FirstnameInvalid, PhoneNumberBanned, |
52 | | - VolumeLocNotFound, UserMigrate, FileIdInvalid, ChannelPrivate, PhoneNumberOccupied, |
| 50 | + VolumeLocNotFound, UserMigrate, ChannelPrivate, PhoneNumberOccupied, |
53 | 51 | PasswordRecoveryNa, PasswordEmpty |
54 | 52 | ) |
55 | 53 | from pyrogram.session import Auth, Session |
@@ -829,85 +827,59 @@ def download_worker(self): |
829 | 827 | log.debug("{} started".format(name)) |
830 | 828 |
|
831 | 829 | while True: |
832 | | - media = self.download_queue.get() |
| 830 | + packet = self.download_queue.get() |
833 | 831 |
|
834 | | - if media is None: |
| 832 | + if packet is None: |
835 | 833 | break |
836 | 834 |
|
837 | 835 | temp_file_path = "" |
838 | 836 | final_file_path = "" |
839 | 837 |
|
840 | 838 | try: |
841 | | - media, file_name, done, progress, progress_args, path = media |
842 | | - |
843 | | - file_id = media.file_id |
844 | | - size = media.file_size |
| 839 | + data, file_name, done, progress, progress_args, path = packet |
| 840 | + data = data # type: BaseClient.FileData |
845 | 841 |
|
846 | 842 | directory, file_name = os.path.split(file_name) |
847 | 843 | directory = directory or "downloads" |
848 | 844 |
|
849 | | - try: |
850 | | - decoded = utils.decode(file_id) |
851 | | - fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq" |
852 | | - unpacked = struct.unpack(fmt, decoded) |
853 | | - except (AssertionError, binascii.Error, struct.error): |
854 | | - raise FileIdInvalid from None |
855 | | - else: |
856 | | - media_type = unpacked[0] |
857 | | - dc_id = unpacked[1] |
858 | | - id = unpacked[2] |
859 | | - access_hash = unpacked[3] |
860 | | - volume_id = None |
861 | | - secret = None |
862 | | - local_id = None |
863 | | - |
864 | | - if len(decoded) > 24: |
865 | | - volume_id = unpacked[4] |
866 | | - secret = unpacked[5] |
867 | | - local_id = unpacked[6] |
868 | | - |
869 | | - media_type_str = Client.MEDIA_TYPE_ID.get(media_type, None) |
| 845 | + media_type_str = Client.MEDIA_TYPE_ID[data.media_type] |
870 | 846 |
|
871 | | - if media_type_str is None: |
872 | | - raise FileIdInvalid("Unknown media type: {}".format(unpacked[0])) |
| 847 | + if not data.file_name: |
| 848 | + guessed_extension = self.guess_extension(data.mime_type) |
873 | 849 |
|
874 | | - file_name = file_name or getattr(media, "file_name", None) |
875 | | - |
876 | | - if not file_name: |
877 | | - guessed_extension = self.guess_extension(media.mime_type) |
878 | | - |
879 | | - if media_type in (0, 1, 2): |
| 850 | + if data.media_type in (0, 1, 2, 14): |
880 | 851 | extension = ".jpg" |
881 | | - elif media_type == 3: |
| 852 | + elif data.media_type == 3: |
882 | 853 | extension = guessed_extension or ".ogg" |
883 | | - elif media_type in (4, 10, 13): |
| 854 | + elif data.media_type in (4, 10, 13): |
884 | 855 | extension = guessed_extension or ".mp4" |
885 | | - elif media_type == 5: |
| 856 | + elif data.media_type == 5: |
886 | 857 | extension = guessed_extension or ".zip" |
887 | | - elif media_type == 8: |
| 858 | + elif data.media_type == 8: |
888 | 859 | extension = guessed_extension or ".webp" |
889 | | - elif media_type == 9: |
| 860 | + elif data.media_type == 9: |
890 | 861 | extension = guessed_extension or ".mp3" |
891 | 862 | else: |
892 | 863 | continue |
893 | 864 |
|
894 | 865 | file_name = "{}_{}_{}{}".format( |
895 | 866 | media_type_str, |
896 | | - datetime.fromtimestamp( |
897 | | - getattr(media, "date", None) or time.time() |
898 | | - ).strftime("%Y-%m-%d_%H-%M-%S"), |
| 867 | + datetime.fromtimestamp(data.date or time.time()).strftime("%Y-%m-%d_%H-%M-%S"), |
899 | 868 | self.rnd_id(), |
900 | 869 | extension |
901 | 870 | ) |
902 | 871 |
|
903 | 872 | temp_file_path = self.get_file( |
904 | | - dc_id=dc_id, |
905 | | - id=id, |
906 | | - access_hash=access_hash, |
907 | | - volume_id=volume_id, |
908 | | - local_id=local_id, |
909 | | - secret=secret, |
910 | | - size=size, |
| 873 | + media_type=data.media_type, |
| 874 | + dc_id=data.dc_id, |
| 875 | + file_id=data.file_id, |
| 876 | + access_hash=data.access_hash, |
| 877 | + thumb_size=data.thumb_size, |
| 878 | + peer_id=data.peer_id, |
| 879 | + volume_id=data.volume_id, |
| 880 | + local_id=data.local_id, |
| 881 | + file_size=data.file_size, |
| 882 | + is_big=data.is_big, |
911 | 883 | progress=progress, |
912 | 884 | progress_args=progress_args |
913 | 885 | ) |
@@ -1549,16 +1521,21 @@ def save_file( |
1549 | 1521 | finally: |
1550 | 1522 | session.stop() |
1551 | 1523 |
|
1552 | | - def get_file(self, |
1553 | | - dc_id: int, |
1554 | | - id: int = None, |
1555 | | - access_hash: int = None, |
1556 | | - volume_id: int = None, |
1557 | | - local_id: int = None, |
1558 | | - secret: int = None, |
1559 | | - size: int = None, |
1560 | | - progress: callable = None, |
1561 | | - progress_args: tuple = ()) -> str: |
| 1524 | + def get_file( |
| 1525 | + self, |
| 1526 | + media_type: int, |
| 1527 | + dc_id: int, |
| 1528 | + file_id: int, |
| 1529 | + access_hash: int, |
| 1530 | + thumb_size: str, |
| 1531 | + peer_id: int, |
| 1532 | + volume_id: int, |
| 1533 | + local_id: int, |
| 1534 | + file_size: int, |
| 1535 | + is_big: bool, |
| 1536 | + progress: callable, |
| 1537 | + progress_args: tuple = () |
| 1538 | + ) -> str: |
1562 | 1539 | with self.media_sessions_lock: |
1563 | 1540 | session = self.media_sessions.get(dc_id, None) |
1564 | 1541 |
|
@@ -1599,18 +1576,33 @@ def get_file(self, |
1599 | 1576 |
|
1600 | 1577 | self.media_sessions[dc_id] = session |
1601 | 1578 |
|
1602 | | - if volume_id: # Photos are accessed by volume_id, local_id, secret |
1603 | | - location = types.InputFileLocation( |
| 1579 | + if media_type == 1: |
| 1580 | + location = types.InputPeerPhotoFileLocation( |
| 1581 | + peer=self.resolve_peer(peer_id), |
1604 | 1582 | volume_id=volume_id, |
1605 | 1583 | local_id=local_id, |
1606 | | - secret=secret, |
1607 | | - file_reference=b"" |
| 1584 | + big=is_big or None |
1608 | 1585 | ) |
1609 | | - else: # Any other file can be more easily accessed by id and access_hash |
| 1586 | + elif media_type in (0, 2): |
| 1587 | + location = types.InputPhotoFileLocation( |
| 1588 | + id=file_id, |
| 1589 | + access_hash=access_hash, |
| 1590 | + file_reference=b"", |
| 1591 | + thumb_size=thumb_size |
| 1592 | + ) |
| 1593 | + elif media_type == 14: |
| 1594 | + location = types.InputDocumentFileLocation( |
| 1595 | + id=file_id, |
| 1596 | + access_hash=access_hash, |
| 1597 | + file_reference=b"", |
| 1598 | + thumb_size=thumb_size |
| 1599 | + ) |
| 1600 | + else: |
1610 | 1601 | location = types.InputDocumentFileLocation( |
1611 | | - id=id, |
| 1602 | + id=file_id, |
1612 | 1603 | access_hash=access_hash, |
1613 | | - file_reference=b"" |
| 1604 | + file_reference=b"", |
| 1605 | + thumb_size="" |
1614 | 1606 | ) |
1615 | 1607 |
|
1616 | 1608 | limit = 1024 * 1024 |
@@ -1641,7 +1633,14 @@ def get_file(self, |
1641 | 1633 | offset += limit |
1642 | 1634 |
|
1643 | 1635 | if progress: |
1644 | | - progress(self, min(offset, size) if size != 0 else offset, size, *progress_args) |
| 1636 | + progress( |
| 1637 | + self, |
| 1638 | + min(offset, file_size) |
| 1639 | + if file_size != 0 |
| 1640 | + else offset, |
| 1641 | + file_size, |
| 1642 | + *progress_args |
| 1643 | + ) |
1645 | 1644 |
|
1646 | 1645 | r = session.send( |
1647 | 1646 | functions.upload.GetFile( |
@@ -1723,7 +1722,14 @@ def get_file(self, |
1723 | 1722 | offset += limit |
1724 | 1723 |
|
1725 | 1724 | if progress: |
1726 | | - progress(self, min(offset, size) if size != 0 else offset, size, *progress_args) |
| 1725 | + progress( |
| 1726 | + self, |
| 1727 | + min(offset, file_size) |
| 1728 | + if file_size != 0 |
| 1729 | + else offset, |
| 1730 | + file_size, |
| 1731 | + *progress_args |
| 1732 | + ) |
1727 | 1733 |
|
1728 | 1734 | if len(chunk) < limit: |
1729 | 1735 | break |
|
0 commit comments