Simplify mime types guessing and remove unused code
This commit is contained in:
parent
29fa3ec520
commit
18b3ca1892
@ -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)
|
||||
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user