-
Notifications
You must be signed in to change notification settings - Fork 2
/
mime.v
76 lines (55 loc) · 1.39 KB
/
mime.v
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
module mime
import filepath
pub struct MimeType {
pub:
source string
extensions []string
compressible bool [skip]
charset string [skip]
}
struct Db {
pub:
db map[string]MimeType
}
fn is_mime(text string) bool {
_text := text.split('/')
if _text.len != 2 { return false }
return true
}
pub fn load() Db {
return Db{load_data()}
}
pub fn (mdb Db) charset(text string) string {
if !is_mime(text) && mdb.db[text.to_lower()].charset.len == 0 {
return ''
}
return mdb.db[text.to_lower()].charset
}
pub fn (mdb Db) content_type(text string) string {
mime := if !is_mime(text) { mdb.lookup(text) } else { text }
if mdb.db[mime].charset.len != 0 {
_charset := mdb.charset(mime)
if _charset.len != 0 {
return mime + '; charset=${_charset.to_lower()}'
}
}
return mime
}
pub fn (mdb Db) extension(text string) string {
_type := mdb.db[text.to_lower()]
if !is_mime(text) || _type.extensions.len == 0 { return '' }
return _type.extensions[0]
}
pub fn (mdb Db) lookup(path string) string {
path_ext := filepath.ext('x.${path}').to_lower()
extension := path_ext[1..path_ext.len]
if extension.len == 0 { return '' }
for k, v in mdb.db {
for x in v.extensions {
if x == extension {
return k
}
}
}
return ''
}