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