Make max photo size before sending as file configurable. Fixes #141

This commit is contained in:
Tulir Asokan
2019-02-16 17:14:02 +02:00
parent be255ec7af
commit 5af045844e
5 changed files with 11 additions and 5 deletions
+2
View File
@@ -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
+1
View File
@@ -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):
+1 -1
View File
@@ -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
+5 -2
View File
@@ -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,
+2 -2
View File
@@ -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 []