|
26 | 26 | import javax.ws.rs.core.MediaType;
|
27 | 27 |
|
28 | 28 | import java.io.File;
|
29 |
| -import java.io.IOException; |
30 | 29 | import java.io.InputStream;
|
31 | 30 | import java.net.MalformedURLException;
|
32 | 31 | import java.net.URISyntaxException;
|
33 | 32 | import java.net.URL;
|
34 |
| -import java.nio.file.Files; |
35 |
| -import java.nio.file.InvalidPathException; |
36 |
| -import java.nio.file.Paths; |
| 33 | +import java.net.URLConnection; |
37 | 34 | import java.util.*;
|
38 | 35 | import java.util.stream.Collectors;
|
39 | 36 |
|
@@ -138,26 +135,31 @@ public String getMimeType(String file) {
|
138 | 135 | if (file == null || !file.contains(".")) {
|
139 | 136 | return null;
|
140 | 137 | }
|
141 |
| - String mimeType = null; |
142 |
| - try { |
143 |
| - mimeType = Files.probeContentType(Paths.get(file)); |
144 |
| - } catch (IOException | InvalidPathException e) { |
145 |
| - log("unable to probe for content type, will use fallback", e); |
146 |
| - } |
147 | 138 |
|
148 |
| - // MimetypesFileTypeMap is kept for backwards compatibility, remove in 2.0 |
| 139 | + // this implementation would be nice but returns null on Lambda |
| 140 | +// try { |
| 141 | +// mimeType = Files.probeContentType(Paths.get(file)); |
| 142 | +// } catch (IOException | InvalidPathException e) { |
| 143 | +// log("unable to probe for content type, will use fallback", e); |
| 144 | +// } |
| 145 | + |
149 | 146 | if (mimeTypes == null) {
|
150 | 147 | mimeTypes = new MimetypesFileTypeMap();
|
151 | 148 | }
|
152 |
| - String backwardsCompatibleMimeType = mimeTypes.getContentType(file); |
| 149 | + String mimeType = mimeTypes.getContentType(file); |
| 150 | + |
153 | 151 | // The getContentType method of the MimetypesFileTypeMap
|
154 | 152 | // returns MimetypesFileTypeMap.defaultType = application/octet-stream
|
155 |
| - // instead of null when the type cannot be found. |
156 |
| - if (mimeType == null || (backwardsCompatibleMimeType != null && !backwardsCompatibleMimeType.equals(mimeType) |
157 |
| - && !MediaType.APPLICATION_OCTET_STREAM.equals(backwardsCompatibleMimeType))) { |
158 |
| - log("using type " + backwardsCompatibleMimeType + " from MimetypesFileTypeMap for " + file |
159 |
| - + " instead of " + mimeType + " for backwards compatibility"); |
160 |
| - mimeType = backwardsCompatibleMimeType; |
| 153 | + // instead of null when the type cannot be found. trying to improve the result... |
| 154 | + if (mimeType == null || MediaType.APPLICATION_OCTET_STREAM.equals(mimeType)) { |
| 155 | + try { |
| 156 | + String mimeTypeGuess = URLConnection.guessContentTypeFromName(new File(file).getName()); |
| 157 | + if (mimeTypeGuess !=null) { |
| 158 | + mimeType = mimeTypeGuess; |
| 159 | + } |
| 160 | + } catch (Exception e) { |
| 161 | + log("couldn't find a better contentType than " + mimeType + " for file " + file, e); |
| 162 | + } |
161 | 163 | }
|
162 | 164 |
|
163 | 165 | return mimeType;
|
|
0 commit comments