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 ++++++++++------------ .../main/java/com/juick/service/ImagesService.java | 17 +++++++ .../java/com/juick/service/ImagesServiceImpl.java | 31 ++++++++----- .../java/com/juick/www/controllers/NewMessage.java | 4 +- .../java/com/juick/www/controllers/Settings.java | 4 +- 7 files changed, 76 insertions(+), 47 deletions(-) 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 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 b5cff16e..192217fe 100644 --- a/juick-common/src/main/java/com/juick/service/ImagesService.java +++ b/juick-common/src/main/java/com/juick/service/ImagesService.java @@ -2,6 +2,23 @@ package com.juick.service; import com.juick.Message; +import java.io.IOException; + public interface ImagesService { void setAttachmentMetadata(String imgDir, String baseUrl, Message msg) throws Exception; + /** + * 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. + */ + void saveImageWithPreviews(String tempFilename, String outputFilename) throws IOException; + /** + * 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. + */ + void saveAvatar(String tempFilename, int uid) throws IOException; } 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 3a5c77dd..dbc3f3a9 100644 --- a/juick-common/src/main/java/com/juick/service/ImagesServiceImpl.java +++ b/juick-common/src/main/java/com/juick/service/ImagesServiceImpl.java @@ -7,9 +7,14 @@ import com.juick.server.util.ImageUtils; import org.springframework.util.StringUtils; import java.io.File; +import java.io.IOException; import java.nio.file.Paths; public class ImagesServiceImpl implements ImagesService { + private ImageUtils imageUtils; + public ImagesServiceImpl(String imgDir, String tmpDir) { + imageUtils = new ImageUtils(imgDir, tmpDir); + } @Override public void setAttachmentMetadata(String imgDir, String baseUrl, Message msg) throws Exception { if (!StringUtils.isEmpty(msg.getAttachmentType())) { @@ -42,30 +47,32 @@ public class ImagesServiceImpl implements ImagesService { builder.append(".").append(msg.getAttachmentType()); String originalUrl = builder.toString(); - Attachment original = new Attachment(); + Attachment original = imageUtils.getAttachment(fullImage); original.setUrl(originalUrl); - original.setHeight(ImageUtils.getImageHeight(fullImage)); - original.setWidth(ImageUtils.getImageWidth(fullImage)); - Attachment medium = new Attachment(); + Attachment medium = imageUtils.getAttachment(mediumImage); medium.setUrl(photo.getMedium()); - medium.setWidth(ImageUtils.getImageWidth(mediumImage)); - medium.setHeight(ImageUtils.getImageHeight(mediumImage)); original.setMedium(medium); - Attachment small = new Attachment(); + Attachment small = imageUtils.getAttachment(smallImage); small.setUrl(photo.getSmall()); - small.setWidth(ImageUtils.getImageWidth(smallImage)); - small.setHeight(ImageUtils.getImageHeight(smallImage)); original.setSmall(small); - Attachment thumb = new Attachment(); + Attachment thumb = imageUtils.getAttachment(thumbnailImage); thumb.setUrl(photo.getMedium()); - thumb.setWidth(ImageUtils.getImageWidth(thumbnailImage)); - thumb.setHeight(ImageUtils.getImageHeight(thumbnailImage)); original.setThumbnail(thumb); msg.setAttachment(original); } } + + @Override + public void saveImageWithPreviews(String tempFilename, String outputFilename) throws IOException { + imageUtils.saveImageWithPreviews(tempFilename, outputFilename); + } + + @Override + public void saveAvatar(String tempFilename, int uid) throws IOException { + imageUtils.saveAvatar(tempFilename, uid); + } } diff --git a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java index 07605830..042a1b0f 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java +++ b/juick-www/src/main/java/com/juick/www/controllers/NewMessage.java @@ -67,6 +67,8 @@ public class NewMessage { private WebApp webApp; @Inject private ExternalComponent xmpp; + @Inject + private ImagesService imagesService; @Value("${img_path:#{systemEnvironment['TEMP'] ?: '/tmp'}}") private String imgDir; @Value("${upload_tmp_dir:#{systemEnvironment['TEMP'] ?: '/tmp'}}") @@ -167,7 +169,7 @@ public class NewMessage { String fname = mid + "-" + ridnew + "." + attachmentType; String attachmentURL = "http://i.juick.com/photos-1024/" + fname; - ImageUtils.saveImageWithPreviews(attachmentFName.getHost(), fname, tmpDir, imgDir); + imagesService.saveImageWithPreviews(attachmentFName.getHost(), fname); body = attachmentURL + "\n" + body; try { diff --git a/juick-www/src/main/java/com/juick/www/controllers/Settings.java b/juick-www/src/main/java/com/juick/www/controllers/Settings.java index 15f18052..3d381815 100644 --- a/juick-www/src/main/java/com/juick/www/controllers/Settings.java +++ b/juick-www/src/main/java/com/juick/www/controllers/Settings.java @@ -76,6 +76,8 @@ public class Settings { private TelegramService telegramService; @Inject private ApplicationEventPublisher applicationEventPublisher; + @Inject + private ImagesService imagesService; @GetMapping("/settings") protected String doGet(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException { @@ -160,7 +162,7 @@ public class Settings { info.setDescription(request.getParameter("descr")); String avatarTmpPath = HttpUtils.receiveMultiPartFile(avatar, tmpDir).getHost(); if (StringUtils.isNotEmpty(avatarTmpPath)) { - ImageUtils.saveAvatar(avatarTmpPath, visitor.getUid(), tmpDir, imgDir); + imagesService.saveAvatar(avatarTmpPath, visitor.getUid()); } if (userService.updateUserInfo(visitor, info)) { applicationEventPublisher.publishEvent(new UserUpdatedEvent(this, visitor)); -- cgit v1.2.3