mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-10 19:46:36 +03:00
Support sending bytes/stream as photos (improved _get_extension)
Letting _get_extension work for photos even when the only information available is a stream or a few bytes allows sending arbitrary bytes as photos, if force_document was not set to True explicitly.
This commit is contained in:
parent
8f8ae9aee5
commit
bf11bbd8a6
|
@ -337,6 +337,12 @@ class UploadMethods(ButtonMethods, MessageParseMethods, UserMethods):
|
|||
else:
|
||||
file_name = str(file_id)
|
||||
|
||||
# If the file name lacks extension, add it if possible.
|
||||
# Else Telegram complains with `PHOTO_EXT_INVALID_ERROR`
|
||||
# even if the uploaded image is indeed a photo.
|
||||
if not os.path.splitext(file_name)[-1]:
|
||||
file_name += utils._get_extension(file)
|
||||
|
||||
# Determine whether the file is too big (over 10MB) or not
|
||||
# Telegram does make a distinction between smaller or larger files
|
||||
is_large = file_size > 10 * 1024 * 1024
|
||||
|
|
|
@ -4,6 +4,9 @@ to convert between an entity like a User, Chat, etc. into its Input version)
|
|||
"""
|
||||
import base64
|
||||
import binascii
|
||||
import imghdr
|
||||
import inspect
|
||||
import io
|
||||
import itertools
|
||||
import math
|
||||
import mimetypes
|
||||
|
@ -16,7 +19,6 @@ from types import GeneratorType
|
|||
from .extensions import markdown, html
|
||||
from .helpers import add_surrogate, del_surrogate
|
||||
from .tl import types
|
||||
import inspect
|
||||
|
||||
try:
|
||||
import hachoir
|
||||
|
@ -632,6 +634,12 @@ def _get_extension(file):
|
|||
"""
|
||||
if isinstance(file, str):
|
||||
return os.path.splitext(file)[-1]
|
||||
elif isinstance(file, bytes):
|
||||
kind = imghdr.what(io.BytesIO(file))
|
||||
return ('.' + kind) if kind else ''
|
||||
elif isinstance(file, io.IOBase) and file.seekable():
|
||||
kind = imghdr.what(file) is not None
|
||||
return ('.' + kind) if kind else ''
|
||||
elif getattr(file, 'name', None):
|
||||
return _get_extension(file.name)
|
||||
else:
|
||||
|
|
Loading…
Reference in New Issue
Block a user