Simplify mime types guessing and remove unused code

This commit is contained in:
Dan 2020-12-20 17:58:35 +01:00
parent 29fa3ec520
commit 18b3ca1892
2 changed files with 6 additions and 35 deletions

View File

@ -1045,12 +1045,8 @@ class Client(Methods, Scaffold):
else: else:
return file_name return file_name
def guess_mime_type(self, filename: str): def guess_mime_type(self, filename: str) -> Optional[str]:
extension = os.path.splitext(filename)[1] return self.mimetypes.guess_type(filename)[0]
return self.extensions_to_mime_types.get(extension)
def guess_extension(self, mime_type: str): def guess_extension(self, mime_type: str) -> Optional[str]:
extensions = self.mime_types_to_extensions.get(mime_type) return self.mimetypes.guess_extension(mime_type)
if extensions:
return extensions.split(" ")[0]

View File

@ -21,6 +21,7 @@ import os
import platform import platform
import re import re
import sys import sys
from mimetypes import MimeTypes
from pathlib import Path from pathlib import Path
import pyrogram import pyrogram
@ -45,33 +46,7 @@ class Scaffold:
PARSE_MODES = ["combined", "markdown", "md", "html", None] PARSE_MODES = ["combined", "markdown", "md", "html", None]
MEDIA_TYPE_ID = { mimetypes = MimeTypes((f"{os.path.dirname(__file__)}/mime.types",))
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)
def __init__(self): def __init__(self):
try: try: