From b0e96b2821023bed229dda86fe5f7cfc12bfbdfe Mon Sep 17 00:00:00 2001 From: YoilyL Date: Thu, 25 Apr 2019 19:31:52 +0100 Subject: [PATCH] Fix stripped image downloads (#1165) --- telethon/client/downloads.py | 4 +++- telethon/utils.py | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/telethon/client/downloads.py b/telethon/client/downloads.py index 06e2917b..77229641 100644 --- a/telethon/client/downloads.py +++ b/telethon/client/downloads.py @@ -344,7 +344,9 @@ class DownloadMethods(UserMethods): f = file try: - f.write(size.bytes) + f.write(utils.stripped_photo_to_jpg(size.bytes) + if isinstance(size, types.PhotoStrippedSize) + else size.bytes) finally: if isinstance(file, str): f.close() diff --git a/telethon/utils.py b/telethon/utils.py index 05cf9432..2784f446 100644 --- a/telethon/utils.py +++ b/telethon/utils.py @@ -1123,3 +1123,19 @@ class AsyncClassWrapper: return w.fget(self.wrapped) else: return w + + +def stripped_photo_to_jpg(stripped): + """ + Adds the JPG header and footer to a stripped image. + + Ported from https://github.com/telegramdesktop/tdesktop/blob/bec39d89e19670eb436dc794a8f20b657cb87c71/Telegram/SourceFiles/ui/image/image.cpp#L225 + """ + if len(stripped) < 3 or stripped[0] != '\x01': + return stripped + + header = bytearray(b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C\x00(\x1c\x1e#\x1e\x19(#!#-+(0