From 58d572162de39f1bec7e861eacbfb9b959f54f49 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sat, 27 Jan 2018 21:57:14 +0200 Subject: [PATCH] Convert stickers to png --- README.md | 2 +- mautrix_telegram/portal.py | 27 ++++++++++++++++++++++----- requirements.txt | 1 + 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 74a0c65f..49fc828a 100644 --- a/README.md +++ b/README.md @@ -86,7 +86,7 @@ does not do this automatically.~~ * [x] Forwards * [x] Images * [x] Locations - * [ ] Stickers (only works if client supports webp, need converter) + * [x] Stickers * [x] Audio messages * [x] Video messages * [x] Documents diff --git a/mautrix_telegram/portal.py b/mautrix_telegram/portal.py index 4f1c8c8e..970611c4 100644 --- a/mautrix_telegram/portal.py +++ b/mautrix_telegram/portal.py @@ -17,6 +17,8 @@ from telethon.tl.functions.messages import GetFullChatRequest, EditChatAdminRequ from telethon.tl.functions.channels import GetParticipantsRequest from telethon.errors.rpc_error_list import ChatAdminRequiredError from telethon.tl.types import * +from PIL import Image +from io import BytesIO import mimetypes import magic from .db import Portal as DBPortal, Message as DBMessage @@ -300,18 +302,31 @@ class Portal: name = media.caption sender.intent.send_image(self.mxid, uploaded["content_uri"], info=info, text=name) + @staticmethod + def convert_webp(file, to="png"): + image = Image.open(BytesIO(file)).convert("RGBA") + file = BytesIO() + image.save(file, to) + return file.getvalue() + def handle_telegram_document(self, source, sender, media): file = source.download_file(media.document) mime_type = magic.from_buffer(file, mime=True) + dont_change_mime = False + if mime_type == "image/webp": + file = self.convert_webp(file, to="png") + mime_type = "image/png" + dont_change_mime = True uploaded = sender.intent.upload_file(file, mime_type) name = media.caption - if not name: - for attr in media.document.attributes: - if isinstance(attr, DocumentAttributeFilename): - name = attr.file_name + for attr in media.document.attributes: + if not name and isinstance(attr, DocumentAttributeFilename): + name = attr.file_name + if not dont_change_mime: (mime_from_name, _) = mimetypes.guess_type(name) mime_type = mime_from_name or mime_type - break + elif isinstance(attr, DocumentAttributeSticker): + name = f"Sticker for {attr.alt}" mime_type = media.document.mime_type or mime_type info = { "size": media.document.size, @@ -322,6 +337,8 @@ class Portal: type = "m.video" elif mime_type.startswith("audio/"): type = "m.audio" + elif mime_type.startswith("image/"): + type = "m.image" sender.intent.send_file(self.mxid, uploaded["content_uri"], info=info, text=name, type=type) diff --git a/requirements.txt b/requirements.txt index eb6c7eb1..34771da1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,4 @@ python-magic SQLAlchemy Telethon Markdown +Pillow