diff options
author | Vitaly Takmazov | 2018-05-17 22:39:28 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2018-05-17 22:39:28 +0300 |
commit | 235bb5b9fc7072531f19cc25c3b07258198d86ec (patch) | |
tree | 127e6c113fe04978ff3c43bee9d6da2fa9e1c6d5 /juick-common/src/main/java/com/juick/server | |
parent | c3f5d83b7beed8a523b8a851df742ef028de5efd (diff) |
ImagesService refactoring
Diffstat (limited to 'juick-common/src/main/java/com/juick/server')
-rw-r--r-- | juick-common/src/main/java/com/juick/server/util/ImageUtils.java | 38 |
1 files changed, 27 insertions, 11 deletions
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 71dec381..cea4cc88 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 @@ -19,7 +19,6 @@ package com.juick.server.util; import com.juick.Attachment; -import org.apache.commons.imaging.ImageInfo; import org.apache.commons.imaging.ImageReadException; import org.apache.commons.imaging.Imaging; import org.apache.commons.imaging.common.ImageMetadata; @@ -33,6 +32,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.imageio.ImageIO; +import javax.imageio.ImageReader; +import javax.imageio.stream.FileImageInputStream; +import javax.imageio.stream.ImageInputStream; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; @@ -40,6 +42,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; +import java.util.Iterator; public class ImageUtils { private static final Logger logger = LoggerFactory.getLogger(ImageUtils.class); @@ -144,19 +147,32 @@ public class ImageUtils { ImageIO.write(Scalr.resize(originalImage, 96), targetExt, Paths.get(imgDir, "a", targetName).toFile()); ImageIO.write(Scalr.resize(originalImage, 32), targetExt, Paths.get(imgDir, "as", targetName).toFile()); } - - public Attachment getAttachment(File imageFile) throws IOException { + public Attachment getAttachment(File imgFile) throws IOException { Attachment attachment = new Attachment(); - if (imageFile.exists()) { + int pos = imgFile.getName().lastIndexOf("."); + if (pos == -1) + throw new IOException("No extension for file: " + imgFile.getAbsolutePath()); + String suffix = imgFile.getName().substring(pos + 1); + Iterator<ImageReader> iter = ImageIO.getImageReadersBySuffix(suffix); + while(iter.hasNext()) { + ImageReader reader = iter.next(); + ImageInputStream stream = null; try { - ImageInfo info = Imaging.getImageInfo(imageFile); - attachment.setHeight(info.getHeight()); - attachment.setWidth(info.getWidth()); - } catch (ImageReadException e) { - logger.info("Can not read {}, moved to {}", imageFile.toPath(), - Files.move(imageFile.toPath(), Paths.get(tmpDir, imageFile.getName()), StandardCopyOption.REPLACE_EXISTING)); + stream = new FileImageInputStream(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); + } finally { + if (stream != null) { + stream.close(); + } + reader.dispose(); } } - return attachment; + + throw new IOException("Not a known image file: " + imgFile.getAbsolutePath()); } }
\ No newline at end of file |