Fix handling thumbnails of documents. Fixes #281
This commit is contained in:
@@ -598,8 +598,8 @@ class Portal:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _get_largest_photo_size(photo: Photo) -> TypePhotoSize:
|
def _get_largest_photo_size(photo: Union[Photo, List[TypePhotoSize]]) -> TypePhotoSize:
|
||||||
return max(photo.sizes, key=(lambda photo2: (
|
return max(photo.sizes if isinstance(photo, Photo) else photo, key=(lambda photo2: (
|
||||||
len(photo2.bytes) if not isinstance(photo2, PhotoSize) else photo2.size)))
|
len(photo2.bytes) if not isinstance(photo2, PhotoSize) else photo2.size)))
|
||||||
|
|
||||||
async def remove_avatar(self, _: 'AbstractUser', save: bool = False) -> None:
|
async def remove_avatar(self, _: 'AbstractUser', save: bool = False) -> None:
|
||||||
@@ -1352,8 +1352,8 @@ class Portal:
|
|||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _parse_telegram_document_meta(evt: Message, file: DBTelegramFile, attrs: Dict
|
def _parse_telegram_document_meta(evt: Message, file: DBTelegramFile, attrs: Dict,
|
||||||
) -> Tuple[Dict, str]:
|
thumb: TypePhotoSize) -> Tuple[Dict, str]:
|
||||||
document = evt.media.document
|
document = evt.media.document
|
||||||
name = evt.message or attrs["name"]
|
name = evt.message or attrs["name"]
|
||||||
if attrs["is_sticker"]:
|
if attrs["is_sticker"]:
|
||||||
@@ -1381,8 +1381,8 @@ class Portal:
|
|||||||
info["thumbnail_url"] = file.thumbnail.mxc
|
info["thumbnail_url"] = file.thumbnail.mxc
|
||||||
info["thumbnail_info"] = {
|
info["thumbnail_info"] = {
|
||||||
"mimetype": file.thumbnail.mime_type,
|
"mimetype": file.thumbnail.mime_type,
|
||||||
"h": file.thumbnail.height or document.thumb.h,
|
"h": file.thumbnail.height or thumb.h,
|
||||||
"w": file.thumbnail.width or document.thumb.w,
|
"w": file.thumbnail.width or thumb.w,
|
||||||
"size": file.thumbnail.size,
|
"size": file.thumbnail.size,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1393,12 +1393,16 @@ class Portal:
|
|||||||
document = evt.media.document
|
document = evt.media.document
|
||||||
attrs = self._parse_telegram_document_attributes(document.attributes)
|
attrs = self._parse_telegram_document_attributes(document.attributes)
|
||||||
|
|
||||||
file = await util.transfer_file_to_matrix(source.client, intent, document,
|
thumb = self._get_largest_photo_size(document.thumbs)
|
||||||
document.thumb, is_sticker=attrs["is_sticker"])
|
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:
|
if not file:
|
||||||
return None
|
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)
|
await intent.set_typing(self.mxid, is_typing=False)
|
||||||
|
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ import asyncio
|
|||||||
import magic
|
import magic
|
||||||
from sqlalchemy.exc import IntegrityError, InvalidRequestError
|
from sqlalchemy.exc import IntegrityError, InvalidRequestError
|
||||||
|
|
||||||
from telethon.tl.types import (Document, FileLocation, InputFileLocation,
|
from telethon.tl.types import (Document, FileLocation, InputFileLocation, InputDocumentFileLocation,
|
||||||
InputDocumentFileLocation, PhotoSize, PhotoCachedSize)
|
TypePhotoSize, PhotoSize, PhotoCachedSize)
|
||||||
from telethon.errors import (AuthBytesInvalidError, AuthKeyInvalidError, LocationInvalidError,
|
from telethon.errors import (AuthBytesInvalidError, AuthKeyInvalidError, LocationInvalidError,
|
||||||
SecurityError)
|
SecurityError)
|
||||||
from mautrix_appservice import IntentAPI
|
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,
|
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]:
|
is_sticker: bool = False) -> Optional[DBTelegramFile]:
|
||||||
location_id = _location_to_id(location)
|
location_id = _location_to_id(location)
|
||||||
if not location_id:
|
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,
|
async def _unlocked_transfer_file_to_matrix(client: MautrixTelegramClient, intent: IntentAPI,
|
||||||
loc_id: str, location: TypeLocation,
|
loc_id: str, location: TypeLocation,
|
||||||
thumbnail: Optional[TypeLocation],
|
thumbnail: Optional[Union[TypeLocation, TypePhotoSize]],
|
||||||
is_sticker: bool) -> Optional[DBTelegramFile]:
|
is_sticker: bool) -> Optional[DBTelegramFile]:
|
||||||
db_file = DBTelegramFile.get(loc_id)
|
db_file = DBTelegramFile.get(loc_id)
|
||||||
if db_file:
|
if db_file:
|
||||||
|
|||||||
Reference in New Issue
Block a user