-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmime
226 lines (208 loc) · 3.98 KB
/
mime
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#! /bin/sh
# mime2ext(1): maps MIME types to file extensions
#
# Yves Arrouye
: ${TMPDIR:=/tmp}
me=`basename $0`
default_MIMETYPES1=/etc/apache2/mime.types
default_MIMETYPES2=/usr/share/cups/mime/mime.types
if [ -f "$default_MIMETYPES1" ]
then
default_MIMETYPES=$default_MIMETYPES1
else
if [ -f "$default_MIMETYPES2" ]
then
default_MIMETYPES=$default_MIMETYPES2
else
default_MIMETYPES=
fi
fi
usage() {
if [ "$1" -eq 0 ]
then
u=U
else
u=u
exec >&2
fi
echo $u"sage: $me [--help] [--mime-types file] [-g, --guess] [-C] [-E] [-h | --no-filename] [-m glob-pattern] [file ...]"
if [ "$1" -eq 0 ]
then
if [ -n "$default_MIMETYPES" ]
then
ordefmimetypesmsg=" or $default_MIMETYPES1 or $default_MIMETYPES2"
fi
cat <<EOF
--help Show this message
--mime-types file Use given file to map MIME types to file extensions (defaults to \$MIMETYPES$ordefmimetypesmsg)
-g, --guess Double guess/check some broad MIME types
-C Do not print charset information
-E Do not print file extensions
-h, --no-filename Do not print filename
-m glob-pattern Exit with 0 if the base MIME type matches glob-pattern for all files, with 1 otherwise
EOF
fi
exit $1
}
while [ $# -ne 0 ]
do
case "$1" in
-[^-]*)
for l in `echo $1 | sed -e 's/^-//' -e 's/\(.\)/\1 /g'`
do
case "$l" in
g)
guess=yes
;;
C)
nocharset=yes
;;
E)
nofileexts=yes
;;
h)
nofilename=yes
;;
m)
if [ $# -lt 2 ]
then
usage 1
fi
shift
pattern=$1
;;
*)
usage 1
;;
esac
done
;;
--help)
usage 0
;;
--mime-types)
if [ $# -lt 2 ]
then
usage 1
fi
shift
MIMETYPES=$1
;;
--guess)
guess=yes
;;
--no-filename)
nofilename=yes
;;
--*)
usage 1
;;
*)
break
;;
esac
shift
done
if [ -n "$MIMETYPES" -o -f "$default_MIMETYPES" ]
then
: ${MIMETYPES:=$default_MIMETYPES}
if [ ! -r "$MIMETYPES" ]
then
>&2 echo "$me: cannot open MIME types file: $MIMETYPES"
exit 2
fi
fi
if [ $# -eq 0 ]
then
extrain=-
fi
res=0
for infile in "$@" $extrain
do
infile=${infile--}
if [ "$infile" = - ]
then
tmpfile=`mktemp $TMPDIR/$me.XXXXXX`
trap "rm -f $tmpfile 2>/dev/null" 0
cat >$tmpfile
infile=$tmpfile
fi
fullmimetype=`file -bI $infile`
if [ -n "$guess" ]
then
case "$fullmimetype" in
text/plain*)
replmimetype=
if head -1 $infile | grep -q '^[ ]*[{[]'
then
replmimetype=application/json
else
if head -1 $infile | grep -q '^HTTP/[0-9][0-9]*\.[0-9][0-9]* [0-9][0-9]*'
then
replmimetype=text/x-http
fi
fi
if [ -n "$replmimetype" ]
then
escreplmimetype=`echo $replmimetype | sed 's,/,\\\\/,g'`
fullmimetype=`echo "$fullmimetype" | sed "s,^text/plain,$escreplmimetype,"`
fi
;;
esac
fi
mimetype=`echo $fullmimetype | sed 's/;.*$//'`
if [ -r "$MIMETYPES" -a -z "$nofileexts" ]
then
mimeerrormsg="cannot find MIME type in $MIMETYPES"
fileexts=`sed -n "s,^$mimetype"'[^a-z0-9]*\(.*\).*,\1,p' $MIMETYPES | sed 's/[a-z0-9]*(.*//'`
else
mimeerrormsg="cannot determine extension for MIME type"
case $mimetype in
text/html)
fileexts=html htm
;;
text/plain)
fileexts=txt
;;
esac
fi
if [ -n "$nofileexts" ]
then
fileexts=
else
fileexts=" $fileexts"
fi
if [ -z "$pattern" ]
then
if [ $# -gt 1 -a -z "$nofilename" ]
then
header="$infile:"
else
header=
fi
if [ -z "$nocharset" ]
then
printmimetype=$fullmimetype
else
printmimetype=$mimetype
fi
echo "$header$printmimetype$fileexts"
else
case "$mimetype" in
$pattern)
;;
*)
res=1
;;
esac
fi
if [ -f "$tmpfile" ]
then
rm -f $tmpfile
fi
if [ $res -eq 1 ]
then
break
fi
done
exit $res