forked from adamlaska/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmime.py
More file actions
27 lines (23 loc) · 853 Bytes
/
mime.py
File metadata and controls
27 lines (23 loc) · 853 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Copyright The IETF Trust 2020, All Rights Reserved
# -*- coding: utf-8 -*-
import magic
import re
def get_mime_type(content):
# try to fixup encoding
if hasattr(magic, "open"):
m = magic.open(magic.MAGIC_MIME)
m.load()
filetype = m.buffer(content)
else:
m = magic.Magic()
m.cookie = magic.magic_open(magic.MAGIC_NONE | magic.MAGIC_MIME | magic.MAGIC_MIME_ENCODING)
magic.magic_load(m.cookie, None)
filetype = m.from_buffer(content)
# Work around silliness in libmagic on OpenSUSE 15.1
filetype = filetype.replace('text/x-Algol68;', 'text/plain;')
if ';' in filetype and 'charset=' in filetype:
mimetype, charset = re.split('; *charset=', filetype)
else:
mimetype = re.split(';', filetype)[0]
charset = 'utf-8'
return mimetype, charset