diff options
author | Vitaly Takmazov | 2017-10-15 05:37:18 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2017-10-15 19:50:15 +0300 |
commit | 9a4add44ecbd32dea6ac3d30fd81ae1ac82e3dbc (patch) | |
tree | 38b7a4ba01d1fc47841d3ba2787209b2b2a2de9e /juick-server-web | |
parent | aeda9e3733e685f5069b5cfbb16272cd32073fa0 (diff) |
api: ImagesService
Diffstat (limited to 'juick-server-web')
4 files changed, 91 insertions, 1 deletions
diff --git a/juick-server-web/build.gradle b/juick-server-web/build.gradle index 7df26a9a..7c43d653 100644 --- a/juick-server-web/build.gradle +++ b/juick-server-web/build.gradle @@ -5,7 +5,7 @@ sourceCompatibility = 1.8 dependencies { compile project(':juick-server-core') - + compile project(':juick-server-jdbc') compile "com.fasterxml.jackson.core:jackson-core:${rootProject.jacksonVersion}" compile "com.fasterxml.jackson.core:jackson-databind:${rootProject.jacksonVersion}" compile "com.fasterxml.jackson.core:jackson-annotations:${rootProject.jacksonVersion}" diff --git a/juick-server-web/src/main/java/com/juick/server/configuration/BaseWebConfiguration.java b/juick-server-web/src/main/java/com/juick/server/configuration/BaseWebConfiguration.java index a2422df9..efbe66cb 100644 --- a/juick-server-web/src/main/java/com/juick/server/configuration/BaseWebConfiguration.java +++ b/juick-server-web/src/main/java/com/juick/server/configuration/BaseWebConfiguration.java @@ -21,8 +21,12 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; +import com.juick.configuration.DataConfiguration; +import com.juick.service.ImagesService; +import com.juick.service.ImagesServiceImpl; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.web.multipart.MultipartResolver; @@ -35,6 +39,7 @@ import java.util.List; /** * Created by vitalyster on 28.06.2016. */ +@Import(DataConfiguration.class) @Configuration public class BaseWebConfiguration implements WebMvcConfigurer { @@ -67,4 +72,8 @@ public class BaseWebConfiguration implements WebMvcConfigurer { resolver.setMaxUploadSize(10000000); return resolver; } + @Bean + public ImagesService imagesService() { + return new ImagesServiceImpl(); + } } diff --git a/juick-server-web/src/main/java/com/juick/server/util/ImageUtils.java b/juick-server-web/src/main/java/com/juick/server/util/ImageUtils.java index 25345439..7f21c0a5 100644 --- a/juick-server-web/src/main/java/com/juick/server/util/ImageUtils.java +++ b/juick-server-web/src/main/java/com/juick/server/util/ImageUtils.java @@ -18,6 +18,7 @@ package com.juick.server.util; +import org.apache.commons.imaging.ImageInfo; import org.apache.commons.io.FilenameUtils; import org.apache.commons.imaging.ImageReadException; import org.apache.commons.imaging.Imaging; @@ -149,4 +150,13 @@ public class ImageUtils { ImageIO.write(Scalr.resize(originalImage, 96), targetExt, Paths.get(imgDir, "a", targetName).toFile()); ImageIO.write(Scalr.resize(originalImage, 32), targetExt, Paths.get(imgDir, "as", targetName).toFile()); } + + public static Integer getImageHeight(File imageFile) throws IOException, ImageReadException { + ImageInfo info = Imaging.getImageInfo(imageFile); + return info.getHeight(); + } + public static Integer getImageWidth(File imageFile) throws IOException, ImageReadException { + ImageInfo info = Imaging.getImageInfo(imageFile); + return info.getWidth(); + } }
\ No newline at end of file diff --git a/juick-server-web/src/main/java/com/juick/service/ImagesServiceImpl.java b/juick-server-web/src/main/java/com/juick/service/ImagesServiceImpl.java new file mode 100644 index 00000000..3a5c77dd --- /dev/null +++ b/juick-server-web/src/main/java/com/juick/service/ImagesServiceImpl.java @@ -0,0 +1,71 @@ +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.nio.file.Paths; + +public class ImagesServiceImpl implements ImagesService { + @Override + public void setAttachmentMetadata(String imgDir, 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 = new Attachment(); + original.setUrl(originalUrl); + original.setHeight(ImageUtils.getImageHeight(fullImage)); + original.setWidth(ImageUtils.getImageWidth(fullImage)); + + Attachment medium = new Attachment(); + medium.setUrl(photo.getMedium()); + medium.setWidth(ImageUtils.getImageWidth(mediumImage)); + medium.setHeight(ImageUtils.getImageHeight(mediumImage)); + original.setMedium(medium); + + Attachment small = new Attachment(); + small.setUrl(photo.getSmall()); + small.setWidth(ImageUtils.getImageWidth(smallImage)); + small.setHeight(ImageUtils.getImageHeight(smallImage)); + original.setSmall(small); + + Attachment thumb = new Attachment(); + thumb.setUrl(photo.getMedium()); + thumb.setWidth(ImageUtils.getImageWidth(thumbnailImage)); + thumb.setHeight(ImageUtils.getImageHeight(thumbnailImage)); + original.setThumbnail(thumb); + + msg.setAttachment(original); + } + } +} |