1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package eu.siacs.conversations.utils;
17import java.io.File;
18import java.io.FileInputStream;
19import java.io.IOException;
20import java.io.InputStream;
21import java.util.HashMap;
22import java.util.Map;
23import java.util.Properties;
24/**
25 * Utilities for dealing with MIME types.
26 * Used to implement java.net.URLConnection and android.webkit.MimeTypeMap.
27 */
28public final class MimeUtils {
29 private static final Map<String, String> mimeTypeToExtensionMap = new HashMap<String, String>();
30 private static final Map<String, String> extensionToMimeTypeMap = new HashMap<String, String>();
31 static {
32 // The following table is based on /etc/mime.types data minus
33 // chemical/* MIME types and MIME types that don't map to any
34 // file extensions. We also exclude top-level domain names to
35 // deal with cases like:
36 //
37 // mail.google.com/a/google.com
38 //
39 // and "active" MIME types (due to potential security issues).
40 // Note that this list is _not_ in alphabetical order and must not be sorted.
41 // The "most popular" extension must come first, so that it's the one returned
42 // by guessExtensionFromMimeType.
43 add("application/andrew-inset", "ez");
44 add("application/dsptype", "tsp");
45 add("application/hta", "hta");
46 add("application/mac-binhex40", "hqx");
47 add("application/mathematica", "nb");
48 add("application/msaccess", "mdb");
49 add("application/oda", "oda");
50 add("application/ogg", "ogg");
51 add("application/ogg", "oga");
52 add("application/pdf", "pdf");
53 add("application/pgp-keys", "key");
54 add("application/pgp-signature", "pgp");
55 add("application/pics-rules", "prf");
56 add("application/pkix-cert", "cer");
57 add("application/rar", "rar");
58 add("application/rdf+xml", "rdf");
59 add("application/rss+xml", "rss");
60 add("application/zip", "zip");
61 add("application/vnd.android.package-archive", "apk");
62 add("application/vnd.cinderella", "cdy");
63 add("application/vnd.ms-pki.stl", "stl");
64 add("application/vnd.oasis.opendocument.database", "odb");
65 add("application/vnd.oasis.opendocument.formula", "odf");
66 add("application/vnd.oasis.opendocument.graphics", "odg");
67 add("application/vnd.oasis.opendocument.graphics-template", "otg");
68 add("application/vnd.oasis.opendocument.image", "odi");
69 add("application/vnd.oasis.opendocument.spreadsheet", "ods");
70 add("application/vnd.oasis.opendocument.spreadsheet-template", "ots");
71 add("application/vnd.oasis.opendocument.text", "odt");
72 add("application/vnd.oasis.opendocument.text-master", "odm");
73 add("application/vnd.oasis.opendocument.text-template", "ott");
74 add("application/vnd.oasis.opendocument.text-web", "oth");
75 add("application/vnd.google-earth.kml+xml", "kml");
76 add("application/vnd.google-earth.kmz", "kmz");
77 add("application/msword", "doc");
78 add("application/msword", "dot");
79 add("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx");
80 add("application/vnd.openxmlformats-officedocument.wordprocessingml.template", "dotx");
81 add("application/vnd.ms-excel", "xls");
82 add("application/vnd.ms-excel", "xlt");
83 add("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx");
84 add("application/vnd.openxmlformats-officedocument.spreadsheetml.template", "xltx");
85 add("application/vnd.ms-powerpoint", "ppt");
86 add("application/vnd.ms-powerpoint", "pot");
87 add("application/vnd.ms-powerpoint", "pps");
88 add("application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx");
89 add("application/vnd.openxmlformats-officedocument.presentationml.template", "potx");
90 add("application/vnd.openxmlformats-officedocument.presentationml.slideshow", "ppsx");
91 add("application/vnd.rim.cod", "cod");
92 add("application/vnd.smaf", "mmf");
93 add("application/vnd.stardivision.calc", "sdc");
94 add("application/vnd.stardivision.draw", "sda");
95 add("application/vnd.stardivision.impress", "sdd");
96 add("application/vnd.stardivision.impress", "sdp");
97 add("application/vnd.stardivision.math", "smf");
98 add("application/vnd.stardivision.writer", "sdw");
99 add("application/vnd.stardivision.writer", "vor");
100 add("application/vnd.stardivision.writer-global", "sgl");
101 add("application/vnd.sun.xml.calc", "sxc");
102 add("application/vnd.sun.xml.calc.template", "stc");
103 add("application/vnd.sun.xml.draw", "sxd");
104 add("application/vnd.sun.xml.draw.template", "std");
105 add("application/vnd.sun.xml.impress", "sxi");
106 add("application/vnd.sun.xml.impress.template", "sti");
107 add("application/vnd.sun.xml.math", "sxm");
108 add("application/vnd.sun.xml.writer", "sxw");
109 add("application/vnd.sun.xml.writer.global", "sxg");
110 add("application/vnd.sun.xml.writer.template", "stw");
111 add("application/vnd.visio", "vsd");
112 add("application/x-abiword", "abw");
113 add("application/x-apple-diskimage", "dmg");
114 add("application/x-bcpio", "bcpio");
115 add("application/x-bittorrent", "torrent");
116 add("application/x-cdf", "cdf");
117 add("application/x-cdlink", "vcd");
118 add("application/x-chess-pgn", "pgn");
119 add("application/x-cpio", "cpio");
120 add("application/x-debian-package", "deb");
121 add("application/x-debian-package", "udeb");
122 add("application/x-director", "dcr");
123 add("application/x-director", "dir");
124 add("application/x-director", "dxr");
125 add("application/x-dms", "dms");
126 add("application/x-doom", "wad");
127 add("application/x-dvi", "dvi");
128 add("application/x-font", "pfa");
129 add("application/x-font", "pfb");
130 add("application/x-font", "gsf");
131 add("application/x-font", "pcf");
132 add("application/x-font", "pcf.Z");
133 add("application/x-freemind", "mm");
134 // application/futuresplash isn't IANA, so application/x-futuresplash should come first.
135 add("application/x-futuresplash", "spl");
136 add("application/futuresplash", "spl");
137 add("application/x-gnumeric", "gnumeric");
138 add("application/x-go-sgf", "sgf");
139 add("application/x-graphing-calculator", "gcf");
140 add("application/x-gtar", "tgz");
141 add("application/x-gtar", "gtar");
142 add("application/x-gtar", "taz");
143 add("application/x-hdf", "hdf");
144 add("application/x-ica", "ica");
145 add("application/x-internet-signup", "ins");
146 add("application/x-internet-signup", "isp");
147 add("application/x-iphone", "iii");
148 add("application/x-iso9660-image", "iso");
149 add("application/x-jmol", "jmz");
150 add("application/x-kchart", "chrt");
151 add("application/x-killustrator", "kil");
152 add("application/x-koan", "skp");
153 add("application/x-koan", "skd");
154 add("application/x-koan", "skt");
155 add("application/x-koan", "skm");
156 add("application/x-kpresenter", "kpr");
157 add("application/x-kpresenter", "kpt");
158 add("application/x-kspread", "ksp");
159 add("application/x-kword", "kwd");
160 add("application/x-kword", "kwt");
161 add("application/x-latex", "latex");
162 add("application/x-lha", "lha");
163 add("application/x-lzh", "lzh");
164 add("application/x-lzx", "lzx");
165 add("application/x-maker", "frm");
166 add("application/x-maker", "maker");
167 add("application/x-maker", "frame");
168 add("application/x-maker", "fb");
169 add("application/x-maker", "book");
170 add("application/x-maker", "fbdoc");
171 add("application/x-mif", "mif");
172 add("application/x-ms-wmd", "wmd");
173 add("application/x-ms-wmz", "wmz");
174 add("application/x-msi", "msi");
175 add("application/x-ns-proxy-autoconfig", "pac");
176 add("application/x-nwc", "nwc");
177 add("application/x-object", "o");
178 add("application/x-oz-application", "oza");
179 add("application/x-pem-file", "pem");
180 add("application/x-pkcs12", "p12");
181 add("application/x-pkcs12", "pfx");
182 add("application/x-pkcs7-certreqresp", "p7r");
183 add("application/x-pkcs7-crl", "crl");
184 add("application/x-quicktimeplayer", "qtl");
185 add("application/x-shar", "shar");
186 add("application/x-shockwave-flash", "swf");
187 add("application/x-stuffit", "sit");
188 add("application/x-sv4cpio", "sv4cpio");
189 add("application/x-sv4crc", "sv4crc");
190 add("application/x-tar", "tar");
191 add("application/x-texinfo", "texinfo");
192 add("application/x-texinfo", "texi");
193 add("application/x-troff", "t");
194 add("application/x-troff", "roff");
195 add("application/x-troff-man", "man");
196 add("application/x-ustar", "ustar");
197 add("application/x-wais-source", "src");
198 add("application/x-wingz", "wz");
199 add("application/x-webarchive", "webarchive");
200 add("application/x-webarchive-xml", "webarchivexml");
201 add("application/x-x509-ca-cert", "crt");
202 add("application/x-x509-user-cert", "crt");
203 add("application/x-x509-server-cert", "crt");
204 add("application/x-xcf", "xcf");
205 add("application/x-xfig", "fig");
206 add("application/xhtml+xml", "xhtml");
207 add("audio/3gpp", "3gpp");
208 add("audio/aac", "aac");
209 add("audio/aac-adts", "aac");
210 add("audio/amr", "amr");
211 add("audio/amr-wb", "awb");
212 add("audio/basic", "snd");
213 add("audio/flac", "flac");
214 add("application/x-flac", "flac");
215 add("audio/imelody", "imy");
216 add("audio/midi", "mid");
217 add("audio/midi", "midi");
218 add("audio/midi", "ota");
219 add("audio/midi", "kar");
220 add("audio/midi", "rtttl");
221 add("audio/midi", "xmf");
222 add("audio/mobile-xmf", "mxmf");
223 // add ".mp3" first so it will be the default for guessExtensionFromMimeType
224 add("audio/mpeg", "mp3");
225 add("audio/mpeg", "mpga");
226 add("audio/mpeg", "mpega");
227 add("audio/mpeg", "mp2");
228 add("audio/mpeg", "m4a");
229 add("audio/mpegurl", "m3u");
230 add("audio/prs.sid", "sid");
231 add("audio/x-aiff", "aif");
232 add("audio/x-aiff", "aiff");
233 add("audio/x-aiff", "aifc");
234 add("audio/x-gsm", "gsm");
235 add("audio/x-matroska", "mka");
236 add("audio/x-mpegurl", "m3u");
237 add("audio/x-ms-wma", "wma");
238 add("audio/x-ms-wax", "wax");
239 add("audio/x-pn-realaudio", "ra");
240 add("audio/x-pn-realaudio", "rm");
241 add("audio/x-pn-realaudio", "ram");
242 add("audio/x-realaudio", "ra");
243 add("audio/x-scpls", "pls");
244 add("audio/x-sd2", "sd2");
245 add("audio/x-wav", "wav");
246 // image/bmp isn't IANA, so image/x-ms-bmp should come first.
247 add("image/x-ms-bmp", "bmp");
248 add("image/bmp", "bmp");
249 add("image/gif", "gif");
250 // image/ico isn't IANA, so image/x-icon should come first.
251 add("image/x-icon", "ico");
252 add("image/ico", "cur");
253 add("image/ico", "ico");
254 add("image/ief", "ief");
255 // add ".jpg" first so it will be the default for guessExtensionFromMimeType
256 add("image/jpeg", "jpg");
257 add("image/jpeg", "jpeg");
258 add("image/jpeg", "jpe");
259 add("image/pcx", "pcx");
260 add("image/png", "png");
261 add("image/svg+xml", "svg");
262 add("image/svg+xml", "svgz");
263 add("image/tiff", "tiff");
264 add("image/tiff", "tif");
265 add("image/vnd.djvu", "djvu");
266 add("image/vnd.djvu", "djv");
267 add("image/vnd.wap.wbmp", "wbmp");
268 add("image/webp", "webp");
269 add("image/x-cmu-raster", "ras");
270 add("image/x-coreldraw", "cdr");
271 add("image/x-coreldrawpattern", "pat");
272 add("image/x-coreldrawtemplate", "cdt");
273 add("image/x-corelphotopaint", "cpt");
274 add("image/x-jg", "art");
275 add("image/x-jng", "jng");
276 add("image/x-photoshop", "psd");
277 add("image/x-portable-anymap", "pnm");
278 add("image/x-portable-bitmap", "pbm");
279 add("image/x-portable-graymap", "pgm");
280 add("image/x-portable-pixmap", "ppm");
281 add("image/x-rgb", "rgb");
282 add("image/x-xbitmap", "xbm");
283 add("image/x-xpixmap", "xpm");
284 add("image/x-xwindowdump", "xwd");
285 add("model/iges", "igs");
286 add("model/iges", "iges");
287 add("model/mesh", "msh");
288 add("model/mesh", "mesh");
289 add("model/mesh", "silo");
290 add("text/calendar", "ics");
291 add("text/calendar", "icz");
292 add("text/comma-separated-values", "csv");
293 add("text/css", "css");
294 add("text/html", "htm");
295 add("text/html", "html");
296 add("text/h323", "323");
297 add("text/iuls", "uls");
298 add("text/mathml", "mml");
299 // add ".txt" first so it will be the default for guessExtensionFromMimeType
300 add("text/plain", "txt");
301 add("text/plain", "asc");
302 add("text/plain", "text");
303 add("text/plain", "diff");
304 add("text/plain", "po"); // reserve "pot" for vnd.ms-powerpoint
305 add("text/richtext", "rtx");
306 add("text/rtf", "rtf");
307 add("text/text", "phps");
308 add("text/tab-separated-values", "tsv");
309 add("text/xml", "xml");
310 add("text/x-bibtex", "bib");
311 add("text/x-boo", "boo");
312 add("text/x-c++hdr", "hpp");
313 add("text/x-c++hdr", "h++");
314 add("text/x-c++hdr", "hxx");
315 add("text/x-c++hdr", "hh");
316 add("text/x-c++src", "cpp");
317 add("text/x-c++src", "c++");
318 add("text/x-c++src", "cc");
319 add("text/x-c++src", "cxx");
320 add("text/x-chdr", "h");
321 add("text/x-component", "htc");
322 add("text/x-csh", "csh");
323 add("text/x-csrc", "c");
324 add("text/x-dsrc", "d");
325 add("text/x-haskell", "hs");
326 add("text/x-java", "java");
327 add("text/x-literate-haskell", "lhs");
328 add("text/x-moc", "moc");
329 add("text/x-pascal", "p");
330 add("text/x-pascal", "pas");
331 add("text/x-pcs-gcd", "gcd");
332 add("text/x-setext", "etx");
333 add("text/x-tcl", "tcl");
334 add("text/x-tex", "tex");
335 add("text/x-tex", "ltx");
336 add("text/x-tex", "sty");
337 add("text/x-tex", "cls");
338 add("text/x-vcalendar", "vcs");
339 add("text/x-vcard", "vcf");
340 add("video/3gpp", "3gpp");
341 add("video/3gpp", "3gp");
342 add("video/3gpp2", "3gpp2");
343 add("video/3gpp2", "3g2");
344 add("video/avi", "avi");
345 add("video/dl", "dl");
346 add("video/dv", "dif");
347 add("video/dv", "dv");
348 add("video/fli", "fli");
349 add("video/m4v", "m4v");
350 add("video/mp2ts", "ts");
351 add("video/mpeg", "mpeg");
352 add("video/mpeg", "mpg");
353 add("video/mpeg", "mpe");
354 add("video/mp4", "mp4");
355 add("video/mpeg", "VOB");
356 add("video/quicktime", "qt");
357 add("video/quicktime", "mov");
358 add("video/vnd.mpegurl", "mxu");
359 add("video/webm", "webm");
360 add("video/x-la-asf", "lsf");
361 add("video/x-la-asf", "lsx");
362 add("video/x-matroska", "mkv");
363 add("video/x-mng", "mng");
364 add("video/x-ms-asf", "asf");
365 add("video/x-ms-asf", "asx");
366 add("video/x-ms-wm", "wm");
367 add("video/x-ms-wmv", "wmv");
368 add("video/x-ms-wmx", "wmx");
369 add("video/x-ms-wvx", "wvx");
370 add("video/x-sgi-movie", "movie");
371 add("video/x-webex", "wrf");
372 add("x-conference/x-cooltalk", "ice");
373 add("x-epoc/x-sisx-app", "sisx");
374 applyOverrides();
375 }
376 private static void add(String mimeType, String extension) {
377 // If we have an existing x -> y mapping, we do not want to
378 // override it with another mapping x -> y2.
379 // If a mime type maps to several extensions
380 // the first extension added is considered the most popular
381 // so we do not want to overwrite it later.
382 if (!mimeTypeToExtensionMap.containsKey(mimeType)) {
383 mimeTypeToExtensionMap.put(mimeType, extension);
384 }
385 if (!extensionToMimeTypeMap.containsKey(extension)) {
386 extensionToMimeTypeMap.put(extension, mimeType);
387 }
388 }
389 private static InputStream getContentTypesPropertiesStream() {
390 // User override?
391 String userTable = System.getProperty("content.types.user.table");
392 if (userTable != null) {
393 File f = new File(userTable);
394 if (f.exists()) {
395 try {
396 return new FileInputStream(f);
397 } catch (IOException ignored) {
398 }
399 }
400 }
401 // Standard location?
402 File f = new File(System.getProperty("java.home"), "lib" + File.separator + "content-types.properties");
403 if (f.exists()) {
404 try {
405 return new FileInputStream(f);
406 } catch (IOException ignored) {
407 }
408 }
409 return null;
410 }
411 /**
412 * This isn't what the RI does. The RI doesn't have hard-coded defaults, so supplying your
413 * own "content.types.user.table" means you don't get any of the built-ins, and the built-ins
414 * come from "$JAVA_HOME/lib/content-types.properties".
415 */
416 private static void applyOverrides() {
417 // Get the appropriate InputStream to read overrides from, if any.
418 InputStream stream = getContentTypesPropertiesStream();
419 if (stream == null) {
420 return;
421 }
422 try {
423 try {
424 // Read the properties file...
425 Properties overrides = new Properties();
426 overrides.load(stream);
427 // And translate its mapping to ours...
428 for (Map.Entry<Object, Object> entry : overrides.entrySet()) {
429 String extension = (String) entry.getKey();
430 String mimeType = (String) entry.getValue();
431 add(mimeType, extension);
432 }
433 } finally {
434 stream.close();
435 }
436 } catch (IOException ignored) {
437 }
438 }
439 private MimeUtils() {
440 }
441 /**
442 * Returns true if the given MIME type has an entry in the map.
443 * @param mimeType A MIME type (i.e. text/plain)
444 * @return True iff there is a mimeType entry in the map.
445 */
446 public static boolean hasMimeType(String mimeType) {
447 if (mimeType == null || mimeType.isEmpty()) {
448 return false;
449 }
450 return mimeTypeToExtensionMap.containsKey(mimeType);
451 }
452 /**
453 * Returns the MIME type for the given extension.
454 * @param extension A file extension without the leading '.'
455 * @return The MIME type for the given extension or null iff there is none.
456 */
457 public static String guessMimeTypeFromExtension(String extension) {
458 if (extension == null || extension.isEmpty()) {
459 return null;
460 }
461 return extensionToMimeTypeMap.get(extension.toLowerCase());
462 }
463 /**
464 * Returns true if the given extension has a registered MIME type.
465 * @param extension A file extension without the leading '.'
466 * @return True iff there is an extension entry in the map.
467 */
468 public static boolean hasExtension(String extension) {
469 if (extension == null || extension.isEmpty()) {
470 return false;
471 }
472 return extensionToMimeTypeMap.containsKey(extension);
473 }
474 /**
475 * Returns the registered extension for the given MIME type. Note that some
476 * MIME types map to multiple extensions. This call will return the most
477 * common extension for the given MIME type.
478 * @param mimeType A MIME type (i.e. text/plain)
479 * @return The extension for the given MIME type or null iff there is none.
480 */
481 public static String guessExtensionFromMimeType(String mimeType) {
482 if (mimeType == null || mimeType.isEmpty()) {
483 return null;
484 }
485 return mimeTypeToExtensionMap.get(mimeType);
486 }
487}