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/CommandsManager.java | 6 ++- .../server/configuration/StorageConfiguration.java | 8 +++- .../java/com/juick/server/util/ImageUtils.java | 53 ++++++++++------------ 3 files changed, 34 insertions(+), 33 deletions(-) (limited to 'juick-common/src/main/java/com/juick/server') diff --git a/juick-common/src/main/java/com/juick/server/CommandsManager.java b/juick-common/src/main/java/com/juick/server/CommandsManager.java index 8dbe99e3..d924ac70 100644 --- a/juick-common/src/main/java/com/juick/server/CommandsManager.java +++ b/juick-common/src/main/java/com/juick/server/CommandsManager.java @@ -74,6 +74,8 @@ public class CommandsManager { private String imgDir; @Inject private ApplicationEventPublisher applicationEventPublisher; + @Inject + private ImagesService imagesService; public CommandResult processCommand(User user, String input, @Nonnull URI attachment) throws Exception { Optional cmd = MethodUtils.getMethodsListWithAnnotation(getClass(), UserCommand.class).stream() @@ -105,7 +107,7 @@ public class CommandsManager { String attachmentFName = attachment.getScheme().equals("juick") ? attachment.getHost() : HttpUtils.downloadImage(attachment.toURL(), tmpDir).getHost(); String fname = String.format("%d.%s", mid, attachmentType); - ImageUtils.saveImageWithPreviews(attachmentFName, fname, tmpDir, imgDir); + imagesService.saveImageWithPreviews(attachmentFName, fname); } com.juick.Message msg = messagesService.getMessage(mid); applicationEventPublisher.publishEvent(new MessageEvent(this, msg)); @@ -480,7 +482,7 @@ public class CommandsManager { String attachmentFName = attachment.getScheme().equals("juick") ? attachment.getHost() : HttpUtils.downloadImage(attachment.toURL(), tmpDir).getHost(); String fname = String.format("%d-%d.%s", mid, newrid, attachmentType); - ImageUtils.saveImageWithPreviews(attachmentFName, fname, tmpDir, imgDir); + imagesService.saveImageWithPreviews(attachmentFName, fname); } Message reply = messagesService.getReply(mid, newrid); applicationEventPublisher.publishEvent(new MessageEvent(this, reply)); diff --git a/juick-common/src/main/java/com/juick/server/configuration/StorageConfiguration.java b/juick-common/src/main/java/com/juick/server/configuration/StorageConfiguration.java index 94b23037..4101f37d 100644 --- a/juick-common/src/main/java/com/juick/server/configuration/StorageConfiguration.java +++ b/juick-common/src/main/java/com/juick/server/configuration/StorageConfiguration.java @@ -2,13 +2,19 @@ package com.juick.server.configuration; import com.juick.service.ImagesService; import com.juick.service.ImagesServiceImpl; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class StorageConfiguration { + + @Value("${upload_tmp_dir:#{systemEnvironment['TEMP'] ?: '/tmp'}}") + private String tmpDir; + @Value("${img_path:#{systemEnvironment['TEMP'] ?: '/tmp'}}") + private String imgDir; @Bean public ImagesService imagesService() { - return new ImagesServiceImpl(); + return new ImagesServiceImpl(imgDir, tmpDir); } } 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