|
1 | 1 | import mimetypes as _mimetypes
|
2 | 2 | import os
|
3 |
| -from mimetypes import * |
4 | 3 |
|
5 | 4 | from .. import Config
|
6 | 5 |
|
7 |
| -__all__ = _mimetypes.__all__ |
| 6 | +WSB_USER_MIMETYPES = 'mime.types' |
| 7 | + |
| 8 | + |
| 9 | +def _patch_mimetypes(): |
| 10 | + patch_types_map = { |
| 11 | + # WebScrapBook related |
| 12 | + '.htz': 'application/html+zip', |
| 13 | + '.maff': 'application/x-maff', |
| 14 | + '.wsba': 'application/wsba+zip', |
| 15 | + |
| 16 | + # Some common types |
| 17 | + '.md': 'text/markdown', |
| 18 | + '.mkd': 'text/markdown', |
| 19 | + '.mkdn': 'text/markdown', |
| 20 | + '.mdwn': 'text/markdown', |
| 21 | + '.mdown': 'text/markdown', |
| 22 | + '.markdown': 'text/markdown', |
| 23 | + '.rss': 'application/rss+xml', |
| 24 | + '.atom': 'application/atom+xml', |
| 25 | + '.woff': 'font/woff', |
| 26 | + '.woff2': 'font/woff2', |
| 27 | + '.webp': 'image/webp', |
| 28 | + '.weba': 'audio/weba', |
| 29 | + '.webm': 'video/webm', |
| 30 | + '.oga': 'audio/ogg', |
| 31 | + '.ogv': 'video/ogg', |
| 32 | + '.ogx': 'application/ogg', # IANA |
| 33 | + '.ogg': 'application/ogg', # MAFF |
| 34 | + '.vtt': 'text/vtt', |
| 35 | + '.swf': 'application/x-shockwave-flash', # Apache, nginx, etc. |
| 36 | + '.jar': 'application/java-archive', |
| 37 | + '.class': 'application/java-vm', |
| 38 | + '.epub': 'application/epub+zip', |
| 39 | + '.7z': 'application/x-7z-compressed', |
| 40 | + '.rar': 'application/vnd.rar', |
| 41 | + |
| 42 | + # .js is mapped to application/javascript or application/x-javascript in some OS |
| 43 | + # ref: https://www.ietf.org/rfc/rfc9239.txt |
| 44 | + # text/javascript is mapped to .es in Debian 12 |
| 45 | + '.js': 'text/javascript', |
| 46 | + |
| 47 | + # .bmp is mapped to image/x-ms-bmp in Python < 3.11 |
| 48 | + # ref: https://github.com/python/cpython/issues/86194 |
| 49 | + '.bmp': 'image/bmp', |
| 50 | + |
| 51 | + # .ico is mapped to image/vnd.microsoft.icon in Python, |
| 52 | + # which is not actually used by Microsoft softwares and causes |
| 53 | + # a compatibility issue in IE9. |
| 54 | + # ref: https://en.wikipedia.org/wiki/ICO_%28file_format%29#MIME_type |
| 55 | + '.ico': 'image/x-icon', |
| 56 | + |
| 57 | + # .zip is mapped to application/x-zip-compressed in Windows |
| 58 | + '.zip': 'application/zip', |
| 59 | + } |
8 | 60 |
|
| 61 | + def patch_db(db): |
| 62 | + # apply the patch |
| 63 | + patch_types_map_inv = {} |
| 64 | + for ext, type in patch_types_map.items(): |
| 65 | + db.types_map[True][ext] = type |
| 66 | + patch_types_map_inv.setdefault(type, []).append(ext) |
| 67 | + for type, exts in patch_types_map_inv.items(): |
| 68 | + entry = db.types_map_inv[True].setdefault(type, []) |
| 69 | + for ext in exts: |
| 70 | + try: |
| 71 | + entry.remove(ext) |
| 72 | + except ValueError: |
| 73 | + pass |
| 74 | + entry[0:0] = exts |
9 | 75 |
|
10 |
| -# add custom user MIME types mapping |
11 |
| -_mimetypes.knownfiles += [os.path.join(Config.user_config_dir(), 'mime.types')] |
12 |
| - |
13 |
| -# WebScrapBook related |
14 |
| -_mimetypes.add_type('application/html+zip', '.htz') |
15 |
| -_mimetypes.add_type('application/x-maff', '.maff') |
16 |
| -_mimetypes.add_type('application/wsba+zip', '.wsba') |
17 |
| - |
18 |
| -# Some common types |
19 |
| -_mimetypes.add_type('text/markdown', '.md') |
20 |
| -_mimetypes.add_type('text/markdown', '.mkd') |
21 |
| -_mimetypes.add_type('text/markdown', '.mkdn') |
22 |
| -_mimetypes.add_type('text/markdown', '.mdwn') |
23 |
| -_mimetypes.add_type('text/markdown', '.mdown') |
24 |
| -_mimetypes.add_type('text/markdown', '.markdown') |
25 |
| -_mimetypes.add_type('application/rss+xml', '.rss') |
26 |
| -_mimetypes.add_type('application/atom+xml', '.atom') |
27 |
| -_mimetypes.add_type('font/woff', '.woff') |
28 |
| -_mimetypes.add_type('font/woff2', '.woff2') |
29 |
| -_mimetypes.add_type('image/webp', '.webp') |
30 |
| -_mimetypes.add_type('audio/weba', '.weba') |
31 |
| -_mimetypes.add_type('video/webm', '.webm') |
32 |
| -_mimetypes.add_type('audio/ogg', '.oga') |
33 |
| -_mimetypes.add_type('video/ogg', '.ogv') |
34 |
| -_mimetypes.add_type('application/ogg', '.ogx') # IANA |
35 |
| -_mimetypes.add_type('application/ogg', '.ogg') # MAFF |
36 |
| -_mimetypes.add_type('text/vtt', '.vtt') |
37 |
| -_mimetypes.add_type('application/x-shockwave-flash', '.swf') # Apache, nginx, etc. |
38 |
| -_mimetypes.add_type('application/java-archive', '.jar') |
39 |
| -_mimetypes.add_type('application/java-vm', '.class') |
40 |
| -_mimetypes.add_type('application/epub+zip', '.epub') |
41 |
| -_mimetypes.add_type('application/x-7z-compressed', '.7z') |
42 |
| -_mimetypes.add_type('application/vnd.rar', '.rar') |
43 |
| - |
44 |
| -# .js is mapped to application/javascript or application/x-javascript in some OS |
45 |
| -# ref: https://www.ietf.org/rfc/rfc9239.txt |
46 |
| -# text/javascript is mapped to .es in Debian 12 |
47 |
| -_mimetypes.add_type('text/javascript', '.js') |
48 |
| - |
49 |
| -# .bmp is mapped to image/x-ms-bmp in Python < 3.11 |
50 |
| -# ref: https://github.com/python/cpython/issues/86194 |
51 |
| -_mimetypes.add_type('image/bmp', '.bmp') |
52 |
| - |
53 |
| -# .ico is mapped to image/vnd.microsoft.icon in Python, |
54 |
| -# which is not actually used by Microsoft softwares and causes |
55 |
| -# a compatibility issue in IE9. |
56 |
| -# ref: https://en.wikipedia.org/wiki/ICO_%28file_format%29#MIME_type |
57 |
| -_mimetypes.add_type('image/x-icon', '.ico') |
58 |
| - |
59 |
| -# .zip is mapped to application/x-zip-compressed in Windows |
60 |
| -_mimetypes.add_type('application/zip', '.zip') |
| 76 | + # load user mappings |
| 77 | + for file in (os.path.join(Config.user_config_dir(), WSB_USER_MIMETYPES),): |
| 78 | + if os.path.isfile(file): |
| 79 | + db.read(file) |
| 80 | + |
| 81 | + if _mimetypes.inited: |
| 82 | + patch_db(_mimetypes._db) |
| 83 | + else: |
| 84 | + # patch init |
| 85 | + patched = False |
| 86 | + _init = _mimetypes.init |
| 87 | + |
| 88 | + def init(files=None): |
| 89 | + nonlocal patched |
| 90 | + _init(files) |
| 91 | + if not patched: |
| 92 | + patch_db(_mimetypes._db) |
| 93 | + patched = True |
| 94 | + |
| 95 | + _mimetypes.init = init |
| 96 | + |
| 97 | + |
| 98 | +_patch_mimetypes() |
| 99 | + |
| 100 | +# export all attributes |
| 101 | +from mimetypes import * # noqa: E402 |
| 102 | + |
| 103 | +__all__ = _mimetypes.__all__ |
0 commit comments