From 3a64336d9f287c433e97fde18cfd0041cbc1d5f9 Mon Sep 17 00:00:00 2001 From: CalDescent Date: Sat, 18 Mar 2023 17:57:07 +0000 Subject: [PATCH] If the MIME type can't be determined from the file's contents, fall back to using the filename. --- .../org/qortal/api/resource/ArbitraryResource.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/qortal/api/resource/ArbitraryResource.java b/src/main/java/org/qortal/api/resource/ArbitraryResource.java index 84e2d3b0..c4c19652 100644 --- a/src/main/java/org/qortal/api/resource/ArbitraryResource.java +++ b/src/main/java/org/qortal/api/resource/ArbitraryResource.java @@ -14,6 +14,8 @@ import io.swagger.v3.oas.annotations.security.SecurityRequirement; import io.swagger.v3.oas.annotations.tags.Tag; import java.io.*; +import java.net.FileNameMap; +import java.net.URLConnection; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; @@ -1397,7 +1399,16 @@ public class ArbitraryResource { java.nio.file.Path filePath = Paths.get(outputPath.toString(), files[0]); ContentInfoUtil util = new ContentInfoUtil(); ContentInfo info = util.findMatch(filePath.toFile()); - String mimeType = (info != null) ? info.getMimeType() : null; + String mimeType; + if (info != null) { + // Attempt to extract MIME type from file contents + mimeType = info.getMimeType(); + } + else { + // Fall back to using the filename + FileNameMap fileNameMap = URLConnection.getFileNameMap(); + mimeType = fileNameMap.getContentTypeFor(filename); + } fileProperties.filename = filename; fileProperties.mimeType = mimeType; }