diff --git a/mautrix_telegram/portal.py b/mautrix_telegram/portal.py index 8afd3de3..527da974 100644 --- a/mautrix_telegram/portal.py +++ b/mautrix_telegram/portal.py @@ -598,8 +598,8 @@ class Portal: return False @staticmethod - def _get_largest_photo_size(photo: Photo) -> TypePhotoSize: - return max(photo.sizes, key=(lambda photo2: ( + def _get_largest_photo_size(photo: Union[Photo, List[TypePhotoSize]]) -> TypePhotoSize: + return max(photo.sizes if isinstance(photo, Photo) else photo, key=(lambda photo2: ( len(photo2.bytes) if not isinstance(photo2, PhotoSize) else photo2.size))) async def remove_avatar(self, _: 'AbstractUser', save: bool = False) -> None: @@ -1352,8 +1352,8 @@ class Portal: return attrs @staticmethod - def _parse_telegram_document_meta(evt: Message, file: DBTelegramFile, attrs: Dict - ) -> Tuple[Dict, str]: + def _parse_telegram_document_meta(evt: Message, file: DBTelegramFile, attrs: Dict, + thumb: TypePhotoSize) -> Tuple[Dict, str]: document = evt.media.document name = evt.message or attrs["name"] if attrs["is_sticker"]: @@ -1381,8 +1381,8 @@ class Portal: info["thumbnail_url"] = file.thumbnail.mxc info["thumbnail_info"] = { "mimetype": file.thumbnail.mime_type, - "h": file.thumbnail.height or document.thumb.h, - "w": file.thumbnail.width or document.thumb.w, + "h": file.thumbnail.height or thumb.h, + "w": file.thumbnail.width or thumb.w, "size": file.thumbnail.size, } @@ -1393,12 +1393,16 @@ class Portal: document = evt.media.document attrs = self._parse_telegram_document_attributes(document.attributes) - file = await util.transfer_file_to_matrix(source.client, intent, document, - document.thumb, is_sticker=attrs["is_sticker"]) + thumb = self._get_largest_photo_size(document.thumbs) + if not isinstance(thumb, (PhotoSize, PhotoCachedSize)): + self.log.debug(f"Unsupported thumbnail type {type(thumb)}") + thumb = None + file = await util.transfer_file_to_matrix(source.client, intent, document, thumb, + is_sticker=attrs["is_sticker"]) if not file: return None - info, name = self._parse_telegram_document_meta(evt, file, attrs) + info, name = self._parse_telegram_document_meta(evt, file, attrs, thumb) await intent.set_typing(self.mxid, is_typing=False) diff --git a/mautrix_telegram/util/file_transfer.py b/mautrix_telegram/util/file_transfer.py index 370cbf1e..bb707045 100644 --- a/mautrix_telegram/util/file_transfer.py +++ b/mautrix_telegram/util/file_transfer.py @@ -23,8 +23,8 @@ import asyncio import magic from sqlalchemy.exc import IntegrityError, InvalidRequestError -from telethon.tl.types import (Document, FileLocation, InputFileLocation, - InputDocumentFileLocation, PhotoSize, PhotoCachedSize) +from telethon.tl.types import (Document, FileLocation, InputFileLocation, InputDocumentFileLocation, + TypePhotoSize, PhotoSize, PhotoCachedSize) from telethon.errors import (AuthBytesInvalidError, AuthKeyInvalidError, LocationInvalidError, SecurityError) from mautrix_appservice import IntentAPI @@ -149,7 +149,7 @@ transfer_locks = {} # type: Dict[str, asyncio.Lock] async def transfer_file_to_matrix(client: MautrixTelegramClient, intent: IntentAPI, - location: TypeLocation, thumbnail: Optional[TypeLocation] = None, + location: TypeLocation, thumbnail: Optional[Union[TypeLocation, TypePhotoSize]] = None, is_sticker: bool = False) -> Optional[DBTelegramFile]: location_id = _location_to_id(location) if not location_id: @@ -171,7 +171,7 @@ async def transfer_file_to_matrix(client: MautrixTelegramClient, intent: IntentA async def _unlocked_transfer_file_to_matrix(client: MautrixTelegramClient, intent: IntentAPI, loc_id: str, location: TypeLocation, - thumbnail: Optional[TypeLocation], + thumbnail: Optional[Union[TypeLocation, TypePhotoSize]], is_sticker: bool) -> Optional[DBTelegramFile]: db_file = DBTelegramFile.get(loc_id) if db_file: