aboutsummaryrefslogtreecommitdiff
path: root/juick-common/src/main
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-05-17 22:39:28 +0300
committerGravatar Vitaly Takmazov2018-05-17 22:39:28 +0300
commit235bb5b9fc7072531f19cc25c3b07258198d86ec (patch)
tree127e6c113fe04978ff3c43bee9d6da2fa9e1c6d5 /juick-common/src/main
parentc3f5d83b7beed8a523b8a851df742ef028de5efd (diff)
ImagesService refactoring
Diffstat (limited to 'juick-common/src/main')
-rw-r--r--juick-common/src/main/java/com/juick/server/util/ImageUtils.java38
-rw-r--r--juick-common/src/main/java/com/juick/service/ImagesService.java2
-rw-r--r--juick-common/src/main/java/com/juick/service/ImagesServiceImpl.java6
3 files changed, 33 insertions, 13 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
diff --git a/juick-common/src/main/java/com/juick/service/ImagesService.java b/juick-common/src/main/java/com/juick/service/ImagesService.java
index 192217fe..902301ed 100644
--- a/juick-common/src/main/java/com/juick/service/ImagesService.java
+++ b/juick-common/src/main/java/com/juick/service/ImagesService.java
@@ -5,7 +5,7 @@ import com.juick.Message;
import java.io.IOException;
public interface ImagesService {
- void setAttachmentMetadata(String imgDir, String baseUrl, Message msg) throws Exception;
+ void setAttachmentMetadata(String baseUrl, Message msg) throws Exception;
/**
* Move attached image from temp folder to image folder.
* Create preview images in corresponding folders.
diff --git a/juick-common/src/main/java/com/juick/service/ImagesServiceImpl.java b/juick-common/src/main/java/com/juick/service/ImagesServiceImpl.java
index dbc3f3a9..67c8360e 100644
--- a/juick-common/src/main/java/com/juick/service/ImagesServiceImpl.java
+++ b/juick-common/src/main/java/com/juick/service/ImagesServiceImpl.java
@@ -12,11 +12,15 @@ import java.nio.file.Paths;
public class ImagesServiceImpl implements ImagesService {
private ImageUtils imageUtils;
+ private String imgDir;
+ private String tmpDir;
public ImagesServiceImpl(String imgDir, String tmpDir) {
+ this.imgDir = imgDir;
+ this.tmpDir = tmpDir;
imageUtils = new ImageUtils(imgDir, tmpDir);
}
@Override
- public void setAttachmentMetadata(String imgDir, String baseUrl, Message msg) throws Exception {
+ public void setAttachmentMetadata(String baseUrl, Message msg) throws Exception {
if (!StringUtils.isEmpty(msg.getAttachmentType())) {
Photo photo = new Photo();
if (msg.getRid()> 0) {