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 | |
parent | 14c6c6c061fd460cb33105bfe181d4b1e5997b00 (diff) |
Correctly detect attachments content type
-rw-r--r-- | juick-common/src/main/java/com/juick/server/util/HttpUtils.java | 36 | ||||
-rw-r--r-- | juick-server/src/test/java/com/juick/server/tests/ServerTests.java | 14 | ||||
-rw-r--r-- | juick-server/src/test/resources/nojfif.jpg | bin | 0 -> 417629 bytes |
3 files changed, 40 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)); diff --git a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java index 185137b0..8c3e6a01 100644 --- a/juick-server/src/test/java/com/juick/server/tests/ServerTests.java +++ b/juick-server/src/test/java/com/juick/server/tests/ServerTests.java @@ -1147,6 +1147,20 @@ public class ServerTests { assertThat(postJpgCmyk.getNewMessage().get().getAttachment().getSmall().getHeight(), is(512)); } @Test + public void JpegWithoutJfifShouldBeProcessedCorrectly() throws Exception { + CommandResult postJpgCmyk = commandsManager.processCommand(ugnich, "YO", URI.create("classpath:nojfif.jpg")); + assertThat(postJpgCmyk.getNewMessage().isPresent(), is(true)); + int mid = postJpgCmyk.getNewMessage().get().getMid(); + File originalFile = Paths.get(imgDir, "p", String.format("%d.jpg", mid)).toFile(); + assertThat(originalFile.exists(), is(true)); + File mediumFile = Paths.get(imgDir, "photos-1024", String.format("%d.jpg", mid)).toFile(); + assertThat(mediumFile.exists(), is(true)); + assertThat(postJpgCmyk.getNewMessage().get().getAttachment().getWidth(), is(3264)); + assertThat(postJpgCmyk.getNewMessage().get().getAttachment().getHeight(), is(2448)); + assertThat(postJpgCmyk.getNewMessage().get().getAttachment().getMedium().getHeight(), is(768)); + assertThat(postJpgCmyk.getNewMessage().get().getAttachment().getSmall().getHeight(), is(384)); + } + @Test public void JpegFromJuickUriShouldBeProcessedCorrectly() throws Exception { Path tmpFile = Paths.get(tmpDir, "2915104.jpg"); Files.copy(Paths.get(ClassLoader.getSystemResource("2915104.jpg").toURI()), tmpFile, StandardCopyOption.REPLACE_EXISTING); diff --git a/juick-server/src/test/resources/nojfif.jpg b/juick-server/src/test/resources/nojfif.jpg Binary files differnew file mode 100644 index 00000000..16ddec1b --- /dev/null +++ b/juick-server/src/test/resources/nojfif.jpg |