-
Notifications
You must be signed in to change notification settings - Fork 4
/
mime.tcl
77 lines (73 loc) · 1.84 KB
/
mime.tcl
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
# MIME type detection by filename extension.
# Copyright (c) 2014-2016 D. Bohdan.
# License: MIT.
namespace eval ::mime {
variable version 1.2.0
variable mimeDataInverted {
text/plain {
makefile
COPYING
LICENSE
README
Makefile
.c
.conf
.h
.log
.md
.sh
.tcl
.terms
.tm
.txt
.wiki
.LICENSE
.README
}
text/css .css
text/csv .csv
image/gif .gif
application/gzip .gz
text/html {
.htm
.html
}
image/jpeg {
.jpg
.jpeg
}
application/javascript .js
application/json .json
application/pdf .pdf
image/png .png
application/postscript .ps
application/xhtml .xhtml
application/xml .xml
application/zip .zip
}
variable byFilename {}
variable byExtension {}
foreach {mimeType files} $mimeDataInverted {
foreach file $files {
if {[string index $file 0] eq "."} {
lappend byExtension $file $mimeType
} else {
lappend byFilename $file $mimeType
}
}
}
unset mimeDataInverted
}
proc ::mime::type {filename} {
variable byFilename
variable byExtension
set tail [file tail $filename]
set ext [file extension $filename]
if {[dict exists $byFilename $tail]} {
return [dict get $byFilename $tail]
} elseif {[dict exists $byExtension $ext]} {
return [dict get $byExtension $ext]
} else {
return application/octet-stream
}
}