From 758556cd30105f53b5d6f61af53e636fe3ab9ad2 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Wed, 6 Mar 2019 09:24:50 +0100 Subject: [PATCH] Fix upload_file not seeking streams back This would cause issues in _cache_media since utils.is_image fails in the second pass (it respects the stream's position, and the user may rightfully pass a stream that should be read only from one pos). --- telethon/client/uploads.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/telethon/client/uploads.py b/telethon/client/uploads.py index 427c5c7e..5e63c6cc 100644 --- a/telethon/client/uploads.py +++ b/telethon/client/uploads.py @@ -384,7 +384,17 @@ class UploadMethods(ButtonMethods, MessageParseMethods, UserMethods): elif isinstance(file, bytes): file_size = len(file) else: - file = file.read() + if isinstance(file, io.IOBase) and file.seekable(): + pos = file.tell() + else: + pos = None + + # TODO Don't load the entire file in memory always + data = file.read() + if pos is not None: + file.seek(pos) + + file = data file_size = len(file) # File will now either be a string or bytes