Skip to content
Permalink
Browse files

Slightly better flow in _file_to_media

  • Loading branch information
Lonami committed Mar 14, 2020
1 parent ccfd7a1 commit 3ab9986fc7be852cd9976b2bf5c72bc4b43906a5
Showing with 16 additions and 15 deletions.
  1. +14 −12 telethon/client/uploads.py
  2. +2 −3 telethon/utils.py
@@ -637,17 +637,10 @@ def callback(current, total):
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,16 +655,18 @@ def callback(current, total):
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
return None, None, as_image

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 @@ def callback(current, total):
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,
@@ -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(

0 comments on commit 3ab9986

Please sign in to comment.
You can’t perform that action at this time.