summaryrefslogtreecommitdiff
path: root/modui/mime.py
blob: e9992a552ba0f930b922a97943b16be1464da69c (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from enum import Enum
from textual import log

mimemoji = {
    "audio" : "🔈",
    "video" : "🎞",
    "text"  : "📄",
    "image" : "🖼",
    "application/zip" : "🗜️",
    "application/x-zip-compressed" : "🗜️",
    "application/x-tar" : "🗄",
    "application/x-cpio" : "🗄",
    "application/x-xz" : "🗜️",
    "application/x-7z-compressed" : "🗜️",
    "application/gzip" : "🗜️",
    "application/zstd" : "🗜️",
    "application/x-rar" : "🗜️",
    "application/x-rar-compressed" : "🗜️",
    "application/vnd.ms-cab-compressed" : "🗜️",
    "application/x-bzip2" : "🗜️",
    "application/x-lzip" : "🗜️",
    "application/x-iso9660-image" : "💿",
    "application/pdf" : "📕",
    "application/epub+zip" : "📕",
    "application/mxf" : "🎞",
    "application/vnd.android.package-archive" : "📦",
    "application/vnd.debian.binary-package" : "📦",
    "application/x-rpm" : "📦",
    "application/x-dosexec" : "⚙",
    "application/x-execuftable" : "⚙",
    "application/x-sharedlib" : "⚙",
    "application/java-archive" : "☕",
    "application/x-qemu-disk" : "🖴",
    "application/pgp-encrypted" : "🔏",
}

MIMECategory = Enum("MIMECategory",
    ["Archive", "Text", "AV", "Document", "Fallback"]
)

class MIMEHandler:
    def __init__(self):
        self.handlers = {
            MIMECategory.Archive : [[
                "application/zip",
                "application/x-zip-compressed",
                "application/x-tar",
                "application/x-cpio",
                "application/x-xz",
                "application/x-7z-compressed",
                "application/gzip",
                "application/zstd",
                "application/x-rar",
                "application/x-rar-compressed",
                "application/vnd.ms-cab-compressed",
                "application/x-bzip2",
                "application/x-lzip",
                "application/x-iso9660-image",
                "application/vnd.android.package-archive",
                "application/vnd.debian.binary-package",
                "application/x-rpm",
                "application/java-archive",
                "application/vnd.openxmlformats"
            ], []],
            MIMECategory.Text : [[
                "text",
                "application/json",
            ], []],
            MIMECategory.AV : [[
                "audio", "video", "image",
                "application/mxf"
            ], []],
            MIMECategory.Document : [[
                "application/pdf",
                "application/epub",
                "application/x-mobipocket-ebook",
            ], []],
            MIMECategory.Fallback : [[], []]
        }

        self.exceptions = {
            MIMECategory.Archive : {
                ".cbz" : MIMECategory.Document,
                ".xps" : MIMECategory.Document,
                ".epub" : MIMECategory.Document,
            },
            MIMECategory.Text : {
                ".fb2" : MIMECategory.Document,
            }
        }

    def register(self, category, handler):
        self.handlers[category][1].append(handler)

    def handle(self, mime, ext):
        def getcat(s):
            cat = MIMECategory.Fallback
            for k, v in self.handlers.items():
                s = s.split(";")[0]
                if s in v[0] or s.split("/")[0] in v[0]:
                    cat = k
                    break

                for x in v[0]:
                    if s.startswith(x):
                        cat = k
                        break

            if cat in self.exceptions:
                cat = self.exceptions[cat].get(ext) or cat

            return cat

        cat = getcat(mime)
        for handler in self.handlers[cat][1]:
            try:
                if handler(cat): return
            except: pass

        for handler in self.handlers[MIMECategory.Fallback][1]:
            try:
                if handler(None): return
            except: pass

        raise RuntimeError(f"Unhandled MIME type category: {cat}")