From 10ee29dcb05f992fe2b7aa726b6ac7c9e9aba87d Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 23 Jul 2018 15:47:54 +0300 Subject: Add TwelveMonkeys jpeg library to handle JPG CMYK and other exotic jpegs --- .../main/java/com/juick/server/util/ImageUtils.java | 11 +++-------- juick-server/build.gradle | 1 + .../test/java/com/juick/server/tests/ServerTests.java | 14 ++++++++++++++ juick-server/src/test/resources/cmyk.jpg | Bin 0 -> 3945732 bytes 4 files changed, 18 insertions(+), 8 deletions(-) create mode 100644 juick-server/src/test/resources/cmyk.jpg diff --git a/juick-common/src/main/java/com/juick/server/util/ImageUtils.java b/juick-common/src/main/java/com/juick/server/util/ImageUtils.java index cea4cc88..d6a455a4 100644 --- a/juick-common/src/main/java/com/juick/server/util/ImageUtils.java +++ b/juick-common/src/main/java/com/juick/server/util/ImageUtils.java @@ -156,19 +156,14 @@ public class ImageUtils { Iterator iter = ImageIO.getImageReadersBySuffix(suffix); while(iter.hasNext()) { ImageReader reader = iter.next(); - ImageInputStream stream = null; - try { - stream = new FileImageInputStream(imgFile); + try (ImageInputStream stream = ImageIO.createImageInputStream(imgFile)) { reader.setInput(stream); attachment.setWidth(reader.getWidth(reader.getMinIndex())); attachment.setHeight(reader.getHeight(reader.getMinIndex())); return attachment; - } catch (IOException e) { - logger.warn("Error reading: " + imgFile.getAbsolutePath(), e); + } catch (Exception e) { + logger.debug("Error reading {}, trying next reader", imgFile.getAbsolutePath()); } finally { - if (stream != null) { - stream.close(); - } reader.dispose(); } } diff --git a/juick-server/build.gradle b/juick-server/build.gradle index 71960d5a..a28f6efa 100644 --- a/juick-server/build.gradle +++ b/juick-server/build.gradle @@ -18,6 +18,7 @@ dependencies { compile 'com.github.pengrad:java-telegram-bot-api:3.6.0' compile 'com.github.messenger4j:messenger4j:1.0.0' compile 'org.springframework.social:spring-social-twitter:1.1.2.RELEASE' + compile 'com.twelvemonkeys.imageio:imageio-jpeg:3.3.2' compile 'org.imgscalr:imgscalr-lib:4.2' compile 'org.twitter4j:twitter4j-core:4.0.6' 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 3ac4ff82..2d45aae0 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 @@ -1084,4 +1084,18 @@ public class ServerTests { commandsManager.processCommand(ugnich, "BL @freefd", emptyUri); messagesService.setLastReadComment(ugnich, mid, 5); } + @Test + public void cmykJpegShouldBeProcessedCorrectly() throws Exception { + CommandResult postJpgCmyk = commandsManager.processCommand(ugnich, "YO", URI.create("classpath:cmyk.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(2585)); + assertThat(postJpgCmyk.getNewMessage().get().getAttachment().getHeight(), is(3335)); + assertThat(postJpgCmyk.getNewMessage().get().getAttachment().getMedium().getHeight(), is(1024)); + assertThat(postJpgCmyk.getNewMessage().get().getAttachment().getSmall().getHeight(), is(512)); + } } diff --git a/juick-server/src/test/resources/cmyk.jpg b/juick-server/src/test/resources/cmyk.jpg new file mode 100644 index 00000000..40af259c Binary files /dev/null and b/juick-server/src/test/resources/cmyk.jpg differ -- cgit v1.2.3