aboutsummaryrefslogtreecommitdiff
path: root/juick-server/src/main/java/com/juick/service/ImagesServiceImpl.java
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-09-25 11:53:37 +0300
committerGravatar Vitaly Takmazov2018-09-25 11:53:37 +0300
commitae76024011a8442ae7eab953e0b97e9fe2c7c201 (patch)
tree9ec4bc077ad2db21dcfce0eefa126a484b1d0fc7 /juick-server/src/main/java/com/juick/service/ImagesServiceImpl.java
parent85abc3e201539f81500754a7fb0e4f3a9d148fc4 (diff)
reorganize layout
Diffstat (limited to 'juick-server/src/main/java/com/juick/service/ImagesServiceImpl.java')
-rw-r--r--juick-server/src/main/java/com/juick/service/ImagesServiceImpl.java82
1 files changed, 82 insertions, 0 deletions
diff --git a/juick-server/src/main/java/com/juick/service/ImagesServiceImpl.java b/juick-server/src/main/java/com/juick/service/ImagesServiceImpl.java
new file mode 100644
index 00000000..67c8360e
--- /dev/null
+++ b/juick-server/src/main/java/com/juick/service/ImagesServiceImpl.java
@@ -0,0 +1,82 @@
+package com.juick.service;
+
+import com.juick.Attachment;
+import com.juick.Message;
+import com.juick.Photo;
+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;
+ private String imgDir;
+ private String tmpDir;
+ public ImagesServiceImpl(String imgDir, String tmpDir) {
+ this.imgDir = imgDir;
+ this.tmpDir = tmpDir;
+ imageUtils = new ImageUtils(imgDir, tmpDir);
+ }
+ @Override
+ public void setAttachmentMetadata(String baseUrl, Message msg) throws Exception {
+ if (!StringUtils.isEmpty(msg.getAttachmentType())) {
+ Photo photo = new Photo();
+ if (msg.getRid()> 0) {
+ photo.setSmall(String.format("%sphotos-512/%d-%d.%s", baseUrl, msg.getMid(), msg.getRid(), msg.getAttachmentType()));
+ photo.setMedium(String.format("%sphotos-1024/%d-%d.%s", baseUrl, msg.getMid(), msg.getRid(), msg.getAttachmentType()));
+ photo.setThumbnail(String.format("%sps/%d-%d.%s", baseUrl, msg.getMid(), msg.getRid(), msg.getAttachmentType()));
+ } else {
+ photo.setSmall(String.format("%sphotos-512/%d.%s", baseUrl, msg.getMid(), msg.getAttachmentType()));
+ photo.setMedium(String.format("%sphotos-1024/%d.%s", baseUrl, msg.getMid(), msg.getAttachmentType()));
+ photo.setThumbnail(String.format("%sps/%d.%s", baseUrl, msg.getMid(), msg.getAttachmentType()));
+ }
+ msg.setPhoto(photo);
+ String imageName = String.format("%s.%s", msg.getMid(), msg.getAttachmentType());
+ if (msg.getRid() > 0) {
+ imageName = String.format("%s-%s.%s", msg.getMid(), msg.getRid(), msg.getAttachmentType());
+ }
+ File fullImage = Paths.get(imgDir, "p", imageName).toFile();
+ File mediumImage = Paths.get(imgDir, "photos-1024", imageName).toFile();
+ File smallImage = Paths.get(imgDir, "photos-512", imageName).toFile();
+ File thumbnailImage = Paths.get(imgDir, "ps", imageName).toFile();
+ StringBuilder builder = new StringBuilder();
+ builder.append(baseUrl);
+ builder.append(msg.getAttachmentType().equals("mp4") ? "video" : "p");
+ builder.append("/").append(msg.getMid());
+ if (msg.getRid() > 0) {
+ builder.append("-").append(msg.getRid());
+ }
+ builder.append(".").append(msg.getAttachmentType());
+ String originalUrl = builder.toString();
+
+ Attachment original = imageUtils.getAttachment(fullImage);
+ original.setUrl(originalUrl);
+
+ Attachment medium = imageUtils.getAttachment(mediumImage);
+ medium.setUrl(photo.getMedium());
+ original.setMedium(medium);
+
+ Attachment small = imageUtils.getAttachment(smallImage);
+ small.setUrl(photo.getSmall());
+ original.setSmall(small);
+
+ Attachment thumb = imageUtils.getAttachment(thumbnailImage);
+ thumb.setUrl(photo.getMedium());
+ 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);
+ }
+}