Use mautrix-python magic wrapper. Fixes #594

This commit is contained in:
Tulir Asokan
2022-09-17 15:00:49 +03:00
parent 80834ccec1
commit 65bd7fcc49
2 changed files with 6 additions and 10 deletions
+2 -3
View File
@@ -132,7 +132,6 @@ from telethon.tl.types import (
UserProfilePhotoEmpty,
)
from telethon.utils import encode_waveform
import magic
from mautrix.appservice import DOUBLE_PUPPET_SOURCE_KEY, IntentAPI
from mautrix.bridge import BasePortal, NotificationDisabler, RejectMatrixInvite, async_getter_lock
@@ -167,7 +166,7 @@ from mautrix.types import (
UserID,
VideoInfo,
)
from mautrix.util import variation_selector
from mautrix.util import magic, variation_selector
from mautrix.util.message_send_checkpoint import MessageSendCheckpointStatus
from mautrix.util.simple_lock import SimpleLock
from mautrix.util.simple_template import SimpleTemplate
@@ -2405,7 +2404,7 @@ class Portal(DBPortal, BasePortal):
self.avatar_url = url
file = await self.main_intent.download_media(url)
mime = magic.from_buffer(file, mime=True)
mime = magic.mimetype(file)
ext = sane_mimetypes.guess_extension(mime)
uploaded = await sender.client.upload_file(file, file_name=f"avatar{ext}")
photo = InputChatUploadedPhoto(file=uploaded)
+4 -7
View File
@@ -42,9 +42,9 @@ from telethon.tl.types import (
PhotoSize,
TypePhotoSize,
)
import magic
from mautrix.appservice import IntentAPI
from mautrix.util import magic
from .. import abstract_user as au
from ..db import TelegramFile as DBTelegramFile
@@ -177,7 +177,7 @@ async def transfer_thumbnail_to_matrix(
else:
file = await client.download_file(thumbnail_loc)
width, height = None, None
mime_type = magic.from_buffer(file, mime=True)
mime_type = magic.mimetype(file)
decryption_info = None
upload_mime_type = mime_type
@@ -335,13 +335,10 @@ async def _unlocked_transfer_file_to_matrix(
return None
width, height = None, None
mime_type = magic.from_buffer(file, mime=True)
mime_type = magic.mimetype(file)
image_converted = False
# A weird bug in alpine/magic makes it return application/octet-stream for gzips...
is_tgs = mime_type == "application/gzip" or (
mime_type == "application/octet-stream" and magic.from_buffer(file).startswith("gzip")
)
is_tgs = mime_type == "application/gzip"
if is_sticker and tgs_convert and is_tgs:
converted_anim = await convert_tgs_to(
file, tgs_convert["target"], **tgs_convert["args"]