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).
This commit is contained in:
Lonami Exo 2019-03-06 09:24:50 +01:00
parent fcfebf75a3
commit 758556cd30

View File

@ -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