mirror of
https://github.com/pyrogram/pyrogram
synced 2025-09-08 10:15:10 +00:00
Bring back automatic mime type detection for new uploads (fixes #239)
- Add mime.types file to contain a good database of type -> ext mappings from svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types - Load mime.type at BaseClient creation and add two convenience methods for guessing mime types from filenames and extensions from mime types, guess_mime_type and guess_extension - Make all send_* method as well as download_media use the new mime type database via guess_mime_type and guess_extension methods
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import os
|
||||
import platform
|
||||
import re
|
||||
from queue import Queue
|
||||
@@ -67,6 +68,20 @@ class BaseClient:
|
||||
13: "video_note"
|
||||
}
|
||||
|
||||
mime_types_to_extensions = {}
|
||||
extensions_to_mime_types = {}
|
||||
|
||||
with open("{}/mime.types".format(os.path.dirname(__file__)), "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 = [".{}".format(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):
|
||||
self.is_bot = None
|
||||
self.dc_id = None
|
||||
@@ -132,3 +147,9 @@ class BaseClient:
|
||||
|
||||
def answer_inline_query(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def guess_mime_type(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def guess_extension(self, *args, **kwargs):
|
||||
pass
|
||||
|
Reference in New Issue
Block a user