From f7a2f1440ef0c13a472b1b815186615d2c54168a Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 11 Apr 2018 14:05:43 +0300 Subject: server: ImagesService refactoring --- .../java/com/juick/server/util/ImageUtils.java | 53 ++++++++++------------ 1 file changed, 23 insertions(+), 30 deletions(-) (limited to 'juick-common/src/main/java/com/juick/server/util/ImageUtils.java') 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 BufferedImage, same as ImageIO.read() 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 -- cgit v1.2.3