Skip to content

Commit 9b5d09f

Browse files
committed
more refactoring upload media
1 parent 82c3ea8 commit 9b5d09f

7 files changed

Lines changed: 297 additions & 277 deletions

File tree

pyrogram/methods/messages/send_animation.py

Lines changed: 35 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
import os
20-
import re
2120
from datetime import datetime
2221
from typing import BinaryIO, Callable, List, Optional, Union
2322

@@ -226,66 +225,34 @@ async def progress(current, total):
226225
reply_parameters=reply_parameters
227226
)
228227

229-
file = None
230-
231228
try:
232229
if isinstance(animation, str):
233-
if os.path.isfile(animation):
234-
thumb = await self.save_file(thumb)
235-
file = await self.save_file(animation, progress=progress, progress_args=progress_args)
236-
media = raw.types.InputMediaUploadedDocument(
237-
mime_type=self.guess_mime_type(animation) or "video/mp4",
238-
file=file,
239-
thumb=thumb,
240-
spoiler=has_spoiler,
241-
attributes=[
242-
raw.types.DocumentAttributeVideo(
243-
supports_streaming=True,
244-
duration=duration,
245-
w=width,
246-
h=height
247-
),
248-
raw.types.DocumentAttributeFilename(file_name=file_name or os.path.basename(animation)),
249-
raw.types.DocumentAttributeAnimated()
250-
]
251-
)
252-
elif re.match("^https?://", animation):
253-
media = raw.types.InputMediaDocumentExternal(
254-
url=animation,
255-
spoiler=has_spoiler
256-
)
257-
else:
258-
media = utils.get_input_media_from_file_id(animation, FileType.ANIMATION, has_spoiler=has_spoiler)
230+
actual_file_name = file_name or os.path.basename(animation)
259231
else:
260-
thumb = await self.save_file(thumb)
261-
file = await self.save_file(animation, progress=progress, progress_args=progress_args)
262-
media = raw.types.InputMediaUploadedDocument(
263-
mime_type=self.guess_mime_type(file_name or animation.name) or "video/mp4",
264-
file=file,
265-
thumb=thumb,
266-
spoiler=has_spoiler,
267-
attributes=[
268-
raw.types.DocumentAttributeVideo(
269-
supports_streaming=True,
270-
duration=duration,
271-
w=width,
272-
h=height
273-
),
274-
raw.types.DocumentAttributeFilename(file_name=file_name or animation.name),
275-
raw.types.DocumentAttributeAnimated()
276-
]
277-
)
278-
279-
# Функция для обработки unsave после отправки
280-
async def post_process(client, message):
281-
if unsave and message.animation:
282-
document_id = utils.get_input_media_from_file_id(
283-
message.animation.file_id,
284-
FileType.ANIMATION,
285-
).id
286-
await client.invoke(raw.functions.messages.SaveGif(id=document_id, unsave=True))
287-
288-
return await send_media(
232+
actual_file_name = file_name or getattr(animation, 'name', 'animation.gif')
233+
234+
attributes = utils.make_document_attributes(
235+
file_name=actual_file_name,
236+
duration=duration,
237+
width=width,
238+
height=height,
239+
supports_streaming=True,
240+
animated=True,
241+
)
242+
243+
media, file = await utils.prepare_media_for_upload(
244+
client=self,
245+
file=animation,
246+
file_type=FileType.ANIMATION,
247+
default_mime="video/mp4",
248+
thumb=thumb,
249+
attributes=attributes,
250+
spoiler=has_spoiler,
251+
progress=progress,
252+
progress_args=progress_args,
253+
)
254+
255+
message = await send_media(
289256
client=self,
290257
chat_id=chat_id,
291258
media=media,
@@ -306,9 +273,16 @@ async def post_process(client, message):
306273
suggested_post_parameters=suggested_post_parameters,
307274
reply_markup=reply_markup,
308275
file_reference=animation,
309-
file_part_callback=lambda ref, part: self.save_file(ref, file_id=file.id, file_part=part),
310-
use_parse_messages=False, # Используем ручной парсинг для совместимости с unsave
311-
post_process_callback=post_process if unsave else None,
276+
file_part_callback=lambda ref, part: self.save_file(ref, file_id=file.id, file_part=part) if file else None,
277+
use_parse_messages=False,
312278
)
279+
280+
if unsave and message.animation:
281+
document_id = utils.get_input_media_from_file_id(
282+
message.animation.file_id,
283+
FileType.ANIMATION,
284+
).id
285+
await self.invoke(raw.functions.messages.SaveGif(id=document_id, unsave=True))
286+
313287
except StopTransmission:
314288
return None

pyrogram/methods/messages/send_audio.py

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
import os
20-
import re
2120
from datetime import datetime
2221
from typing import BinaryIO, Callable, List, Optional, Union
2322

2423
import pyrogram
25-
from pyrogram import StopTransmission, enums, raw, types, utils
24+
from pyrogram import StopTransmission, enums, types, utils
2625
from pyrogram.file_id import FileType
2726
from pyrogram.utils import handle_deprecated_reply_parameters, send_media
2827

@@ -216,54 +215,35 @@ async def progress(current, total):
216215
reply_parameters=reply_parameters
217216
)
218217

219-
file = None
220-
221218
try:
222219
if isinstance(audio, str):
223-
if os.path.isfile(audio):
224-
mime_type = self.guess_mime_type(audio) or "audio/mpeg"
225-
if mime_type == "audio/ogg":
226-
mime_type = "audio/opus"
227-
thumb = await self.save_file(thumb)
228-
file = await self.save_file(audio, progress=progress, progress_args=progress_args)
229-
media = raw.types.InputMediaUploadedDocument(
230-
mime_type=mime_type,
231-
file=file,
232-
thumb=thumb,
233-
attributes=[
234-
raw.types.DocumentAttributeAudio(
235-
duration=duration,
236-
performer=performer,
237-
title=title
238-
),
239-
raw.types.DocumentAttributeFilename(file_name=file_name or os.path.basename(audio))
240-
]
241-
)
242-
elif re.match("^https?://", audio):
243-
media = raw.types.InputMediaDocumentExternal(
244-
url=audio
245-
)
246-
else:
247-
media = utils.get_input_media_from_file_id(audio, FileType.AUDIO)
220+
actual_file_name = file_name or os.path.basename(audio)
248221
else:
249-
mime_type = self.guess_mime_type(file_name or audio.name) or "audio/mpeg"
250-
if mime_type == "audio/ogg":
251-
mime_type = "audio/opus"
252-
thumb = await self.save_file(thumb)
253-
file = await self.save_file(audio, progress=progress, progress_args=progress_args)
254-
media = raw.types.InputMediaUploadedDocument(
255-
mime_type=mime_type,
256-
file=file,
257-
thumb=thumb,
258-
attributes=[
259-
raw.types.DocumentAttributeAudio(
260-
duration=duration,
261-
performer=performer,
262-
title=title
263-
),
264-
raw.types.DocumentAttributeFilename(file_name=file_name or audio.name)
265-
]
266-
)
222+
actual_file_name = file_name or getattr(audio, 'name', 'audio.mp3')
223+
224+
mime_type = self.guess_mime_type(actual_file_name) or "audio/mpeg"
225+
if mime_type == "audio/ogg":
226+
mime_type = "audio/opus"
227+
228+
attributes = utils.make_document_attributes(
229+
file_name=actual_file_name,
230+
duration=duration,
231+
performer=performer,
232+
title=title,
233+
)
234+
235+
# Подготавливаем медиа
236+
media, file = await utils.prepare_media_for_upload(
237+
client=self,
238+
file=audio,
239+
file_type=FileType.AUDIO,
240+
mime_type=mime_type,
241+
default_mime="audio/mpeg",
242+
thumb=thumb,
243+
attributes=attributes,
244+
progress=progress,
245+
progress_args=progress_args,
246+
)
267247

268248
return await send_media(
269249
client=self,
@@ -285,7 +265,7 @@ async def progress(current, total):
285265
suggested_post_parameters=suggested_post_parameters,
286266
reply_markup=reply_markup,
287267
file_reference=audio,
288-
file_part_callback=lambda ref, part: self.save_file(ref, file_id=file.id, file_part=part),
268+
file_part_callback=lambda ref, part: self.save_file(ref, file_id=file.id, file_part=part) if file else None,
289269
)
290270
except StopTransmission:
291271
return None

pyrogram/methods/messages/send_document.py

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
import os
20-
import re
2120
from datetime import datetime
2221
from typing import BinaryIO, Callable, List, Optional, Union
2322

2423
import pyrogram
25-
from pyrogram import StopTransmission, enums, raw, types, utils
24+
from pyrogram import StopTransmission, enums, types, utils
2625
from pyrogram.file_id import FileType
2726
from pyrogram.utils import handle_deprecated_reply_parameters, send_media
2827

@@ -202,39 +201,25 @@ async def progress(current, total):
202201
reply_parameters=reply_parameters
203202
)
204203

205-
file = None
206-
207204
try:
208205
if isinstance(document, str):
209-
if os.path.isfile(document):
210-
thumb = await self.save_file(thumb)
211-
file = await self.save_file(document, progress=progress, progress_args=progress_args)
212-
media = raw.types.InputMediaUploadedDocument(
213-
mime_type=self.guess_mime_type(document) or "application/zip",
214-
file=file,
215-
force_file=force_document or None,
216-
thumb=thumb,
217-
attributes=[
218-
raw.types.DocumentAttributeFilename(file_name=file_name or os.path.basename(document))
219-
]
220-
)
221-
elif re.match("^https?://", document):
222-
media = raw.types.InputMediaDocumentExternal(
223-
url=document
224-
)
225-
else:
226-
media = utils.get_input_media_from_file_id(document, FileType.DOCUMENT)
206+
actual_file_name = file_name or os.path.basename(document)
227207
else:
228-
thumb = await self.save_file(thumb)
229-
file = await self.save_file(document, progress=progress, progress_args=progress_args)
230-
media = raw.types.InputMediaUploadedDocument(
231-
mime_type=self.guess_mime_type(file_name or document.name) or "application/zip",
232-
file=file,
233-
thumb=thumb,
234-
attributes=[
235-
raw.types.DocumentAttributeFilename(file_name=file_name or document.name)
236-
]
237-
)
208+
actual_file_name = file_name or getattr(document, 'name', 'document')
209+
210+
attributes = utils.make_document_attributes(file_name=actual_file_name)
211+
212+
media, file = await utils.prepare_media_for_upload(
213+
client=self,
214+
file=document,
215+
file_type=FileType.DOCUMENT,
216+
default_mime="application/zip",
217+
thumb=thumb,
218+
attributes=attributes,
219+
force_file=force_document,
220+
progress=progress,
221+
progress_args=progress_args,
222+
)
238223

239224
return await send_media(
240225
client=self,
@@ -256,7 +241,7 @@ async def progress(current, total):
256241
suggested_post_parameters=suggested_post_parameters,
257242
reply_markup=reply_markup,
258243
file_reference=document,
259-
file_part_callback=lambda ref, part: self.save_file(ref, file_id=file.id, file_part=part),
244+
file_part_callback=lambda ref, part: self.save_file(ref, file_id=file.id, file_part=part) if file else None,
260245
)
261246
except StopTransmission:
262247
return None

pyrogram/methods/messages/send_sticker.py

Lines changed: 19 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
import os
20-
import re
2120
from datetime import datetime
2221
from typing import BinaryIO, Callable, List, Optional, Union
2322

2423
import pyrogram
25-
from pyrogram import StopTransmission, enums, raw, types, utils
24+
from pyrogram import StopTransmission, enums, types, utils
2625
from pyrogram.file_id import FileType
2726
from pyrogram.utils import handle_deprecated_reply_parameters, send_media
2827

@@ -184,42 +183,26 @@ async def send_sticker(
184183
reply_parameters=reply_parameters
185184
)
186185

187-
file = None
188-
189186
try:
190187
if isinstance(sticker, str):
191-
if os.path.isfile(sticker):
192-
file = await self.save_file(sticker, progress=progress, progress_args=progress_args)
193-
media = raw.types.InputMediaUploadedDocument(
194-
mime_type=self.guess_mime_type(sticker) or "image/webp",
195-
file=file,
196-
attributes=[
197-
raw.types.DocumentAttributeFilename(file_name=os.path.basename(sticker)),
198-
raw.types.DocumentAttributeSticker(
199-
alt=emoji,
200-
stickerset=raw.types.InputStickerSetEmpty()
201-
),
202-
]
203-
)
204-
elif re.match("^https?://", sticker):
205-
media = raw.types.InputMediaDocumentExternal(
206-
url=sticker
207-
)
208-
else:
209-
media = utils.get_input_media_from_file_id(sticker, FileType.STICKER)
188+
actual_file_name = os.path.basename(sticker) if os.path.isfile(sticker) else "sticker.webp"
210189
else:
211-
file = await self.save_file(sticker, progress=progress, progress_args=progress_args)
212-
media = raw.types.InputMediaUploadedDocument(
213-
mime_type=self.guess_mime_type(sticker.name) or "image/webp",
214-
file=file,
215-
attributes=[
216-
raw.types.DocumentAttributeFilename(file_name=sticker.name),
217-
raw.types.DocumentAttributeSticker(
218-
alt=emoji,
219-
stickerset=raw.types.InputStickerSetEmpty()
220-
),
221-
]
222-
)
190+
actual_file_name = getattr(sticker, 'name', 'sticker.webp')
191+
192+
attributes = utils.make_document_attributes(
193+
file_name=actual_file_name,
194+
sticker_alt=emoji,
195+
)
196+
197+
media, file = await utils.prepare_media_for_upload(
198+
client=self,
199+
file=sticker,
200+
file_type=FileType.STICKER,
201+
default_mime="image/webp",
202+
attributes=attributes,
203+
progress=progress,
204+
progress_args=progress_args,
205+
)
223206

224207
return await send_media(
225208
client=self,
@@ -241,7 +224,7 @@ async def send_sticker(
241224
suggested_post_parameters=suggested_post_parameters,
242225
reply_markup=reply_markup,
243226
file_reference=sticker,
244-
file_part_callback=lambda ref, part: self.save_file(ref, file_id=file.id, file_part=part),
227+
file_part_callback=lambda ref, part: self.save_file(ref, file_id=file.id, file_part=part) if file else None,
245228
)
246229
except StopTransmission:
247230
return None

0 commit comments

Comments
 (0)