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;
17
18import android.content.Context;
19import android.database.Cursor;
20import android.net.Uri;
21import android.provider.OpenableColumns;
22import android.util.Log;
23
24import com.google.common.base.Strings;
25import com.google.common.collect.ImmutableList;
26
27import java.io.File;
28import java.io.FileInputStream;
29import java.io.IOException;
30import java.io.InputStream;
31import java.util.HashMap;
32import java.util.List;
33import java.util.Map;
34import java.util.Properties;
35
36import eu.siacs.conversations.Config;
37import eu.siacs.conversations.entities.Transferable;
38import eu.siacs.conversations.services.ExportBackupService;
39
40/**
41 * Utilities for dealing with MIME types.
42 * Used to implement java.net.URLConnection and android.webkit.MimeTypeMap.
43 */
44public final class MimeUtils {
45
46 public static final List<String> AMBIGUOUS_CONTAINER_FORMATS = ImmutableList.of(
47 "application/ogg",
48 "video/3gpp", // .3gp files can contain audio, video or both
49 "video/3gpp2"
50 );
51
52 private static final Map<String, String> mimeTypeToExtensionMap = new HashMap<>();
53 private static final Map<String, String> extensionToMimeTypeMap = new HashMap<>();
54
55 static {
56 // The following table is based on /etc/mime.types data minus
57 // chemical/* MIME types and MIME types that don't map to any
58 // file extensions. We also exclude top-level domain names to
59 // deal with cases like:
60 //
61 // mail.google.com/a/google.com
62 //
63 // and "active" MIME types (due to potential security issues).
64 // Note that this list is _not_ in alphabetical order and must not be sorted.
65 // The "most popular" extension must come first, so that it's the one returned
66 // by guessExtensionFromMimeType.
67 add("application/andrew-inset", "ez");
68 add("application/dsptype", "tsp");
69 add("application/epub+zip", "epub");
70 add("application/gpx+xml", "gpx");
71 add("application/hta", "hta");
72 add("application/mac-binhex40", "hqx");
73 add("application/mathematica", "nb");
74 add("application/msaccess", "mdb");
75 add("application/oda", "oda");
76 add("application/ogg", "ogg");
77 add("application/pdf", "pdf");
78 add("application/pgp-keys", "key");
79 add("application/pgp-signature", "pgp");
80 add("application/pics-rules", "prf");
81 add("application/pkix-cert", "cer");
82 add("application/rar", "rar");
83 add("application/rdf+xml", "rdf");
84 add("application/rss+xml", "rss");
85 add("application/zip", "zip");
86 add("application/vnd.amazon.mobi8-ebook", "azw3");
87 add("application/vnd.amazon.mobi8-ebook", "azw");
88 add("application/vnd.amazon.mobi8-ebook", "kfx");
89 add("application/vnd.android.package-archive", "apk");
90 add("application/vnd.cinderella", "cdy");
91 add(ExportBackupService.MIME_TYPE, "ceb");
92 add("application/vnd.ms-pki.stl", "stl");
93 add("application/vnd.oasis.opendocument.database", "odb");
94 add("application/vnd.oasis.opendocument.formula", "odf");
95 add("application/vnd.oasis.opendocument.graphics", "odg");
96 add("application/vnd.oasis.opendocument.graphics-template", "otg");
97 add("application/vnd.oasis.opendocument.image", "odi");
98 add("application/vnd.oasis.opendocument.spreadsheet", "ods");
99 add("application/vnd.oasis.opendocument.spreadsheet-template", "ots");
100 add("application/vnd.oasis.opendocument.text", "odt");
101 add("application/vnd.oasis.opendocument.text-master", "odm");
102 add("application/vnd.oasis.opendocument.text-template", "ott");
103 add("application/vnd.oasis.opendocument.text-web", "oth");
104 add("application/vnd.google-earth.kml+xml", "kml");
105 add("application/vnd.google-earth.kmz", "kmz");
106 add("application/msword", "doc");
107 add("application/msword", "dot");
108 add("application/vnd.openxmlformats-officedocument.wordprocessingml.document", "docx");
109 add("application/vnd.openxmlformats-officedocument.wordprocessingml.template", "dotx");
110 add("application/vnd.ms-excel", "xls");
111 add("application/vnd.ms-excel", "xlt");
112 add("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx");
113 add("application/vnd.openxmlformats-officedocument.spreadsheetml.template", "xltx");
114 add("application/vnd.ms-powerpoint", "ppt");
115 add("application/vnd.ms-powerpoint", "pot");
116 add("application/vnd.ms-powerpoint", "pps");
117 add("application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx");
118 add("application/vnd.openxmlformats-officedocument.presentationml.template", "potx");
119 add("application/vnd.openxmlformats-officedocument.presentationml.slideshow", "ppsx");
120 add("application/vnd.rim.cod", "cod");
121 add("application/vnd.smaf", "mmf");
122 add("application/vnd.stardivision.calc", "sdc");
123 add("application/vnd.stardivision.draw", "sda");
124 add("application/vnd.stardivision.impress", "sdd");
125 add("application/vnd.stardivision.impress", "sdp");
126 add("application/vnd.stardivision.math", "smf");
127 add("application/vnd.stardivision.writer", "sdw");
128 add("application/vnd.stardivision.writer", "vor");
129 add("application/vnd.stardivision.writer-global", "sgl");
130 add("application/vnd.sun.xml.calc", "sxc");
131 add("application/vnd.sun.xml.calc.template", "stc");
132 add("application/vnd.sun.xml.draw", "sxd");
133 add("application/vnd.sun.xml.draw.template", "std");
134 add("application/vnd.sun.xml.impress", "sxi");
135 add("application/vnd.sun.xml.impress.template", "sti");
136 add("application/vnd.sun.xml.math", "sxm");
137 add("application/vnd.sun.xml.writer", "sxw");
138 add("application/vnd.sun.xml.writer.global", "sxg");
139 add("application/vnd.sun.xml.writer.template", "stw");
140 add("application/vnd.visio", "vsd");
141 add("application/x-abiword", "abw");
142 add("application/x-apple-diskimage", "dmg");
143 add("application/x-bcpio", "bcpio");
144 add("application/x-bittorrent", "torrent");
145 add("application/x-cdf", "cdf");
146 add("application/x-cdlink", "vcd");
147 add("application/x-chess-pgn", "pgn");
148 add("application/x-cpio", "cpio");
149 add("application/x-debian-package", "deb");
150 add("application/x-debian-package", "udeb");
151 add("application/x-director", "dcr");
152 add("application/x-director", "dir");
153 add("application/x-director", "dxr");
154 add("application/x-dms", "dms");
155 add("application/x-doom", "wad");
156 add("application/x-dvi", "dvi");
157 add("application/x-font", "pfa");
158 add("application/x-font", "pfb");
159 add("application/x-font", "gsf");
160 add("application/x-font", "pcf");
161 add("application/x-font", "pcf.Z");
162 add("application/x-freemind", "mm");
163 // application/futuresplash isn't IANA, so application/x-futuresplash should come first.
164 add("application/x-futuresplash", "spl");
165 add("application/futuresplash", "spl");
166 add("application/x-gnumeric", "gnumeric");
167 add("application/x-go-sgf", "sgf");
168 add("application/x-graphing-calculator", "gcf");
169 add("application/x-gtar", "tgz");
170 add("application/x-gtar", "gtar");
171 add("application/x-gtar", "taz");
172 add("application/x-hdf", "hdf");
173 add("application/x-ica", "ica");
174 add("application/x-internet-signup", "ins");
175 add("application/x-internet-signup", "isp");
176 add("application/x-iphone", "iii");
177 add("application/x-iso9660-image", "iso");
178 add("application/x-jmol", "jmz");
179 add("application/x-kchart", "chrt");
180 add("application/x-killustrator", "kil");
181 add("application/x-koan", "skp");
182 add("application/x-koan", "skd");
183 add("application/x-koan", "skt");
184 add("application/x-koan", "skm");
185 add("application/x-kpresenter", "kpr");
186 add("application/x-kpresenter", "kpt");
187 add("application/x-kspread", "ksp");
188 add("application/x-kword", "kwd");
189 add("application/x-kword", "kwt");
190 add("application/x-latex", "latex");
191 add("application/x-lha", "lha");
192 add("application/x-lzh", "lzh");
193 add("application/x-lzx", "lzx");
194 add("application/x-maker", "frm");
195 add("application/x-maker", "maker");
196 add("application/x-maker", "frame");
197 add("application/x-maker", "fb");
198 add("application/x-maker", "book");
199 add("application/x-maker", "fbdoc");
200 add("application/x-mif", "mif");
201 add("application/x-mobipocket-ebook", "mobi");
202 add("application/x-ms-wmd", "wmd");
203 add("application/x-ms-wmz", "wmz");
204 add("application/x-msi", "msi");
205 add("application/x-ns-proxy-autoconfig", "pac");
206 add("application/x-nwc", "nwc");
207 add("application/x-object", "o");
208 add("application/x-oz-application", "oza");
209 add("application/x-pem-file", "pem");
210 add("application/x-pkcs12", "p12");
211 add("application/x-pkcs12", "pfx");
212 add("application/x-pkcs7-certreqresp", "p7r");
213 add("application/x-pkcs7-crl", "crl");
214 add("application/x-quicktimeplayer", "qtl");
215 add("application/x-shar", "shar");
216 add("application/x-shockwave-flash", "swf");
217 add("application/x-stuffit", "sit");
218 add("application/x-sv4cpio", "sv4cpio");
219 add("application/x-sv4crc", "sv4crc");
220 add("application/x-tar", "tar");
221 add("application/x-texinfo", "texinfo");
222 add("application/x-texinfo", "texi");
223 add("application/x-troff", "t");
224 add("application/x-troff", "roff");
225 add("application/x-troff-man", "man");
226 add("application/x-ustar", "ustar");
227 add("application/x-wais-source", "src");
228 add("application/x-wingz", "wz");
229 add("application/x-webarchive", "webarchive");
230 add("application/x-webarchive-xml", "webarchivexml");
231 add("application/x-x509-ca-cert", "crt");
232 add("application/x-x509-user-cert", "crt");
233 add("application/x-x509-server-cert", "crt");
234 add("application/x-xcf", "xcf");
235 add("application/x-xfig", "fig");
236 add("application/xhtml+xml", "xhtml");
237 add("video/3gpp", "3gpp");
238 add("video/3gpp", "3gp");
239 add("video/3gpp2", "3gpp2");
240 add("video/3gpp2", "3g2");
241 add("audio/3gpp", "3gpp");
242 add("audio/3gpp", "3gp");
243 add("audio/aac", "aac");
244 add("audio/aac-adts", "aac");
245 add("audio/amr", "amr");
246 add("audio/amr-wb", "awb");
247 add("audio/basic", "snd");
248 add("audio/flac", "flac");
249 add("application/x-flac", "flac");
250 add("audio/imelody", "imy");
251 add("audio/midi", "mid");
252 add("audio/midi", "midi");
253 add("audio/midi", "ota");
254 add("audio/midi", "kar");
255 add("audio/midi", "rtttl");
256 add("audio/midi", "xmf");
257 add("audio/mobile-xmf", "mxmf");
258 // add ".mp3" first so it will be the default for guessExtensionFromMimeType
259 add("audio/mpeg", "mp3");
260 add("audio/mpeg", "mpga");
261 add("audio/mpeg", "mpega");
262 add("audio/mpeg", "mp2");
263 add("audio/mpeg", "m4a");
264 add("audio/mpegurl", "m3u");
265 add("audio/ogg", "oga");
266 add("audio/opus", "opus");
267 add("audio/prs.sid", "sid");
268 add("audio/x-aiff", "aif");
269 add("audio/x-aiff", "aiff");
270 add("audio/x-aiff", "aifc");
271 add("audio/x-gsm", "gsm");
272 add("audio/x-matroska", "mka");
273 add("audio/x-mpegurl", "m3u");
274 add("audio/x-ms-wma", "wma");
275 add("audio/x-ms-wax", "wax");
276 add("audio/x-pn-realaudio", "ra");
277 add("audio/x-pn-realaudio", "rm");
278 add("audio/x-pn-realaudio", "ram");
279 add("audio/x-realaudio", "ra");
280 add("audio/x-scpls", "pls");
281 add("audio/x-sd2", "sd2");
282 add("audio/x-wav", "wav");
283 // image/bmp isn't IANA, so image/x-ms-bmp should come first.
284 add("image/x-ms-bmp", "bmp");
285 add("image/bmp", "bmp");
286 add("image/gif", "gif");
287 // image/ico isn't IANA, so image/x-icon should come first.
288 add("image/x-icon", "ico");
289 add("image/ico", "cur");
290 add("image/ico", "ico");
291 add("image/ief", "ief");
292 add("image/heic", "heic");
293 add("image/heif", "heif");
294 add("image/avif", "avif");
295 // add ".jpg" first so it will be the default for guessExtensionFromMimeType
296 add("image/jpeg", "jpg");
297 add("image/jpeg", "jpeg");
298 add("image/jpeg", "jpe");
299 add("image/jpeg", "jfif");
300 add("image/jpeg", "jif");
301 add("image/pcx", "pcx");
302 add("image/png", "png");
303 add("image/svg+xml", "svg");
304 add("image/svg+xml", "svgz");
305 add("image/tiff", "tiff");
306 add("image/tiff", "tif");
307 add("image/vnd.djvu", "djvu");
308 add("image/vnd.djvu", "djv");
309 add("image/vnd.wap.wbmp", "wbmp");
310 add("image/webp", "webp");
311 add("image/x-cmu-raster", "ras");
312 add("image/x-coreldraw", "cdr");
313 add("image/x-coreldrawpattern", "pat");
314 add("image/x-coreldrawtemplate", "cdt");
315 add("image/x-corelphotopaint", "cpt");
316 add("image/x-jg", "art");
317 add("image/x-jng", "jng");
318 add("image/x-photoshop", "psd");
319 add("image/x-portable-anymap", "pnm");
320 add("image/x-portable-bitmap", "pbm");
321 add("image/x-portable-graymap", "pgm");
322 add("image/x-portable-pixmap", "ppm");
323 add("image/x-rgb", "rgb");
324 add("image/x-xbitmap", "xbm");
325 add("image/x-xpixmap", "xpm");
326 add("image/x-xwindowdump", "xwd");
327 add("model/iges", "igs");
328 add("model/iges", "iges");
329 add("model/mesh", "msh");
330 add("model/mesh", "mesh");
331 add("model/mesh", "silo");
332 add("text/calendar", "ics");
333 add("text/calendar", "icz");
334 add("text/comma-separated-values", "csv");
335 add("text/css", "css");
336 add("text/html", "htm");
337 add("text/html", "html");
338 add("text/h323", "323");
339 add("text/iuls", "uls");
340 add("text/mathml", "mml");
341 // add ".txt" first so it will be the default for guessExtensionFromMimeType
342 add("text/plain", "txt");
343 add("text/plain", "asc");
344 add("text/plain", "text");
345 add("text/plain", "diff");
346 add("text/plain", "po"); // reserve "pot" for vnd.ms-powerpoint
347 add("text/richtext", "rtx");
348 add("text/rtf", "rtf");
349 add("text/text", "phps");
350 add("text/tab-separated-values", "tsv");
351 add("text/xml", "xml");
352 add("text/x-bibtex", "bib");
353 add("text/x-boo", "boo");
354 add("text/x-c++hdr", "hpp");
355 add("text/x-c++hdr", "h++");
356 add("text/x-c++hdr", "hxx");
357 add("text/x-c++hdr", "hh");
358 add("text/x-c++src", "cpp");
359 add("text/x-c++src", "c++");
360 add("text/x-c++src", "cc");
361 add("text/x-c++src", "cxx");
362 add("text/x-chdr", "h");
363 add("text/x-component", "htc");
364 add("text/x-csh", "csh");
365 add("text/x-csrc", "c");
366 add("text/x-dsrc", "d");
367 add("text/x-haskell", "hs");
368 add("text/x-java", "java");
369 add("text/x-literate-haskell", "lhs");
370 add("text/x-moc", "moc");
371 add("text/x-pascal", "p");
372 add("text/x-pascal", "pas");
373 add("text/x-pcs-gcd", "gcd");
374 add("text/x-setext", "etx");
375 add("text/x-tcl", "tcl");
376 add("text/x-tex", "tex");
377 add("text/x-tex", "ltx");
378 add("text/x-tex", "sty");
379 add("text/x-tex", "cls");
380 add("text/x-vcalendar", "vcs");
381 add("text/x-vcard", "vcf");
382 add("video/avi", "avi");
383 add("video/dl", "dl");
384 add("video/dv", "dif");
385 add("video/dv", "dv");
386 add("video/fli", "fli");
387 add("video/m4v", "m4v");
388 add("video/mp2ts", "ts");
389 add("video/ogg", "ogv");
390 add("video/mpeg", "mpeg");
391 add("video/mpeg", "mpg");
392 add("video/mpeg", "mpe");
393 add("video/mp4", "mp4");
394 add("video/mpeg", "VOB");
395 add("video/quicktime", "qt");
396 add("video/quicktime", "mov");
397 add("video/vnd.mpegurl", "mxu");
398 add("video/webm", "webm");
399 add("video/x-la-asf", "lsf");
400 add("video/x-la-asf", "lsx");
401 add("video/x-matroska", "mkv");
402 add("video/x-mng", "mng");
403 add("video/x-ms-asf", "asf");
404 add("video/x-ms-asf", "asx");
405 add("video/x-ms-wm", "wm");
406 add("video/x-ms-wmv", "wmv");
407 add("video/x-ms-wmx", "wmx");
408 add("video/x-ms-wvx", "wvx");
409 add("video/x-sgi-movie", "movie");
410 add("video/x-webex", "wrf");
411 add("x-conference/x-cooltalk", "ice");
412 add("x-epoc/x-sisx-app", "sisx");
413 applyOverrides();
414 }
415
416 private static void add(String mimeType, String extension) {
417 // If we have an existing x -> y mapping, we do not want to
418 // override it with another mapping x -> y2.
419 // If a mime type maps to several extensions
420 // the first extension added is considered the most popular
421 // so we do not want to overwrite it later.
422 if (!mimeTypeToExtensionMap.containsKey(mimeType)) {
423 mimeTypeToExtensionMap.put(mimeType, extension);
424 }
425 if (!extensionToMimeTypeMap.containsKey(extension)) {
426 extensionToMimeTypeMap.put(extension, mimeType);
427 }
428 }
429
430 private static InputStream getContentTypesPropertiesStream() {
431 // User override?
432 String userTable = System.getProperty("content.types.user.table");
433 if (userTable != null) {
434 File f = new File(userTable);
435 if (f.exists()) {
436 try {
437 return new FileInputStream(f);
438 } catch (IOException ignored) {
439 }
440 }
441 }
442 // Standard location?
443 File f = new File(System.getProperty("java.home"), "lib" + File.separator + "content-types.properties");
444 if (f.exists()) {
445 try {
446 return new FileInputStream(f);
447 } catch (IOException ignored) {
448 }
449 }
450 return null;
451 }
452
453 /**
454 * This isn't what the RI does. The RI doesn't have hard-coded defaults, so supplying your
455 * own "content.types.user.table" means you don't get any of the built-ins, and the built-ins
456 * come from "$JAVA_HOME/lib/content-types.properties".
457 */
458 private static void applyOverrides() {
459 // Get the appropriate InputStream to read overrides from, if any.
460 InputStream stream = getContentTypesPropertiesStream();
461 if (stream == null) {
462 return;
463 }
464 try {
465 try {
466 // Read the properties file...
467 Properties overrides = new Properties();
468 overrides.load(stream);
469 // And translate its mapping to ours...
470 for (Map.Entry<Object, Object> entry : overrides.entrySet()) {
471 String extension = (String) entry.getKey();
472 String mimeType = (String) entry.getValue();
473 add(mimeType, extension);
474 }
475 } finally {
476 stream.close();
477 }
478 } catch (IOException ignored) {
479 }
480 }
481
482 private MimeUtils() {
483 }
484
485 /**
486 * Returns true if the given MIME type has an entry in the map.
487 *
488 * @param mimeType A MIME type (i.e. text/plain)
489 * @return True iff there is a mimeType entry in the map.
490 */
491 public static boolean hasMimeType(String mimeType) {
492 if (mimeType == null || mimeType.isEmpty()) {
493 return false;
494 }
495 return mimeTypeToExtensionMap.containsKey(mimeType);
496 }
497
498 /**
499 * Returns the MIME type for the given extension.
500 *
501 * @param extension A file extension without the leading '.'
502 * @return The MIME type for the given extension or null iff there is none.
503 */
504 public static String guessMimeTypeFromExtension(String extension) {
505 if (extension == null || extension.isEmpty()) {
506 return null;
507 }
508 return extensionToMimeTypeMap.get(extension.toLowerCase());
509 }
510
511 /**
512 * Returns true if the given extension has a registered MIME type.
513 *
514 * @param extension A file extension without the leading '.'
515 * @return True iff there is an extension entry in the map.
516 */
517 public static boolean hasExtension(String extension) {
518 if (extension == null || extension.isEmpty()) {
519 return false;
520 }
521 return extensionToMimeTypeMap.containsKey(extension);
522 }
523
524 /**
525 * Returns the registered extension for the given MIME type. Note that some
526 * MIME types map to multiple extensions. This call will return the most
527 * common extension for the given MIME type.
528 *
529 * @param mimeType A MIME type (i.e. text/plain)
530 * @return The extension for the given MIME type or null iff there is none.
531 */
532 public static String guessExtensionFromMimeType(String mimeType) {
533 if (mimeType == null || mimeType.isEmpty()) {
534 return null;
535 }
536 return mimeTypeToExtensionMap.get(mimeType.split(";")[0]);
537 }
538
539 public static String guessMimeTypeFromUriAndMime(final Context context, final Uri uri, final String mime) {
540 Log.d(Config.LOGTAG, "guessMimeTypeFromUriAndMime " + uri + " and mime=" + mime);
541 final String guess = guessMimeTypeFromUri(context, uri);
542 if (guess != null) {
543 return guess;
544 } else {
545 return mime;
546 }
547 }
548
549 public static String guessMimeTypeFromUri(Context context, Uri uri) {
550 // try the content resolver
551 String mimeType;
552 try {
553 mimeType = context.getContentResolver().getType(uri);
554 } catch (final Throwable throwable) {
555 mimeType = null;
556 }
557 // try the extension
558 if (mimeType == null || mimeType.equals("application/octet-stream")) {
559 final String path = uri.getPath();
560 if (path != null) {
561 mimeType = guessFromPath(path);
562 }
563 }
564 if (mimeType == null && "content".equals(uri.getScheme())) {
565 final String name = getDisplayName(context, uri);
566 if (name != null) {
567 mimeType = guessFromPath(name);
568 }
569 }
570 // sometimes this works (as with the commit content api)
571 if (mimeType == null) {
572 mimeType = uri.getQueryParameter("mimeType");
573 }
574 return mimeType;
575 }
576
577 private static String getDisplayName(final Context context, final Uri uri) {
578 try (final Cursor cursor = context.getContentResolver().query(uri, null, null, null, null)) {
579 if (cursor != null && cursor.moveToFirst()) {
580 final int index = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
581 if (index == -1) {
582 return null;
583 }
584 return cursor.getString(index);
585 }
586 } catch (final Exception e) {
587 return null;
588 }
589 return null;
590 }
591
592 private static String guessFromPath(final String path) {
593 final int start = path.lastIndexOf('.') + 1;
594 if (start < path.length()) {
595 return MimeUtils.guessMimeTypeFromExtension(path.substring(start));
596 }
597 return null;
598 }
599
600 public static String extractRelevantExtension(final String path) {
601 return extractRelevantExtension(path, false);
602 }
603
604 public static String extractRelevantExtension(final String path, final boolean ignoreCryptoExtension) {
605 if (Strings.isNullOrEmpty(path)) {
606 return null;
607 }
608
609 final String filenameQueryAnchor = path.substring(path.lastIndexOf('/') + 1);
610 final String filenameQuery = cutBefore(filenameQueryAnchor, '#');
611 final String filename = cutBefore(filenameQuery, '?');
612 final int dotPosition = filename.lastIndexOf('.');
613
614 if (dotPosition == -1) {
615 return null;
616 }
617 final String extension = filename.substring(dotPosition + 1);
618 // we want the real file extension, not the crypto one
619 if (ignoreCryptoExtension && Transferable.VALID_CRYPTO_EXTENSIONS.contains(extension)) {
620 return extractRelevantExtension(filename.substring(0, dotPosition));
621 } else {
622 return extension;
623 }
624 }
625
626 private static String cutBefore(final String input, final char c) {
627 final int position = input.indexOf(c);
628 if (position > 0) {
629 return input.substring(0, position);
630 } else {
631 return input;
632 }
633 }
634}