aboutsummaryrefslogtreecommitdiff
path: root/juick-common/src/main/java/com/juick/server/util
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-04-11 14:05:43 +0300
committerGravatar Vitaly Takmazov2018-04-11 14:05:43 +0300
commitf7a2f1440ef0c13a472b1b815186615d2c54168a (patch)
tree2270b2bd9948cdb809e40942b63cf4c84172a53a /juick-common/src/main/java/com/juick/server/util
parent6f346454e906d07551c35a7173fd8175b4d0975d (diff)
server: ImagesService refactoring
Diffstat (limited to 'juick-common/src/main/java/com/juick/server/util')
-rw-r--r--juick-common/src/main/java/com/juick/server/util/ImageUtils.java53
1 files changed, 23 insertions, 30 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 94ecf71e..41455da4 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
@@ -18,6 +18,7 @@
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;
@@ -28,6 +29,8 @@ import org.apache.commons.imaging.formats.tiff.constants.TiffTagConstants;
import org.apache.commons.io.FilenameUtils;
import org.imgscalr.Scalr;
import org.imgscalr.Scalr.Rotation;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
@@ -39,7 +42,15 @@ import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
public class ImageUtils {
+ private static final Logger logger = LoggerFactory.getLogger(ImageUtils.class);
+ private String imgDir;
+ private String tmpDir;
+
+ public ImageUtils(String imgDir, String tmpDir) {
+ this.imgDir = imgDir;
+ this.tmpDir = tmpDir;
+ }
/**
* Returns <code>BufferedImage</code>, same as <code>ImageIO.read()</code> does.
*
@@ -101,16 +112,7 @@ public class ImageUtils {
return image;
}
- /**
- * Move attached image from temp folder to image folder.
- * Create preview images in corresponding folders.
- *
- * @param tempFilename Name of the image file in the temp folder.
- * @param outputFilename Name that will be used in the image folder.
- * @param tmpDir Path string for the temp folder.
- * @param imgDir Path string for the image folder.
- */
- public static void saveImageWithPreviews(String tempFilename, String outputFilename, String tmpDir, String imgDir)
+ public void saveImageWithPreviews(String tempFilename, String outputFilename)
throws IOException {
String ext = FilenameUtils.getExtension(outputFilename);
@@ -129,15 +131,7 @@ public class ImageUtils {
ImageIO.write(image0160, ext, Paths.get(imgDir, "ps", outputFilename).toFile());
}
- /**
- * Save new avatar in all required sizes.
- *
- * @param tempFilename Name of the image file in the temp folder.
- * @param uid User id that is used to build image file names.
- * @param tmpDir Path string for the temp folder.
- * @param imgDir Path string for the image folder.
- */
- public static void saveAvatar(String tempFilename, int uid, String tmpDir, String imgDir)
+ public void saveAvatar(String tempFilename, int uid)
throws IOException {
String ext = FilenameUtils.getExtension(tempFilename);
String originalName = String.format("%s.%s", uid, ext);
@@ -151,18 +145,17 @@ public class ImageUtils {
ImageIO.write(Scalr.resize(originalImage, 32), targetExt, Paths.get(imgDir, "as", targetName).toFile());
}
- public static Integer getImageHeight(File imageFile) throws IOException, ImageReadException {
- if (imageFile.exists()) {
- ImageInfo info = Imaging.getImageInfo(imageFile);
- return info.getHeight();
- }
- return 0;
- }
- public static Integer getImageWidth(File imageFile) throws IOException, ImageReadException {
+ public Attachment getAttachment(File imageFile) throws IOException {
+ Attachment attachment = new Attachment();
if (imageFile.exists()) {
- ImageInfo info = Imaging.getImageInfo(imageFile);
- return info.getWidth();
+ 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), StandardCopyOption.REPLACE_EXISTING));
+ }
}
- return 0;
+ return attachment;
}
} \ No newline at end of file