Slightly better flow in _file_to_media

This commit is contained in:
Lonami Exo 2020-03-14 12:16:52 +01:00
parent ccfd7a1015
commit 3ab9986fc7
2 changed files with 16 additions and 15 deletions

View File

@ -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,

View File

@ -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(