From 88769a95b73a4c07bcd4c5697ba3a36ba0a9f0fc Mon Sep 17 00:00:00 2001 From: Sijmen Schoon Date: Thu, 5 Jan 2017 16:25:27 +0000 Subject: [PATCH] Hardcode images to jpeg For some reason, Telegram now sets the mimetype of images to application/octet-stream, breaking the extension detection. Hardcoding it to jpeg is a dirty but effective solution. --- telematrix/__init__.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/telematrix/__init__.py b/telematrix/__init__.py index 9827a37..cb1980a 100644 --- a/telematrix/__init__.py +++ b/telematrix/__init__.py @@ -376,15 +376,14 @@ def send_matrix_message(room_id, user_id, txn_id, **kwargs): async def upload_tgfile_to_matrix(file_id, user_id): file_path = (await TG_BOT.get_file(file_id))['file_path'] request = await TG_BOT.download_file(file_path) - mimetype = request.headers['Content-Type'] data = await request.read() - j = await matrix_post('media', 'upload', user_id, data, mimetype) + j = await matrix_post('media', 'upload', user_id, data, 'image/jpeg') if 'content_uri' in j: - return j['content_uri'], mimetype, len(data) + return j['content_uri'], len(data) else: - return None, None, 0 + return None, 0 async def register_join_matrix(chat, room_id, user_id): @@ -399,7 +398,7 @@ async def register_join_matrix(chat, room_id, user_id): profile_photos = await TG_BOT.get_user_profile_photos(chat.sender['id']) try: pp_file_id = profile_photos['result']['photos'][0][-1]['file_id'] - pp_uri, _, _ = await upload_tgfile_to_matrix(pp_file_id, user_id) + pp_uri, _ = await upload_tgfile_to_matrix(pp_file_id, user_id) if pp_uri: await matrix_put('client', 'profile/{}/avatar_url'.format(user_id), user_id, {'avatar_url': pp_uri}) @@ -423,11 +422,10 @@ async def aiotg_photo(chat, photo): txn_id = quote('{}:{}'.format(chat.message['message_id'], chat.id)) file_id = photo[-1]['file_id'] - uri, mime, length = await upload_tgfile_to_matrix(file_id, user_id) - info = {'mimetype': mime, 'size': length, 'h': photo[-1]['height'], + uri, length = await upload_tgfile_to_matrix(file_id, user_id) + info = {'mimetype': 'image/jpeg', 'size': length, 'h': photo[-1]['height'], 'w': photo[-1]['width']} - body = 'Image_{}{}'.format(int(time() * 1000), - mimetypes.guess_extension(mime)) + body = 'Image_{}.jpg'.format(int(time() * 1000)) if uri: j = await send_matrix_message(room_id, user_id, txn_id, body=body,