Allow to disable loading file to memory before sending.

This commit is contained in:
Alex Root Junior 2017-11-21 00:22:05 +02:00
parent 7dd44c1dfa
commit 233acab68e

View file

@ -28,18 +28,19 @@ class InputMedia(base.TelegramObject):
@file.setter
def file(self, file: io.IOBase):
# File must be not closed before sending media.
# Read file into BytesIO
if isinstance(file, io.BufferedIOBase):
# Go to start of file
if file.seekable():
if self.conf.get('cache'):
# File must be not closed before sending media.
# Read file into BytesIO
if isinstance(file, io.BufferedIOBase):
# Go to start of file
if file.seekable():
file.seek(0)
# Read
temp_file = io.BytesIO(file.read())
# Reset cursor
file.seek(0)
# Read
temp_file = io.BytesIO(file.read())
# Reset cursor
file.seek(0)
# Replace variable
file = temp_file
# Replace variable
file = temp_file
setattr(self, '_file', file)
self.media = ATTACHMENT_PREFIX + secrets.token_urlsafe(16)
@ -58,8 +59,8 @@ class InputMediaPhoto(InputMedia):
https://core.telegram.org/bots/api#inputmediaphoto
"""
def __init__(self, media: base.InputFile, caption: base.String = None):
super(InputMediaPhoto, self).__init__(type='photo', media=media, caption=caption)
def __init__(self, media: base.InputFile, caption: base.String = None, cache=True):
super(InputMediaPhoto, self).__init__(type='photo', media=media, caption=caption, conf={'cache': cache})
if isinstance(media, io.IOBase):
self.file = media
@ -76,9 +77,11 @@ class InputMediaVideo(InputMedia):
duration: base.Integer = fields.Field()
def __init__(self, media: base.InputFile, caption: base.String = None,
width: base.Integer = None, height: base.Integer = None, duration: base.Integer = None):
width: base.Integer = None, height: base.Integer = None, duration: base.Integer = None,
cache=True):
super(InputMediaVideo, self).__init__(type='video', media=media, caption=caption,
width=width, height=height, duration=duration)
width=width, height=height, duration=duration,
conf={'cache': cache})
if isinstance(media, io.IOBase):
self.file = media