From 18b3ca18929ad05609c516070dfd50f828d3ae87 Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 20 Dec 2020 17:58:35 +0100 Subject: [PATCH] Simplify mime types guessing and remove unused code --- pyrogram/client.py | 12 ++++-------- pyrogram/scaffold.py | 29 ++--------------------------- 2 files changed, 6 insertions(+), 35 deletions(-) diff --git a/pyrogram/client.py b/pyrogram/client.py index a7acaed6..68afabf6 100644 --- a/pyrogram/client.py +++ b/pyrogram/client.py @@ -1045,12 +1045,8 @@ class Client(Methods, Scaffold): else: return file_name - def guess_mime_type(self, filename: str): - extension = os.path.splitext(filename)[1] - return self.extensions_to_mime_types.get(extension) + def guess_mime_type(self, filename: str) -> Optional[str]: + return self.mimetypes.guess_type(filename)[0] - def guess_extension(self, mime_type: str): - extensions = self.mime_types_to_extensions.get(mime_type) - - if extensions: - return extensions.split(" ")[0] + def guess_extension(self, mime_type: str) -> Optional[str]: + return self.mimetypes.guess_extension(mime_type) diff --git a/pyrogram/scaffold.py b/pyrogram/scaffold.py index a118955c..395ad7d3 100644 --- a/pyrogram/scaffold.py +++ b/pyrogram/scaffold.py @@ -21,6 +21,7 @@ import os import platform import re import sys +from mimetypes import MimeTypes from pathlib import Path import pyrogram @@ -45,33 +46,7 @@ class Scaffold: PARSE_MODES = ["combined", "markdown", "md", "html", None] - MEDIA_TYPE_ID = { - 0: "photo_thumbnail", - 1: "chat_photo", - 2: "photo", - 3: "voice", - 4: "video", - 5: "document", - 8: "sticker", - 9: "audio", - 10: "animation", - 13: "video_note", - 14: "document_thumbnail" - } - - mime_types_to_extensions = {} - extensions_to_mime_types = {} - - with open(f"{os.path.dirname(__file__)}/mime.types", "r", encoding="UTF-8") as f: - for match in re.finditer(r"^([^#\s]+)\s+(.+)$", f.read(), flags=re.M): - mime_type, extensions = match.groups() - - extensions = [f".{ext}" for ext in extensions.split(" ")] - - for ext in extensions: - extensions_to_mime_types[ext] = mime_type - - mime_types_to_extensions[mime_type] = " ".join(extensions) + mimetypes = MimeTypes((f"{os.path.dirname(__file__)}/mime.types",)) def __init__(self): try: