|
| 1 | +/* |
| 2 | + * Copyright (C) 2021-2023 Arpit Khurana <arpitkh96@gmail.com>, Vishal Nehra <vishalmeham2@gmail.com>, |
| 3 | + * Emmanuel Messulam<emmanuelbendavid@gmail.com>, Raymond Lai <airwave209gt at gmail.com> and Contributors. |
| 4 | + * |
| 5 | + * This file is part of Amaze File Utilities. |
| 6 | + * |
| 7 | + * Amaze File Utilities is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +package com.artifex.mupdf.viewer.share; |
| 22 | + |
| 23 | +import android.webkit.MimeTypeMap; |
| 24 | + |
| 25 | +import androidx.annotation.Nullable; |
| 26 | + |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.Locale; |
| 29 | +import java.util.regex.Pattern; |
| 30 | + |
| 31 | +public final class MimeTypes { |
| 32 | + |
| 33 | + public static final String ALL_MIME_TYPES = "*/*"; |
| 34 | + |
| 35 | + // construct a with an approximation of the capacity |
| 36 | + private static final HashMap<String, String> MIME_TYPES = new HashMap<>(1 + (int) (68 / 0.75)); |
| 37 | + |
| 38 | + static { |
| 39 | + |
| 40 | + /* |
| 41 | + * ================= MIME TYPES ==================== |
| 42 | + */ |
| 43 | + MIME_TYPES.put("asm", "text/x-asm"); |
| 44 | + MIME_TYPES.put("json", "application/json"); |
| 45 | + MIME_TYPES.put("js", "application/javascript"); |
| 46 | + |
| 47 | + MIME_TYPES.put("def", "text/plain"); |
| 48 | + MIME_TYPES.put("in", "text/plain"); |
| 49 | + MIME_TYPES.put("rc", "text/plain"); |
| 50 | + MIME_TYPES.put("list", "text/plain"); |
| 51 | + MIME_TYPES.put("log", "text/plain"); |
| 52 | + MIME_TYPES.put("pl", "text/plain"); |
| 53 | + MIME_TYPES.put("prop", "text/plain"); |
| 54 | + MIME_TYPES.put("properties", "text/plain"); |
| 55 | + MIME_TYPES.put("ini", "text/plain"); |
| 56 | + MIME_TYPES.put("md", "text/markdown"); |
| 57 | + |
| 58 | + MIME_TYPES.put("epub", "application/epub+zip"); |
| 59 | + MIME_TYPES.put("ibooks", "application/x-ibooks+zip"); |
| 60 | + |
| 61 | + MIME_TYPES.put("ifb", "text/calendar"); |
| 62 | + MIME_TYPES.put("eml", "message/rfc822"); |
| 63 | + MIME_TYPES.put("msg", "application/vnd.ms-outlook"); |
| 64 | + |
| 65 | + MIME_TYPES.put("ace", "application/x-ace-compressed"); |
| 66 | + MIME_TYPES.put("bz", "application/x-bzip"); |
| 67 | + MIME_TYPES.put("bz2", "application/x-bzip2"); |
| 68 | + MIME_TYPES.put("cab", "application/vnd.ms-cab-compressed"); |
| 69 | + MIME_TYPES.put("gz", "application/x-gzip"); |
| 70 | + MIME_TYPES.put("7z", "application/x-7z-compressed"); |
| 71 | + MIME_TYPES.put("lrf", "application/octet-stream"); |
| 72 | + MIME_TYPES.put("jar", "application/java-archive"); |
| 73 | + MIME_TYPES.put("xz", "application/x-xz"); |
| 74 | + MIME_TYPES.put("lzma", "application/x-lzma"); |
| 75 | + MIME_TYPES.put("Z", "application/x-compress"); |
| 76 | + |
| 77 | + MIME_TYPES.put("bat", "application/x-msdownload"); |
| 78 | + MIME_TYPES.put("ksh", "text/plain"); |
| 79 | + MIME_TYPES.put("sh", "application/x-sh"); |
| 80 | + |
| 81 | + MIME_TYPES.put("db", "application/octet-stream"); |
| 82 | + MIME_TYPES.put("db3", "application/octet-stream"); |
| 83 | + |
| 84 | + MIME_TYPES.put("otf", "application/x-font-otf"); |
| 85 | + MIME_TYPES.put("ttf", "application/x-font-ttf"); |
| 86 | + MIME_TYPES.put("psf", "application/x-font-linux-psf"); |
| 87 | + |
| 88 | + MIME_TYPES.put("cgm", "image/cgm"); |
| 89 | + MIME_TYPES.put("btif", "image/prs.btif"); |
| 90 | + MIME_TYPES.put("dwg", "image/vnd.dwg"); |
| 91 | + MIME_TYPES.put("dxf", "image/vnd.dxf"); |
| 92 | + MIME_TYPES.put("fbs", "image/vnd.fastbidsheet"); |
| 93 | + MIME_TYPES.put("fpx", "image/vnd.fpx"); |
| 94 | + MIME_TYPES.put("fst", "image/vnd.fst"); |
| 95 | + MIME_TYPES.put("mdi", "image/vnd.ms-mdi"); |
| 96 | + MIME_TYPES.put("npx", "image/vnd.net-fpx"); |
| 97 | + MIME_TYPES.put("xif", "image/vnd.xiff"); |
| 98 | + MIME_TYPES.put("pct", "image/x-pict"); |
| 99 | + MIME_TYPES.put("pic", "image/x-pict"); |
| 100 | + MIME_TYPES.put("gif", "image/gif"); |
| 101 | + |
| 102 | + MIME_TYPES.put("adp", "audio/adpcm"); |
| 103 | + MIME_TYPES.put("au", "audio/basic"); |
| 104 | + MIME_TYPES.put("snd", "audio/basic"); |
| 105 | + MIME_TYPES.put("m2a", "audio/mpeg"); |
| 106 | + MIME_TYPES.put("m3a", "audio/mpeg"); |
| 107 | + MIME_TYPES.put("oga", "audio/ogg"); |
| 108 | + MIME_TYPES.put("spx", "audio/ogg"); |
| 109 | + MIME_TYPES.put("aac", "audio/x-aac"); |
| 110 | + MIME_TYPES.put("mka", "audio/x-matroska"); |
| 111 | + MIME_TYPES.put("opus", "audio/ogg"); |
| 112 | + |
| 113 | + MIME_TYPES.put("jpgv", "video/jpeg"); |
| 114 | + MIME_TYPES.put("jpgm", "video/jpm"); |
| 115 | + MIME_TYPES.put("jpm", "video/jpm"); |
| 116 | + MIME_TYPES.put("mj2", "video/mj2"); |
| 117 | + MIME_TYPES.put("mjp2", "video/mj2"); |
| 118 | + MIME_TYPES.put("mpa", "video/mpeg"); |
| 119 | + MIME_TYPES.put("ogv", "video/ogg"); |
| 120 | + MIME_TYPES.put("flv", "video/x-flv"); |
| 121 | + MIME_TYPES.put("mkv", "video/x-matroska"); |
| 122 | + MIME_TYPES.put("mts", "video/mp2t"); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Get Mime Type of a file |
| 127 | + * |
| 128 | + * @param path the file of which mime type to get |
| 129 | + * @return Mime type in form of String |
| 130 | + */ |
| 131 | + public static String getMimeType(String path, boolean isDirectory) { |
| 132 | + if (isDirectory) { |
| 133 | + return null; |
| 134 | + } |
| 135 | + |
| 136 | + String type = ALL_MIME_TYPES; |
| 137 | + final String extension = getExtension(path); |
| 138 | + |
| 139 | + // mapping extension to system mime types |
| 140 | + if (extension != null && !extension.isEmpty()) { |
| 141 | + final String extensionLowerCase = extension.toLowerCase(Locale.getDefault()); |
| 142 | + final MimeTypeMap mime = MimeTypeMap.getSingleton(); |
| 143 | + type = mime.getMimeTypeFromExtension(extensionLowerCase); |
| 144 | + if (type == null) { |
| 145 | + type = MIME_TYPES.get(extensionLowerCase); |
| 146 | + } |
| 147 | + } |
| 148 | + if (type == null) type = ALL_MIME_TYPES; |
| 149 | + return type; |
| 150 | + } |
| 151 | + |
| 152 | + public static boolean mimeTypeMatch(String mime, String input) { |
| 153 | + return Pattern.matches(mime.replace("*", ".*"), input); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Helper method for {@link #getMimeType(String, boolean)} to calculate the last '.' extension of |
| 158 | + * files |
| 159 | + * |
| 160 | + * @param path the path of file |
| 161 | + * @return extension extracted from name in lowercase |
| 162 | + */ |
| 163 | + public static String getExtension(@Nullable String path) { |
| 164 | + if (path != null && path.contains(".")) |
| 165 | + return path.substring(path.lastIndexOf(".") + 1).toLowerCase(); |
| 166 | + else return ""; |
| 167 | + } |
| 168 | +} |
0 commit comments