From 49fa015cb3e069b9b1c62685dfe4996a42f1812f Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sat, 15 Sep 2018 23:36:00 +0300 Subject: Correctly detect attachments content type --- .../main/java/com/juick/server/util/HttpUtils.java | 36 ++++++++++++++++------ 1 file changed, 26 insertions(+), 10 deletions(-) (limited to 'juick-common/src/main/java/com/juick') 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 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 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)); -- cgit v1.2.3