Skip to content

Commit ff9aeb1

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

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

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,11 @@
2727
import jakarta.activation.MimetypesFileTypeMap;
2828

2929
import java.io.File;
30-
import java.io.IOException;
3130
import java.io.InputStream;
3231
import java.net.MalformedURLException;
3332
import java.net.URISyntaxException;
3433
import java.net.URL;
35-
import java.nio.file.Files;
36-
import java.nio.file.InvalidPathException;
37-
import java.nio.file.Paths;
34+
import java.net.URLConnection;
3835
import java.util.*;
3936
import java.util.stream.Collectors;
4037

@@ -168,26 +165,31 @@ public String getMimeType(String file) {
168165
if (file == null || !file.contains(".")) {
169166
return null;
170167
}
171-
String mimeType = null;
172-
try {
173-
mimeType = Files.probeContentType(Paths.get(file));
174-
} catch (IOException | InvalidPathException e) {
175-
log("unable to probe for content type, will use fallback", e);
176-
}
177168

178-
// MimetypesFileTypeMap is kept for backwards compatibility, remove in 2.0
169+
// this implementation would be nice but returns null on Lambda
170+
// try {
171+
// mimeType = Files.probeContentType(Paths.get(file));
172+
// } catch (IOException | InvalidPathException e) {
173+
// log("unable to probe for content type, will use fallback", e);
174+
// }
175+
179176
if (mimeTypes == null) {
180177
mimeTypes = new MimetypesFileTypeMap();
181178
}
182-
String backwardsCompatibleMimeType = mimeTypes.getContentType(file);
179+
String mimeType = mimeTypes.getContentType(file);
180+
183181
// The getContentType method of the MimetypesFileTypeMap
184182
// returns MimetypesFileTypeMap.defaultType = application/octet-stream
185-
// instead of null when the type cannot be found.
186-
if (mimeType == null || (backwardsCompatibleMimeType != null && !backwardsCompatibleMimeType.equals(mimeType)
187-
&& !MediaType.APPLICATION_OCTET_STREAM.equals(backwardsCompatibleMimeType))) {
188-
log("using type " + backwardsCompatibleMimeType + " from MimetypesFileTypeMap for " + file
189-
+ " instead of " + mimeType + " for backwards compatibility");
190-
mimeType = backwardsCompatibleMimeType;
183+
// instead of null when the type cannot be found. trying to improve the result...
184+
if (mimeType == null || MediaType.APPLICATION_OCTET_STREAM.equals(mimeType)) {
185+
try {
186+
String mimeTypeGuess = URLConnection.guessContentTypeFromName(new File(file).getName());
187+
if (mimeTypeGuess !=null) {
188+
mimeType = mimeTypeGuess;
189+
}
190+
} catch (Exception e) {
191+
log("couldn't find a better contentType than " + mimeType + " for file " + file, e);
192+
}
191193
}
192194

193195
return mimeType;

0 commit comments

Comments
 (0)