aboutsummaryrefslogtreecommitdiff
path: root/juick-common/src
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
parent6f346454e906d07551c35a7173fd8175b4d0975d (diff)
server: ImagesService refactoring
Diffstat (limited to 'juick-common/src')
-rw-r--r--juick-common/src/main/java/com/juick/server/CommandsManager.java6
-rw-r--r--juick-common/src/main/java/com/juick/server/configuration/StorageConfiguration.java8
-rw-r--r--juick-common/src/main/java/com/juick/server/util/ImageUtils.java53
-rw-r--r--juick-common/src/main/java/com/juick/service/ImagesService.java17
-rw-r--r--juick-common/src/main/java/com/juick/service/ImagesServiceImpl.java31
5 files changed, 70 insertions, 45 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<Method> 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 <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
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);
+ }
}