Only send PNGs as images

This commit is contained in:
Tulir Asokan
2018-01-28 21:35:04 +02:00
parent 28593ea50c
commit 9ebc7f5d57
2 changed files with 16 additions and 5 deletions
+15 -4
View File
@@ -26,6 +26,8 @@ import magic
from .db import Portal as DBPortal, Message as DBMessage
from . import puppet as p, user as u, formatter
mimetypes.init()
config = None
@@ -233,6 +235,18 @@ class Portal:
# endregion
# region Matrix event handling
def _get_file_meta(self, body, mime):
file_name = None
try:
current_extension = body[body.rindex("."):]
if mimetypes.types_map[current_extension] == mime:
file_name = body
else:
file_name = f"matrix_upload{mimetypes.guess_extension(mime)}"
except (ValueError, KeyError):
file_name = f"matrix_upload{mimetypes.guess_extension(mime)}"
return file_name, None if file_name == body else body
def handle_matrix_message(self, sender, message, event_id):
type = message["msgtype"]
if type == "m.text":
@@ -252,12 +266,9 @@ class Portal:
file = self.main_intent.download_file(message["url"])
info = message["info"]
body = message["body"]
mime = info["mimetype"]
extension = mimetypes.guess_extension(mime)
file_name = body if body.endswith(extension) else f"matrix_upload{extension}"
caption = None if file_name == body else body
file_name, caption = self._get_file_meta(message["body"], mime)
attributes = [DocumentAttributeFilename(file_name=file_name)]
if "w" in info and "h" in info:
+1 -1
View File
@@ -147,7 +147,7 @@ class User:
file_handle = self.client.upload_file(file, file_name=file_name, use_cache=False)
if mime_type.startswith("image/"):
if mime_type == "image/png":
media = InputMediaUploadedPhoto(file_handle, caption or "")
else:
attr_dict = {type(attr): attr for attr in attributes}