-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMimeMap.java
More file actions
315 lines (268 loc) · 9.94 KB
/
MimeMap.java
File metadata and controls
315 lines (268 loc) · 9.94 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* Copyright (c) 2003-2017 Fred Hutchinson Cancer Research Center
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.labkey.api.util;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.junit.Assert;
import org.junit.Test;
import org.labkey.api.util.logging.LogHelper;
import java.io.File;
import java.io.InputStream;
import java.net.FileNameMap;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* A mime type map that implements the java.net.FileNameMap interface.
* Copied from Tomcat, modified to read from mime.txt, loaded as a classloader resource.
*/
public class MimeMap implements FileNameMap
{
private static final Map<String, MimeType> mimeTypeMap = new HashMap<>(101);
private static final Map<String, MimeType> extensionMap = new HashMap<>();
public static final MimeMap DEFAULT = new MimeMap();
public static class MimeType
{
private final String _contentType;
private final boolean _inline;
private final boolean _json;
public MimeType(String contentType)
{
this(contentType, false);
}
public MimeType(String contentType, boolean inline)
{
this(contentType, inline, false);
}
public MimeType(String contentType, boolean inline, boolean json)
{
_contentType = contentType;
_inline = inline;
_json = json;
}
public String getContentType()
{
return _contentType;
}
// e.g. is displayable by all browsers
public boolean canInline()
{
return _inline;
}
public boolean isImage()
{
return _contentType.startsWith("image/");
}
public boolean isInlineImage()
{
return canInline() && isImage();
}
@Override
public String toString()
{
return _contentType;
}
@Override
public boolean equals(Object o)
{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MimeType mimeType = (MimeType) o;
if (_inline != mimeType._inline) return false;
return _contentType != null ? _contentType.equals(mimeType._contentType) : mimeType._contentType == null;
}
@Override
public int hashCode()
{
int result = _contentType != null ? _contentType.hashCode() : 0;
result = 31 * result + (_inline ? 1 : 0);
return result;
}
// Constants for a few common mime types. We could add a lot more. If you add/remove constants then modify the list below.
// And if you can find a more official MimeType enum or constant list in one of our libraries then by all means get rid of these.
public static final MimeType GIF = new MimeType("image/gif", true);
public static final MimeType JPEG = new MimeType("image/jpeg", true);
public static final MimeType PDF = new MimeType("application/pdf", true);
public static final MimeType PNG = new MimeType("image/png", true);
public static final MimeType SVG = new MimeType("image/svg+xml", true);
public static final MimeType HTML = new MimeType("text/html");
public static final MimeType PLAIN = new MimeType("text/plain");
public static final MimeType XML = new MimeType("text/xml");
public static final MimeType JSON = new MimeType("application/json", false, true);
public static final MimeType TEXT_JSON = new MimeType("text/json", false, true);
public static final MimeType CSP_REPORT_URI_JSON = new MimeType("application/csp-report", false, true);
public static final MimeType CSP_REPORT_TO_JSON = new MimeType("application/reports+json", false, true);
}
static
{
for (MimeType mt : Arrays.asList(MimeType.GIF, MimeType.JPEG, MimeType.PDF, MimeType.PNG, MimeType.SVG, MimeType.HTML, MimeType.PLAIN, MimeType.XML,
MimeType.TEXT_JSON, MimeType.JSON, MimeType.CSP_REPORT_URI_JSON, MimeType.CSP_REPORT_TO_JSON))
{
mimeTypeMap.put(mt.getContentType(), mt);
}
try (InputStream is = MimeMap.class.getResourceAsStream("mime.txt"))
{
// tab delimited file is easier to edit in a spreadsheet
List<String> lines = IOUtils.readLines(is, StringUtilsLabKey.DEFAULT_CHARSET);
for (String line : lines)
{
int tab = StringUtils.indexOfAny(line, "\t ");
if (tab < 0) continue;
String extn = StringUtils.trimToNull(line.substring(0,tab));
String mimeType = StringUtils.trimToNull(line.substring(tab+1));
if (null != extn && null != mimeType)
{
MimeType mt = mimeTypeMap.computeIfAbsent(mimeType, MimeType::new);
extensionMap.put(extn, mt);
}
}
}
catch (Exception e)
{
LogHelper.getLogger(MimeMap.class, "Resolves file names and extensions to MIME types").error("Failed loading MIME types", e);
}
}
public MimeType getMimeType(String extn)
{
extn = extn.toLowerCase();
return extensionMap.get(extn);
}
public boolean canInlineFor(String filename)
{
MimeType mime = getMimeTypeFor(filename);
// Don't allow inlining of HTML due to script injection concerns - see issue 38714
return mime != null && mime.canInline() && mime != MimeMap.MimeType.HTML;
}
@Nullable
public MimeType getMimeTypeFor(String fileName)
{
String extn = FileUtil.getExtension(fileName);
if (null == extn)
return null;
return getMimeType(extn);
}
public String getContentType(String extn)
{
MimeType type = getMimeType(extn);
return type == null ? null : type.getContentType();
}
public boolean isInlineImage(String extn)
{
MimeType type = getMimeType(extn);
return type != null && type.isInlineImage();
}
public boolean isInlineImageFor(String fileName)
{
String extn = FileUtil.getExtension(fileName);
if (extn != null)
{
return isInlineImage(extn);
}
else
{
return false;
}
}
public boolean isInlineImageFor(@NotNull File file)
{
String extn = FileUtil.getExtension(file);
if (extn != null)
{
return isInlineImage(extn);
}
else
{
return false;
}
}
public boolean isOfficeDocument(String extn)
{
MimeType mimeType = getMimeType(extn);
if (null == mimeType)
return false;
String contentType = mimeType.getContentType();
if (!contentType.startsWith("application/"))
return false;
String subType = contentType.substring("application/".length());
return (subType.startsWith("vnd.openxmlformats-officedocument") || subType.startsWith("vnd.ms") || subType.startsWith("ms") || subType.equals("x-excel"));
}
public boolean isOfficeDocumentFor(String fileName)
{
String extn = FileUtil.getExtension(fileName);
if (extn != null)
{
return isOfficeDocument(extn);
}
else
{
return false;
}
}
public boolean isJsonContentTypeHeader(String contentType)
{
if (null == contentType)
return false;
var i = contentType.indexOf(";");
if (i > 0)
contentType = contentType.substring(0, i);
var mt = mimeTypeMap.get(contentType.trim());
return null != mt && mt._json;
}
@Override
public String getContentTypeFor(String fileName)
{
String extn = FileUtil.getExtension(fileName);
if (extn != null)
{
return getContentType(extn);
}
else
{
// no extension, no content type
return null;
}
}
public static class TestCase extends Assert
{
@Test
public void testMimeMap()
{
MimeMap m = new MimeMap();
MimeType h = new MimeType("text/html");
assertEquals(h,m.getMimeType("html"));
assertEquals(h.hashCode(), m.getMimeType("html").hashCode());
assertEquals("image/jpeg", m.getContentTypeFor("photo.jpg"));
assertEquals("image/jpeg", m.getContentTypeFor("photo.jpeg"));
assertEquals("text/html", m.getContentTypeFor("file.htm"));
assertEquals("text/html", m.getContentTypeFor("file.html"));
assertTrue(m.isInlineImageFor("photo.jpg"));
assertTrue(m.isInlineImageFor("photo.svg"));
assertFalse(m.isInlineImageFor("file.html"));
MimeType pdf = m.getMimeType("pdf");
assertTrue(pdf.canInline());
assertFalse(pdf.isInlineImage());
assertTrue(m.isOfficeDocument("doc"));
assertTrue(m.isOfficeDocument("docx"));
assertTrue(m.isOfficeDocument("xls"));
assertTrue(m.isOfficeDocument("xlsx"));
assertTrue(m.isOfficeDocument("ppt"));
assertTrue(m.isOfficeDocument("pptx"));
}
}
}