From 5af045844ea6f9998aeb3d6a140b86d92c681cd3 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sat, 16 Feb 2019 17:14:02 +0200 Subject: [PATCH] Make max photo size before sending as file configurable. Fixes #141 --- example-config.yaml | 2 ++ mautrix_telegram/config.py | 1 + mautrix_telegram/db/user.py | 2 +- mautrix_telegram/portal.py | 7 +++++-- mautrix_telegram/tgclient.py | 4 ++-- 5 files changed, 11 insertions(+), 5 deletions(-) diff --git a/example-config.yaml b/example-config.yaml index e1c46fa9..9840d2cd 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -144,6 +144,8 @@ bridge: # Use inline images instead of a separate message for the caption. # N.B. Inline images are not supported on all clients (e.g. Riot iOS). inline_images: false + # Maximum size of image in megabytes before sending to Telegram as a document. + image_as_file_size: 10 # Whether to bridge Telegram bot messages as m.notices or m.texts. bot_messages_as_notices: true diff --git a/mautrix_telegram/config.py b/mautrix_telegram/config.py index 9e27773b..c5ad8c4f 100644 --- a/mautrix_telegram/config.py +++ b/mautrix_telegram/config.py @@ -206,6 +206,7 @@ class Config(DictWithRecursion): copy("bridge.sync_with_custom_puppets") copy("bridge.telegram_link_preview") copy("bridge.inline_images") + copy("bridge.image_as_file_size") copy("bridge.bot_messages_as_notices") if isinstance(self["bridge.bridge_notices"], bool): diff --git a/mautrix_telegram/db/user.py b/mautrix_telegram/db/user.py index 1bcd9c36..e7afbffb 100644 --- a/mautrix_telegram/db/user.py +++ b/mautrix_telegram/db/user.py @@ -18,7 +18,7 @@ from sqlalchemy import Column, ForeignKey, ForeignKeyConstraint, Integer, String from sqlalchemy.engine.result import RowProxy from typing import Optional, Iterable, Tuple -from ..types import MatrixUserID, MatrixRoomID, TelegramID +from ..types import MatrixUserID, TelegramID from .base import Base diff --git a/mautrix_telegram/portal.py b/mautrix_telegram/portal.py index 357d5599..42d1710a 100644 --- a/mautrix_telegram/portal.py +++ b/mautrix_telegram/portal.py @@ -610,7 +610,8 @@ class Portal: return False @staticmethod - def _get_largest_photo_size(photo: Union[Photo, List[TypePhotoSize]]) -> Optional[TypePhotoSize]: + def _get_largest_photo_size(photo: Union[Photo, List[TypePhotoSize]] + ) -> Optional[TypePhotoSize]: if not photo: return None return max(photo.sizes if isinstance(photo, Photo) else photo, key=(lambda photo2: ( @@ -983,7 +984,9 @@ class Portal: caption = message["body"] if message["body"].lower() != file_name.lower() else None - media = await client.upload_file_direct(file, mime, attributes, file_name) + media = await client.upload_file_direct( + file, mime, attributes, file_name, + max_image_size=config["bridge.image_as_file_size"] * 1000 ** 2) lock = self.require_send_lock(sender_id) async with lock: response = await client.send_media(self.peer, media, reply_to=reply_to, diff --git a/mautrix_telegram/tgclient.py b/mautrix_telegram/tgclient.py index a33f16d6..db8920d8 100644 --- a/mautrix_telegram/tgclient.py +++ b/mautrix_telegram/tgclient.py @@ -27,11 +27,11 @@ from telethon.tl.patched import Message class MautrixTelegramClient(TelegramClient): async def upload_file_direct(self, file: bytes, mime_type: str = None, attributes: List[TypeDocumentAttribute] = None, - file_name: str = None + file_name: str = None, max_image_size: float = 10 * 1000 ** 2, ) -> Union[InputMediaUploadedDocument, InputMediaUploadedPhoto]: file_handle = await super().upload_file(file, file_name=file_name, use_cache=False) - if (mime_type == "image/png" or mime_type == "image/jpeg") and len(file) < 10 * 1000 ** 2: + if (mime_type == "image/png" or mime_type == "image/jpeg") and len(file) < max_image_size: return InputMediaUploadedPhoto(file_handle) else: attributes = attributes or []