Skip to content

Commit 92c1b48

Browse files
committed
Fix FILE_REFERENCE_* errors for uploads
1 parent 1cd9452 commit 92c1b48

31 files changed

+163
-69
lines changed

pyrogram/client/client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1869,7 +1869,7 @@ def get_file(
18691869
peer_access_hash: int,
18701870
volume_id: int,
18711871
local_id: int,
1872-
file_ref: bytes,
1872+
file_ref: str,
18731873
file_size: int,
18741874
is_big: bool,
18751875
progress: callable,
@@ -1910,6 +1910,8 @@ def get_file(
19101910

19111911
self.media_sessions[dc_id] = session
19121912

1913+
file_ref = utils.decode_file_ref(file_ref)
1914+
19131915
if media_type == 1:
19141916
location = types.InputPeerPhotoFileLocation(
19151917
peer=types.InputPeerUser(

pyrogram/client/ext/file_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(
2222
self, *, media_type: int = None, dc_id: int = None, document_id: int = None, access_hash: int = None,
2323
thumb_size: str = None, peer_id: int = None, peer_access_hash: int = None, volume_id: int = None,
2424
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: bytes = None
25+
date: int = None, file_ref: str = None
2626
):
2727
self.media_type = media_type
2828
self.dc_id = dc_id

pyrogram/client/ext/utils.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ def encode(s: bytes) -> str:
7070
return base64.urlsafe_b64encode(r).decode().rstrip("=")
7171

7272

73+
def encode_file_ref(file_ref: bytes) -> str:
74+
return base64.urlsafe_b64encode(file_ref).decode().rstrip("=")
75+
76+
77+
def decode_file_ref(file_ref: str) -> bytes:
78+
if file_ref is None:
79+
return b""
80+
81+
return base64.urlsafe_b64decode(file_ref + "=" * (-len(file_ref) % 4))
82+
83+
7384
def get_offset_date(dialogs):
7485
for m in reversed(dialogs.messages):
7586
if isinstance(m, types.MessageEmpty):
@@ -82,6 +93,7 @@ def get_offset_date(dialogs):
8293

8394
def get_input_media_from_file_id(
8495
file_id_str: str,
96+
file_ref: str = None,
8597
expected_media_type: int = None
8698
) -> Union[types.InputMediaPhoto, types.InputMediaDocument]:
8799
try:
@@ -111,7 +123,7 @@ def get_input_media_from_file_id(
111123
id=types.InputPhoto(
112124
id=file_id,
113125
access_hash=access_hash,
114-
file_reference=b""
126+
file_reference=decode_file_ref(file_ref)
115127
)
116128
)
117129

@@ -123,7 +135,7 @@ def get_input_media_from_file_id(
123135
id=types.InputDocument(
124136
id=file_id,
125137
access_hash=access_hash,
126-
file_reference=b""
138+
file_reference=decode_file_ref(file_ref)
127139
)
128140
)
129141

pyrogram/client/methods/messages/download_media.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class DownloadMedia(BaseClient):
3535
def download_media(
3636
self,
3737
message: Union["pyrogram.Message", str],
38-
file_ref: bytes = None,
38+
file_ref: str = None,
3939
file_name: str = DEFAULT_DOWNLOAD_DIR,
4040
block: bool = True,
4141
progress: callable = None,
@@ -48,8 +48,9 @@ def download_media(
4848
Pass a Message containing the media, the media itself (message.audio, message.video, ...) or
4949
the file id as string.
5050
51-
file_ref (``bytes``, *optional*):
51+
file_ref (``str``, *optional*):
5252
A valid file reference obtained by a recently fetched media message.
53+
To be used in combination with a file id in case a file reference is needed.
5354
5455
file_name (``str``, *optional*):
5556
A custom *file_name* to be used instead of the one provided by Telegram.
@@ -133,7 +134,7 @@ def download_media(
133134
file_size=file_size,
134135
mime_type=mime_type,
135136
date=date,
136-
file_ref=file_ref or b""
137+
file_ref=file_ref
137138
)
138139

139140
def get_existing_attributes() -> dict:

pyrogram/client/methods/messages/edit_inline_media.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,35 +77,35 @@ def edit_inline_media(
7777
url=media.media
7878
)
7979
else:
80-
media = utils.get_input_media_from_file_id(media.media, 2)
80+
media = utils.get_input_media_from_file_id(media.media, media.file_ref, 2)
8181
elif isinstance(media, InputMediaVideo):
8282
if media.media.startswith("http"):
8383
media = types.InputMediaDocumentExternal(
8484
url=media.media
8585
)
8686
else:
87-
media = utils.get_input_media_from_file_id(media.media, 4)
87+
media = utils.get_input_media_from_file_id(media.media, media.file_ref, 4)
8888
elif isinstance(media, InputMediaAudio):
8989
if media.media.startswith("http"):
9090
media = types.InputMediaDocumentExternal(
9191
url=media.media
9292
)
9393
else:
94-
media = utils.get_input_media_from_file_id(media.media, 9)
94+
media = utils.get_input_media_from_file_id(media.media, media.file_ref, 9)
9595
elif isinstance(media, InputMediaAnimation):
9696
if media.media.startswith("http"):
9797
media = types.InputMediaDocumentExternal(
9898
url=media.media
9999
)
100100
else:
101-
media = utils.get_input_media_from_file_id(media.media, 10)
101+
media = utils.get_input_media_from_file_id(media.media, media.file_ref, 10)
102102
elif isinstance(media, InputMediaDocument):
103103
if media.media.startswith("http"):
104104
media = types.InputMediaDocumentExternal(
105105
url=media.media
106106
)
107107
else:
108-
media = utils.get_input_media_from_file_id(media.media, 5)
108+
media = utils.get_input_media_from_file_id(media.media, media.file_ref, 5)
109109

110110
return self.send(
111111
functions.messages.EditInlineBotMessage(

pyrogram/client/methods/messages/edit_message_media.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def edit_message_media(
100100
url=media.media
101101
)
102102
else:
103-
media = utils.get_input_media_from_file_id(media.media, 2)
103+
media = utils.get_input_media_from_file_id(media.media, media.file_ref, 2)
104104
elif isinstance(media, InputMediaVideo):
105105
if os.path.exists(media.media):
106106
media = self.send(
@@ -137,7 +137,7 @@ def edit_message_media(
137137
url=media.media
138138
)
139139
else:
140-
media = utils.get_input_media_from_file_id(media.media, 4)
140+
media = utils.get_input_media_from_file_id(media.media, media.file_ref, 4)
141141
elif isinstance(media, InputMediaAudio):
142142
if os.path.exists(media.media):
143143
media = self.send(
@@ -173,7 +173,7 @@ def edit_message_media(
173173
url=media.media
174174
)
175175
else:
176-
media = utils.get_input_media_from_file_id(media.media, 9)
176+
media = utils.get_input_media_from_file_id(media.media, media.file_ref, 9)
177177
elif isinstance(media, InputMediaAnimation):
178178
if os.path.exists(media.media):
179179
media = self.send(
@@ -211,7 +211,7 @@ def edit_message_media(
211211
url=media.media
212212
)
213213
else:
214-
media = utils.get_input_media_from_file_id(media.media, 10)
214+
media = utils.get_input_media_from_file_id(media.media, media.file_ref, 10)
215215
elif isinstance(media, InputMediaDocument):
216216
if os.path.exists(media.media):
217217
media = self.send(
@@ -242,7 +242,7 @@ def edit_message_media(
242242
url=media.media
243243
)
244244
else:
245-
media = utils.get_input_media_from_file_id(media.media, 5)
245+
media = utils.get_input_media_from_file_id(media.media, media.file_ref, 5)
246246

247247
r = self.send(
248248
functions.messages.EditMessage(

pyrogram/client/methods/messages/send_animation.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def send_animation(
3030
self,
3131
chat_id: Union[int, str],
3232
animation: str,
33+
file_ref: str = None,
3334
caption: str = "",
3435
unsave: bool = False,
3536
parse_mode: Union[str, None] = object,
@@ -63,6 +64,10 @@ def send_animation(
6364
pass an HTTP URL as a string for Telegram to get an animation from the Internet, or
6465
pass a file path as string to upload a new animation that exists on your local machine.
6566
67+
file_ref (``str``, *optional*):
68+
A valid file reference obtained by a recently fetched media message.
69+
To be used in combination with a file id in case a file reference is needed.
70+
6671
caption (``str``, *optional*):
6772
Animation caption, 0-1024 characters.
6873
@@ -176,7 +181,7 @@ def progress(current, total):
176181
url=animation
177182
)
178183
else:
179-
media = utils.get_input_media_from_file_id(animation, 10)
184+
media = utils.get_input_media_from_file_id(animation, file_ref, 10)
180185

181186
while True:
182187
try:
@@ -209,7 +214,7 @@ def progress(current, total):
209214

210215
if unsave:
211216
document = message.animation or message.document
212-
document_id = utils.get_input_media_from_file_id(document.file_id).id
217+
document_id = utils.get_input_media_from_file_id(document.file_id, document.file_ref).id
213218

214219
self.send(
215220
functions.messages.SaveGif(

pyrogram/client/methods/messages/send_audio.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def send_audio(
3030
self,
3131
chat_id: Union[int, str],
3232
audio: str,
33+
file_ref: str = None,
3334
caption: str = "",
3435
parse_mode: Union[str, None] = object,
3536
duration: int = 0,
@@ -64,6 +65,10 @@ def send_audio(
6465
pass an HTTP URL as a string for Telegram to get an audio file from the Internet, or
6566
pass a file path as string to upload a new audio file that exists on your local machine.
6667
68+
file_ref (``str``, *optional*):
69+
A valid file reference obtained by a recently fetched media message.
70+
To be used in combination with a file id in case a file reference is needed.
71+
6772
caption (``str``, *optional*):
6873
Audio caption, 0-1024 characters.
6974
@@ -174,7 +179,7 @@ def progress(current, total):
174179
url=audio
175180
)
176181
else:
177-
media = utils.get_input_media_from_file_id(audio, 9)
182+
media = utils.get_input_media_from_file_id(audio, file_ref, 9)
178183

179184
while True:
180185
try:

pyrogram/client/methods/messages/send_cached_media.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def send_cached_media(
2828
self,
2929
chat_id: Union[int, str],
3030
file_id: str,
31+
file_ref: str = None,
3132
caption: str = "",
3233
parse_mode: Union[str, None] = object,
3334
disable_notification: bool = None,
@@ -56,6 +57,10 @@ def send_cached_media(
5657
Media to send.
5758
Pass a file_id as string to send a media that exists on the Telegram servers.
5859
60+
file_ref (``str``, *optional*):
61+
A valid file reference obtained by a recently fetched media message.
62+
To be used in combination with a file id in case a file reference is needed.
63+
5964
caption (``bool``, *optional*):
6065
Media caption, 0-1024 characters.
6166
@@ -92,7 +97,7 @@ def send_cached_media(
9297
r = self.send(
9398
functions.messages.SendMedia(
9499
peer=self.resolve_peer(chat_id),
95-
media=utils.get_input_media_from_file_id(file_id),
100+
media=utils.get_input_media_from_file_id(file_id, file_ref),
96101
silent=disable_notification or None,
97102
reply_to_msg_id=reply_to_message_id,
98103
random_id=self.rnd_id(),

pyrogram/client/methods/messages/send_document.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def send_document(
3030
self,
3131
chat_id: Union[int, str],
3232
document: str,
33+
file_ref: str = None,
3334
thumb: str = None,
3435
caption: str = "",
3536
parse_mode: Union[str, None] = object,
@@ -59,6 +60,10 @@ def send_document(
5960
pass an HTTP URL as a string for Telegram to get a file from the Internet, or
6061
pass a file path as string to upload a new file that exists on your local machine.
6162
63+
file_ref (``str``, *optional*):
64+
A valid file reference obtained by a recently fetched media message.
65+
To be used in combination with a file id in case a file reference is needed.
66+
6267
thumb (``str``, *optional*):
6368
Thumbnail of the file sent.
6469
The thumbnail should be in JPEG format and less than 200 KB in size.
@@ -149,7 +154,7 @@ def progress(current, total):
149154
url=document
150155
)
151156
else:
152-
media = utils.get_input_media_from_file_id(document, 5)
157+
media = utils.get_input_media_from_file_id(document, file_ref, 5)
153158

154159
while True:
155160
try:

0 commit comments

Comments
 (0)