Skip to content

Commit 945bc61

Browse files
authored
Fix group, channel and supergroup ChatPhoto downloads (pyrogram#327)
Closes #326
1 parent cedb87e commit 945bc61

File tree

4 files changed

+33
-8
lines changed

4 files changed

+33
-8
lines changed

pyrogram/client/client.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,6 +1221,7 @@ def download_worker(self):
12211221
access_hash=data.access_hash,
12221222
thumb_size=data.thumb_size,
12231223
peer_id=data.peer_id,
1224+
peer_type=data.peer_type,
12241225
peer_access_hash=data.peer_access_hash,
12251226
volume_id=data.volume_id,
12261227
local_id=data.local_id,
@@ -1866,6 +1867,7 @@ def get_file(
18661867
access_hash: int,
18671868
thumb_size: str,
18681869
peer_id: int,
1870+
peer_type: str,
18691871
peer_access_hash: int,
18701872
volume_id: int,
18711873
local_id: int,
@@ -1913,11 +1915,23 @@ def get_file(
19131915
file_ref = utils.decode_file_ref(file_ref)
19141916

19151917
if media_type == 1:
1916-
location = types.InputPeerPhotoFileLocation(
1917-
peer=types.InputPeerUser(
1918+
if peer_type == "user":
1919+
peer = types.InputPeerUser(
19181920
user_id=peer_id,
19191921
access_hash=peer_access_hash
1920-
),
1922+
)
1923+
elif peer_type == "chat":
1924+
peer = types.InputPeerChat(
1925+
chat_id=peer_id
1926+
)
1927+
else:
1928+
peer = types.InputPeerChannel(
1929+
channel_id=peer_id,
1930+
access_hash=peer_access_hash
1931+
)
1932+
1933+
location = types.InputPeerPhotoFileLocation(
1934+
peer=peer,
19211935
volume_id=volume_id,
19221936
local_id=local_id,
19231937
big=is_big or None

pyrogram/client/ext/file_data.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,17 @@
2020
class FileData:
2121
def __init__(
2222
self, *, media_type: int = None, dc_id: int = None, document_id: int = None, access_hash: int = None,
23-
thumb_size: str = None, peer_id: int = None, peer_access_hash: int = None, volume_id: int = None,
24-
local_id: int = None, is_big: bool = None, file_size: int = None, mime_type: str = None, file_name: str = None,
25-
date: int = None, file_ref: str = None
23+
thumb_size: str = None, peer_id: int = None, peer_type: str = None, peer_access_hash: int = None,
24+
volume_id: int = None, local_id: int = None, is_big: bool = None, file_size: int = None, mime_type: str = None,
25+
file_name: str = None, date: int = None, file_ref: str = None
2626
):
2727
self.media_type = media_type
2828
self.dc_id = dc_id
2929
self.document_id = document_id
3030
self.access_hash = access_hash
3131
self.thumb_size = thumb_size
3232
self.peer_id = peer_id
33+
self.peer_type = peer_type
3334
self.peer_access_hash = peer_access_hash
3435
self.volume_id = volume_id
3536
self.local_id = local_id

pyrogram/client/methods/messages/download_media.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,23 @@ def get_existing_attributes() -> dict:
146146

147147
if media_type == 1:
148148
unpacked = struct.unpack("<iiqqqiiiqi", decoded)
149-
dc_id, photo_id, _, volume_id, size_type, peer_id, _, peer_access_hash, local_id = unpacked[1:]
149+
dc_id, photo_id, _, volume_id, size_type, peer_id, x, peer_access_hash, local_id = unpacked[1:]
150+
151+
if x == 0:
152+
peer_type = "user"
153+
elif x == -1:
154+
peer_id = -peer_id
155+
peer_type = "chat"
156+
else:
157+
peer_id = utils.get_channel_id(peer_id - 1000727379968)
158+
peer_type = "channel"
150159

151160
data = FileData(
152161
**get_existing_attributes(),
153162
media_type=media_type,
154163
dc_id=dc_id,
155164
peer_id=peer_id,
165+
peer_type=peer_type,
156166
peer_access_hash=peer_access_hash,
157167
volume_id=volume_id,
158168
local_id=local_id,

pyrogram/client/types/user_and_chats/chat_photo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def _parse(client, chat_photo: types.UserProfilePhoto or types.ChatPhoto, peer_i
5555
if not isinstance(chat_photo, (types.UserProfilePhoto, types.ChatPhoto)):
5656
return None
5757

58-
if not peer_access_hash:
58+
if peer_access_hash is None:
5959
return None
6060

6161
photo_id = getattr(chat_photo, "photo_id", 0)

0 commit comments

Comments
 (0)