Support URLs on send_file

This commit is contained in:
Lonami Exo 2018-06-26 16:39:22 +02:00
parent 2b090f8888
commit 2bd0c6c525
2 changed files with 25 additions and 6 deletions

View File

@ -3,7 +3,7 @@ import io
import logging
import os
import pathlib
import warnings
import re
from io import BytesIO
from mimetypes import guess_type
@ -371,14 +371,26 @@ class UploadMethods(MessageParseMethods, UserMethods):
except TypeError:
return None, None # Can't turn whatever was given into media
media = None
as_image = utils.is_image(file) and not force_document
use_cache = types.InputPhoto if as_image else types.InputDocument
file_handle = await self.upload_file(
file, progress_callback=progress_callback,
use_cache=use_cache if allow_cache else None
)
if isinstance(file, str) and re.match('https?://', file):
file_handle = None
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:
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):
if media:
pass # Already have media, don't check the rest
elif isinstance(file_handle, use_cache):
# File was cached, so an instance of use_cache was returned
if as_image:
media = types.InputMediaPhoto(file_handle)

View File

@ -510,6 +510,13 @@ def is_image(file):
return re.match(r'\.(png|jpe?g)', _get_extension(file), re.IGNORECASE)
def is_gif(file):
"""
Returns ``True`` if the file extension looks like a gif file to Telegram.
"""
return re.match(r'\.gif', _get_extension(file), re.IGNORECASE)
def is_audio(file):
"""Returns ``True`` if the file extension looks like an audio file."""
file = 'a' + _get_extension(file)