From 8c82b3afa77d1e3a8291b3e2a6e5248cc23230ca Mon Sep 17 00:00:00 2001 From: YeeChanKo Date: Mon, 6 Aug 2018 20:07:28 +0900 Subject: [PATCH] Fixed branching logic in _file_to_media --- telethon/client/uploads.py | 41 ++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/telethon/client/uploads.py b/telethon/client/uploads.py index e0c2596f..fbdd1ddb 100644 --- a/telethon/client/uploads.py +++ b/telethon/client/uploads.py @@ -394,26 +394,29 @@ class UploadMethods(ButtonMethods, MessageParseMethods, UserMethods): as_image = utils.is_image(file) and not force_document use_cache = types.InputPhoto if as_image else types.InputDocument - if re.match('https?://', file): - if as_image: - media = types.InputMediaPhotoExternal(file) - elif not force_document and utils.is_gif(file): - media = types.InputMediaGifExternal(file, '') + if isinstance(file, str): + if re.match('https?://', file): + if as_image: + media = types.InputMediaPhotoExternal(file) + elif not force_document and utils.is_gif(file): + media = types.InputMediaGifExternal(file, '') + else: + media = types.InputMediaDocumentExternal(file) else: - media = types.InputMediaDocumentExternal(file) - elif isinstance(file, str): - file_handle = await self.upload_file( - file, progress_callback=progress_callback, - use_cache=use_cache if allow_cache else None - ) - else: - bot_file = utils.resolve_bot_file_id(file) - if bot_file: - media = utils.get_input_media(bot_file) - - if media: - pass # Already have media, don't check the rest - elif isinstance(file_handle, use_cache): + bot_file = utils.resolve_bot_file_id(file) + if bot_file: + media = utils.get_input_media(bot_file) + # Return if media is resolved + if media: + return None, media + + # Proceed with local file handle + file_handle = await self.upload_file( + file, progress_callback=progress_callback, + use_cache=use_cache if allow_cache else None + ) + + if isinstance(file_handle, use_cache): # File was cached, so an instance of use_cache was returned if as_image: media = types.InputMediaPhoto(file_handle)