Fix finding largest photo size. Fixes #586
This commit is contained in:
@@ -27,7 +27,7 @@ from telethon.tl.types import (Channel, ChannelFull, Chat, ChatFull, InputChanne
|
|||||||
TypeUser, TypeUserFull, User, UserFull, TypeInputChannel, Photo,
|
TypeUser, TypeUserFull, User, UserFull, TypeInputChannel, Photo,
|
||||||
Document, TypePhotoSize, PhotoSize, InputPhotoFileLocation,
|
Document, TypePhotoSize, PhotoSize, InputPhotoFileLocation,
|
||||||
TypeChatParticipant, TypeChannelParticipant, PhotoEmpty, ChatPhoto,
|
TypeChatParticipant, TypeChannelParticipant, PhotoEmpty, ChatPhoto,
|
||||||
ChatPhotoEmpty)
|
ChatPhotoEmpty, PhotoSizeProgressive, PhotoSizeEmpty)
|
||||||
|
|
||||||
from mautrix.errors import MatrixRequestError, IntentError
|
from mautrix.errors import MatrixRequestError, IntentError
|
||||||
from mautrix.appservice import AppService, IntentAPI
|
from mautrix.appservice import AppService, IntentAPI
|
||||||
@@ -222,7 +222,18 @@ class BasePortal(MautrixBasePortal, ABC):
|
|||||||
return config[f"bridge.{key}"]
|
return config[f"bridge.{key}"]
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _get_largest_photo_size(photo: Union[Photo, Document]
|
def _photo_size_key(photo: TypePhotoSize) -> int:
|
||||||
|
if isinstance(photo, PhotoSize):
|
||||||
|
return photo.size
|
||||||
|
elif isinstance(photo, PhotoSizeProgressive):
|
||||||
|
return max(photo.sizes)
|
||||||
|
elif isinstance(photo, PhotoSizeEmpty):
|
||||||
|
return 0
|
||||||
|
else:
|
||||||
|
return len(photo.bytes)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def _get_largest_photo_size(cls, photo: Union[Photo, Document]
|
||||||
) -> Tuple[Optional[InputPhotoFileLocation],
|
) -> Tuple[Optional[InputPhotoFileLocation],
|
||||||
Optional[TypePhotoSize]]:
|
Optional[TypePhotoSize]]:
|
||||||
if not photo or isinstance(photo, PhotoEmpty) or (isinstance(photo, Document)
|
if not photo or isinstance(photo, PhotoEmpty) or (isinstance(photo, Document)
|
||||||
@@ -230,9 +241,7 @@ class BasePortal(MautrixBasePortal, ABC):
|
|||||||
return None, None
|
return None, None
|
||||||
|
|
||||||
largest = max(photo.thumbs if isinstance(photo, Document) else photo.sizes,
|
largest = max(photo.thumbs if isinstance(photo, Document) else photo.sizes,
|
||||||
key=(lambda photo2: (len(photo2.bytes)
|
key=cls._photo_size_key)
|
||||||
if not isinstance(photo2, PhotoSize)
|
|
||||||
else photo2.size)))
|
|
||||||
return InputPhotoFileLocation(
|
return InputPhotoFileLocation(
|
||||||
id=photo.id,
|
id=photo.id,
|
||||||
access_hash=photo.access_hash,
|
access_hash=photo.access_hash,
|
||||||
|
|||||||
@@ -109,8 +109,7 @@ class PortalTelegram(BasePortal, ABC):
|
|||||||
return await self._send_message(intent, content, timestamp=evt.date)
|
return await self._send_message(intent, content, timestamp=evt.date)
|
||||||
info = ImageInfo(
|
info = ImageInfo(
|
||||||
height=largest_size.h, width=largest_size.w, orientation=0, mimetype=file.mime_type,
|
height=largest_size.h, width=largest_size.w, orientation=0, mimetype=file.mime_type,
|
||||||
size=(len(largest_size.bytes) if (isinstance(largest_size, PhotoCachedSize))
|
size=self._photo_size_key(largest_size))
|
||||||
else largest_size.size))
|
|
||||||
ext = sane_mimetypes.guess_extension(file.mime_type)
|
ext = sane_mimetypes.guess_extension(file.mime_type)
|
||||||
name = f"disappearing_image{ext}" if media.ttl_seconds else f"image{ext}"
|
name = f"disappearing_image{ext}" if media.ttl_seconds else f"image{ext}"
|
||||||
await intent.set_typing(self.mxid, is_typing=False)
|
await intent.set_typing(self.mxid, is_typing=False)
|
||||||
|
|||||||
Reference in New Issue
Block a user