From 41ec1a9a2940bd9b8fab02ba492c50403a74af40 Mon Sep 17 00:00:00 2001 From: Robert Imschweiler Date: Thu, 22 Sep 2022 11:36:41 +0200 Subject: [PATCH] matrix bridge: Improve mime-type detection. --- .../bridge_with_matrix/matrix_bridge.py | 13 ++++--------- .../bridge_with_matrix/requirements.txt | 2 -- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/zulip/integrations/bridge_with_matrix/matrix_bridge.py b/zulip/integrations/bridge_with_matrix/matrix_bridge.py index dabcb00..6b7e68d 100644 --- a/zulip/integrations/bridge_with_matrix/matrix_bridge.py +++ b/zulip/integrations/bridge_with_matrix/matrix_bridge.py @@ -14,9 +14,6 @@ from concurrent.futures import ThreadPoolExecutor from io import BytesIO from typing import Any, Dict, List, Match, Optional, Set, Tuple, Type, Union -if os.name != "nt": - import magic - import magic.compat import nio from nio.responses import ( DownloadError, @@ -352,17 +349,15 @@ class ZulipToMatrix: if result["result"] != "success": success = False continue + 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: success = False continue - mimetype: str - if os.name == "nt": - mimetype = "m.file" - else: - mimetype = magic.from_buffer(file_content, mime=True) filename: str = file.split("/")[-1] response, _ = await self.matrix_client.upload( diff --git a/zulip/integrations/bridge_with_matrix/requirements.txt b/zulip/integrations/bridge_with_matrix/requirements.txt index 08656cf..45eefe2 100644 --- a/zulip/integrations/bridge_with_matrix/requirements.txt +++ b/zulip/integrations/bridge_with_matrix/requirements.txt @@ -1,3 +1 @@ matrix-nio -python-magic -python-magic-bin; platform_system == "Windows"