diff options
author | Vitaly Takmazov | 2018-09-15 23:36:00 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2018-09-15 23:36:00 +0300 |
commit | 49fa015cb3e069b9b1c62685dfe4996a42f1812f (patch) | |
tree | 80a7f8b0c6fe05abbfcbd912d9874e6b306f5f6e /juick-common/src | |
parent | 14c6c6c061fd460cb33105bfe181d4b1e5997b00 (diff) |
Correctly detect attachments content type
Diffstat (limited to 'juick-common/src')
-rw-r--r-- | juick-common/src/main/java/com/juick/server/util/HttpUtils.java | 36 |
1 files changed, 26 insertions, 10 deletions
diff --git a/juick-common/src/main/java/com/juick/server/util/HttpUtils.java b/juick-common/src/main/java/com/juick/server/util/HttpUtils.java index dbdbc062..9f356aa5 100644 --- a/juick-common/src/main/java/com/juick/server/util/HttpUtils.java +++ b/juick-common/src/main/java/com/juick/server/util/HttpUtils.java @@ -22,6 +22,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.multipart.MultipartFile; +import javax.imageio.ImageIO; +import javax.imageio.ImageReader; +import javax.imageio.stream.ImageInputStream; import java.io.BufferedInputStream; import java.io.IOException; import java.io.InputStream; @@ -30,6 +33,7 @@ import java.net.URL; import java.net.URLConnection; import java.nio.file.Files; import java.nio.file.Paths; +import java.util.Iterator; import java.util.UUID; /** @@ -41,9 +45,15 @@ public class HttpUtils { public static URI receiveMultiPartFile(MultipartFile attach, String tmpDir) throws IOException { if (attach != null && !attach.isEmpty()) { - InputStream attachmentStream = new BufferedInputStream(attach.getInputStream()); - String guessedContentType = URLConnection.guessContentTypeFromStream(attachmentStream); - String attachmentType = attachmentTypeFromContentType(guessedContentType); + ImageInputStream iis = ImageIO.createImageInputStream(attach.getInputStream()); + Iterator<ImageReader> readers = ImageIO.getImageReaders(iis); + + String format = StringUtils.EMPTY; + while (readers.hasNext()) { + ImageReader read = readers.next(); + format = read.getFormatName(); + } + String attachmentType = attachmentTypeFromFormat(format); if (attachmentType.equals("jpg") || attachmentType.equals("png")) { String attachmentFName = DigestUtils.md5Hex(UUID.randomUUID().toString()) + "." + attachmentType; try { @@ -59,17 +69,25 @@ public class HttpUtils { return URI.create(StringUtils.EMPTY); } - private static String attachmentTypeFromContentType(String mime) throws IOException { - if (mime != null && mime.equals("image/jpeg")) { + private static String attachmentTypeFromFormat(String format) throws IOException { + if (format != null && format.equals("JPEG")) { return "jpg"; - } else if (mime != null && mime.equals("image/png")) { + } else if (format != null && format.equals("png")) { return "png"; } else { - throw new IOException("Wrong file type: " + mime); + throw new IOException("Wrong file type: " + format); } } public static URI downloadImage(URL url, String tmpDir) throws IOException { + ImageInputStream iis = ImageIO.createImageInputStream(url.openStream()); + Iterator<ImageReader> readers = ImageIO.getImageReaders(iis); + + String format = StringUtils.EMPTY; + while (readers.hasNext()) { + ImageReader read = readers.next(); + format = read.getFormatName(); + } URLConnection urlConn; try { urlConn = url.openConnection(); @@ -79,9 +97,7 @@ public class HttpUtils { } try (InputStream is = new BufferedInputStream(urlConn.getInputStream())) { - String mime = URLConnection.guessContentTypeFromStream(is); - - String attachmentType = attachmentTypeFromContentType(mime); + String attachmentType = attachmentTypeFromFormat(format); String attachmentFName = DigestUtils.md5Hex(UUID.randomUUID().toString()) + "." + attachmentType; Files.copy(is, Paths.get(tmpDir, attachmentFName)); |