From 3ab9986fc7be852cd9976b2bf5c72bc4b43906a5 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sat, 14 Mar 2020 12:16:52 +0100 Subject: [PATCH] Slightly better flow in _file_to_media --- telethon/client/uploads.py | 26 ++++++++++++++------------ telethon/utils.py | 5 ++--- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/telethon/client/uploads.py b/telethon/client/uploads.py index 3ec0990e..fb924595 100644 --- a/telethon/client/uploads.py +++ b/telethon/client/uploads.py @@ -637,17 +637,10 @@ class UploadMethods: if as_image is None: as_image = utils.is_image(file) and not force_document - if as_image or not thumb: - # Images don't have thumb so don't bother uploading it - thumb = None - else: - if isinstance(thumb, pathlib.Path): - thumb = str(thumb.absolute()) - thumb = await self.upload_file(thumb) - # `aiofiles` do not base `io.IOBase` but do have `read`, so we # just check for the read attribute to see if it's file-like. - if not isinstance(file, (str, bytes)) and not hasattr(file, 'read'): + if not isinstance(file, (str, bytes, types.InputFile, types.InputFileBig))\ + and not hasattr(file, 'read'): # The user may pass a Message containing media (or the media, # or anything similar) that should be treated as a file. Try # getting the input media for whatever they passed and send it. @@ -662,8 +655,7 @@ class UploadMethods: force_document=force_document, voice_note=voice_note, video_note=video_note, - supports_streaming=supports_streaming, - thumb=thumb, + supports_streaming=supports_streaming ), as_image) except TypeError: # Can't turn whatever was given into media @@ -671,7 +663,10 @@ class UploadMethods: media = None file_handle = None - if not isinstance(file, str) or os.path.isfile(file): + + if isinstance(file, (types.InputFile, types.InputFileBig)): + file_handle = file + elif not isinstance(file, str) or os.path.isfile(file): file_handle = await self.upload_file( _resize_photo_if_needed(file, as_image), progress_callback=progress_callback @@ -708,6 +703,13 @@ class UploadMethods: supports_streaming=supports_streaming ) + if not thumb: + thumb = None + else: + if isinstance(thumb, pathlib.Path): + thumb = str(thumb.absolute()) + thumb = await self.upload_file(thumb) + media = types.InputMediaUploadedDocument( file=file_handle, mime_type=mime_type, diff --git a/telethon/utils.py b/telethon/utils.py index b07cf875..0c63d143 100644 --- a/telethon/utils.py +++ b/telethon/utils.py @@ -427,8 +427,7 @@ def get_input_geo(geo): def get_input_media( media, *, is_photo=False, attributes=None, force_document=False, - voice_note=False, video_note=False, supports_streaming=False, - thumb=None, + voice_note=False, video_note=False, supports_streaming=False ): """ Similar to :meth:`get_input_peer`, but for media. @@ -482,7 +481,7 @@ def get_input_media( supports_streaming=supports_streaming ) return types.InputMediaUploadedDocument( - file=media, mime_type=mime, attributes=attrs, thumb=thumb) + file=media, mime_type=mime, attributes=attrs) if isinstance(media, types.MessageMediaGame): return types.InputMediaGame(id=types.InputGameID(