matrix bridge: Improve mime-type detection.

This commit is contained in:
Robert Imschweiler 2022-09-22 11:36:41 +02:00 committed by Tim Abbott
parent 92120914f8
commit 41ec1a9a29
2 changed files with 4 additions and 11 deletions

View file

@ -14,9 +14,6 @@ from concurrent.futures import ThreadPoolExecutor
from io import BytesIO from io import BytesIO
from typing import Any, Dict, List, Match, Optional, Set, Tuple, Type, Union from typing import Any, Dict, List, Match, Optional, Set, Tuple, Type, Union
if os.name != "nt":
import magic
import magic.compat
import nio import nio
from nio.responses import ( from nio.responses import (
DownloadError, DownloadError,
@ -352,17 +349,15 @@ class ZulipToMatrix:
if result["result"] != "success": if result["result"] != "success":
success = False success = False
continue continue
try: try:
file_content: bytes = urllib.request.urlopen(self.server_url + result["url"]).read() with urllib.request.urlopen(self.server_url + result["url"]) as response:
file_content: bytes = response.read()
mimetype: str = response.headers.get_content_type()
except Exception: except Exception:
success = False success = False
continue continue
mimetype: str
if os.name == "nt":
mimetype = "m.file"
else:
mimetype = magic.from_buffer(file_content, mime=True)
filename: str = file.split("/")[-1] filename: str = file.split("/")[-1]
response, _ = await self.matrix_client.upload( response, _ = await self.matrix_client.upload(

View file

@ -1,3 +1 @@
matrix-nio matrix-nio
python-magic
python-magic-bin; platform_system == "Windows"