Skip to content

Commit 1beea50

Browse files
committed
fix: provide more accurate result for content type (another try as Files.probeContentType returns null on Lambda)
1 parent 0675c1e commit 1beea50

File tree

1 file changed

+20
-18
lines changed
  • aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet

1 file changed

+20
-18
lines changed

aws-serverless-java-container-core/src/main/java/com/amazonaws/serverless/proxy/internal/servlet/AwsServletContext.java

+20-18
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,11 @@
2626
import javax.ws.rs.core.MediaType;
2727

2828
import java.io.File;
29-
import java.io.IOException;
3029
import java.io.InputStream;
3130
import java.net.MalformedURLException;
3231
import java.net.URISyntaxException;
3332
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;
3734
import java.util.*;
3835
import java.util.stream.Collectors;
3936

@@ -138,26 +135,31 @@ public String getMimeType(String file) {
138135
if (file == null || !file.contains(".")) {
139136
return null;
140137
}
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-
}
147138

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+
149146
if (mimeTypes == null) {
150147
mimeTypes = new MimetypesFileTypeMap();
151148
}
152-
String backwardsCompatibleMimeType = mimeTypes.getContentType(file);
149+
String mimeType = mimeTypes.getContentType(file);
150+
153151
// The getContentType method of the MimetypesFileTypeMap
154152
// 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+
}
161163
}
162164

163165
return mimeType;

0 commit comments

Comments
 (0)