From 233acab68e30358a2116a5d336ec8530bae8918e Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Tue, 21 Nov 2017 00:22:05 +0200 Subject: [PATCH] Allow to disable loading file to memory before sending. --- aiogram/types/input_media.py | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/aiogram/types/input_media.py b/aiogram/types/input_media.py index 391946d3..98a7bc86 100644 --- a/aiogram/types/input_media.py +++ b/aiogram/types/input_media.py @@ -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