1717# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818
1919import os
20- import re
2120from datetime import datetime
2221from 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
0 commit comments