aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar KillyMXI2017-06-20 01:50:33 +0300
committerGravatar KillyMXI2017-06-20 01:50:33 +0300
commit9943851e037f907623cd879dab2f0e8b31f260cf (patch)
treead479d52100caeed458bd3d626d8432d2eb2e31e
parentf3312679fa538122cd81e0e9a0a251ebb296b80e (diff)
All the image processing is moved to ImageUtils class.
Small images keep their size and do not stretch to 512/1024px.
-rw-r--r--juick-api/build.gradle1
-rw-r--r--juick-api/src/main/java/com/juick/api/controllers/Post.java21
-rw-r--r--juick-server/build.gradle2
-rw-r--r--juick-server/src/main/java/com/juick/server/util/ImageUtils.java66
-rw-r--r--juick-www/build.gradle1
-rw-r--r--juick-www/src/main/java/com/juick/www/controllers/NewMessage.java35
-rw-r--r--juick-www/src/main/java/com/juick/www/controllers/Settings.java20
7 files changed, 76 insertions, 70 deletions
diff --git a/juick-api/build.gradle b/juick-api/build.gradle
index d2e52eaf..a1440240 100644
--- a/juick-api/build.gradle
+++ b/juick-api/build.gradle
@@ -15,7 +15,6 @@ dependencies {
compile "org.springframework:spring-websocket:${rootProject.springFrameworkVersion}"
compile 'com.github.pengrad:java-telegram-bot-api:3.0.1'
- compile 'org.imgscalr:imgscalr-lib:4.2'
providedRuntime 'mysql:mysql-connector-java:5.1.40'
testCompile project(path: ':juick-server', configuration: 'testArtifacts')
diff --git a/juick-api/src/main/java/com/juick/api/controllers/Post.java b/juick-api/src/main/java/com/juick/api/controllers/Post.java
index 0173cf04..45eb31e7 100644
--- a/juick-api/src/main/java/com/juick/api/controllers/Post.java
+++ b/juick-api/src/main/java/com/juick/api/controllers/Post.java
@@ -6,13 +6,12 @@ import com.juick.server.util.HttpBadRequestException;
import com.juick.server.util.HttpForbiddenException;
import com.juick.server.util.HttpNotFoundException;
import com.juick.server.util.HttpUtils;
+import com.juick.server.util.ImageUtils;
import com.juick.service.MessagesService;
import com.juick.service.SubscriptionService;
import com.juick.service.UserService;
import com.juick.util.UserUtils;
-import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
-import org.imgscalr.Scalr;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
@@ -25,16 +24,11 @@ import rocks.xmpp.core.stanza.model.Message;
import rocks.xmpp.extensions.nick.model.Nickname;
import rocks.xmpp.extensions.oob.model.x.OobX;
-import javax.imageio.ImageIO;
import javax.inject.Inject;
-import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.Paths;
/**
* Created by vt on 24/11/2016.
@@ -173,18 +167,7 @@ public class Post {
String fname = mid + "-" + ridnew + "." + attachmentType;
String attachmentURL = "http://i.juick.com/photos-1024/" + fname;
- Path origName = Paths.get(apiServer.imgDir, "p", fname);
- Files.move(Paths.get(apiServer.tmpDir, attachmentFName), origName);
- BufferedImage originalImage = ImageIO.read(origName.toFile());
- ImageIO.write(Scalr.resize(originalImage, 1024),
- FilenameUtils.getExtension(origName.toString()),
- Paths.get(apiServer.imgDir, "photos-1024", fname).toFile());
- ImageIO.write(Scalr.resize(originalImage, 512),
- FilenameUtils.getExtension(origName.toString()),
- Paths.get(apiServer.imgDir, "photos-512", fname).toFile());
- ImageIO.write(Scalr.resize(originalImage, 160),
- FilenameUtils.getExtension(origName.toString()),
- Paths.get(apiServer.imgDir, "ps", fname).toFile());
+ ImageUtils.saveImageWithPreviews(attachmentFName, fname, apiServer.tmpDir, apiServer.imgDir);
body = attachmentURL + "\n" + body;
try {
diff --git a/juick-server/build.gradle b/juick-server/build.gradle
index 3e6c8ccd..8f23e8db 100644
--- a/juick-server/build.gradle
+++ b/juick-server/build.gradle
@@ -41,6 +41,8 @@ dependencies {
compile "rocks.xmpp:xmpp-core-client:0.7.4"
compile "rocks.xmpp:xmpp-extensions-client:0.7.4"
+ compile 'org.imgscalr:imgscalr-lib:4.2'
+
providedCompile "javax.servlet:javax.servlet-api:3.1.0"
providedRuntime "commons-fileupload:commons-fileupload:1.3.3"
diff --git a/juick-server/src/main/java/com/juick/server/util/ImageUtils.java b/juick-server/src/main/java/com/juick/server/util/ImageUtils.java
new file mode 100644
index 00000000..61677750
--- /dev/null
+++ b/juick-server/src/main/java/com/juick/server/util/ImageUtils.java
@@ -0,0 +1,66 @@
+
+package com.juick.server.util;
+
+import org.apache.commons.io.FilenameUtils;
+import org.imgscalr.Scalr;
+
+import javax.imageio.ImageIO;
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
+
+public class ImageUtils {
+
+ /**
+ * Move attached image from temp folder to image folder.
+ * Create preview images in corresponding folders.
+ *
+ * @param tempFilename Name of the image file in temp folder.
+ * @param outputFilename Name that will be used in in 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)
+ throws IOException {
+ String ext = FilenameUtils.getExtension(outputFilename);
+
+ Path outputImagePath = Paths.get(imgDir, "p", outputFilename);
+ Files.move(Paths.get(tmpDir, tempFilename), outputImagePath);
+ BufferedImage originalImage = ImageIO.read(outputImagePath.toFile());
+
+ int width = originalImage.getWidth();
+ int height = originalImage.getHeight();
+ int maxDimension = (width > height) ? width : height;
+ BufferedImage image1024 = (maxDimension > 1024) ? Scalr.resize(originalImage, 1024) : originalImage;
+ BufferedImage image0512 = (maxDimension > 512) ? Scalr.resize(originalImage, 512) : originalImage;
+ BufferedImage image0160 = (maxDimension > 160) ? Scalr.resize(originalImage, 160) : originalImage;
+ ImageIO.write(image1024, ext, Paths.get(imgDir, "photos-1024", outputFilename).toFile());
+ ImageIO.write(image0512, ext, Paths.get(imgDir, "photos-512", outputFilename).toFile());
+ 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 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)
+ throws IOException {
+ String ext = FilenameUtils.getExtension(tempFilename);
+ String originalName = String.format("%s.%s", uid, ext);
+ Path originalPath = Paths.get(imgDir, "ao", originalName);
+ Files.move(Paths.get(tmpDir, tempFilename), originalPath, StandardCopyOption.REPLACE_EXISTING);
+ BufferedImage originalImage = ImageIO.read(originalPath.toFile());
+
+ String targetExt = "png";
+ String targetName = String.format("%s.%s", uid, targetExt);
+ 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());
+ }
+} \ No newline at end of file
diff --git a/juick-www/build.gradle b/juick-www/build.gradle
index ab0480f4..55a15a83 100644
--- a/juick-www/build.gradle
+++ b/juick-www/build.gradle
@@ -20,7 +20,6 @@ apply plugin: 'org.akhikhl.gretty'
dependencies {
compile project(':juick-server')
compile 'com.github.scribejava:scribejava-apis:4.1.1'
- compile 'org.imgscalr:imgscalr-lib:4.2'
compile 'com.github.ooxi:serialized-php-parser:0.5.0'
compile 'com.sun.mail:javax.mail:1.5.6'
compile "org.springframework:spring-webmvc:${rootProject.springFrameworkVersion}"
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 1931b1d9..4dcf6c10 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
@@ -24,15 +24,14 @@ import com.juick.server.util.HttpBadRequestException;
import com.juick.server.util.HttpForbiddenException;
import com.juick.server.util.HttpNotFoundException;
import com.juick.server.util.HttpUtils;
+import com.juick.server.util.ImageUtils;
import com.juick.service.*;
import com.juick.util.UserUtils;
import com.juick.www.Utils;
import com.juick.www.WebApp;
-import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.CharEncoding;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
-import org.imgscalr.Scalr;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
@@ -47,11 +46,9 @@ import rocks.xmpp.core.stanza.model.Message;
import rocks.xmpp.extensions.nick.model.Nickname;
import rocks.xmpp.extensions.oob.model.x.OobX;
-import javax.imageio.ImageIO;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
@@ -59,9 +56,6 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.Paths;
import java.util.List;
/**
@@ -226,18 +220,7 @@ public class NewMessage {
String fname = mid + "." + attachmentType;
String attachmentURL = "http://i.juick.com/photos-1024/" + fname;
- Path origName = Paths.get(webApp.getImgDir(), "p", fname);
- Files.move(Paths.get(webApp.getTmpDir(), attachmentFName), origName);
- BufferedImage originalImage = ImageIO.read(origName.toFile());
- ImageIO.write(Scalr.resize(originalImage, 1024),
- FilenameUtils.getExtension(origName.toString()),
- Paths.get(webApp.getImgDir(), "photos-1024", fname).toFile());
- ImageIO.write(Scalr.resize(originalImage, 512),
- FilenameUtils.getExtension(origName.toString()),
- Paths.get(webApp.getImgDir(), "photos-512", fname).toFile());
- ImageIO.write(Scalr.resize(originalImage, 160),
- FilenameUtils.getExtension(origName.toString()),
- Paths.get(webApp.getImgDir(), "ps", fname).toFile());
+ ImageUtils.saveImageWithPreviews(attachmentFName, fname, webApp.getTmpDir(), webApp.getImgDir());
body = attachmentURL + "\n" + body;
try {
@@ -385,18 +368,8 @@ public class NewMessage {
String fname = mid + "-" + ridnew + "." + attachmentType;
String attachmentURL = "http://i.juick.com/photos-1024/" + fname;
- Path origName = Paths.get(webApp.getImgDir(), "p", fname);
- Files.move(Paths.get(webApp.getTmpDir(), attachmentFName), origName);
- BufferedImage originalImage = ImageIO.read(origName.toFile());
- ImageIO.write(Scalr.resize(originalImage, 1024),
- FilenameUtils.getExtension(origName.toString()),
- Paths.get(webApp.getImgDir(), "photos-1024", fname).toFile());
- ImageIO.write(Scalr.resize(originalImage, 512),
- FilenameUtils.getExtension(origName.toString()),
- Paths.get(webApp.getImgDir(), "photos-512", fname).toFile());
- ImageIO.write(Scalr.resize(originalImage, 160),
- FilenameUtils.getExtension(origName.toString()),
- Paths.get(webApp.getImgDir(), "ps", fname).toFile());
+ ImageUtils.saveImageWithPreviews(attachmentFName, fname, webApp.getTmpDir(), webApp.getImgDir());
+
body = attachmentURL + "\n" + body;
try {
xmsg.addExtension(new OobX(new URI(attachmentURL)));
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 3c4511c1..d2b65661 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
@@ -21,12 +21,11 @@ import com.juick.server.helpers.NotifyOpts;
import com.juick.server.helpers.UserInfo;
import com.juick.server.util.HttpBadRequestException;
import com.juick.server.util.HttpUtils;
+import com.juick.server.util.ImageUtils;
import com.juick.service.*;
import com.juick.util.UserUtils;
import com.juick.www.WebApp;
-import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
-import org.imgscalr.Scalr;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
@@ -36,7 +35,6 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
-import javax.imageio.ImageIO;
import javax.inject.Inject;
import javax.mail.Message;
import javax.mail.MessagingException;
@@ -48,12 +46,7 @@ import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import java.awt.image.BufferedImage;
import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.Paths;
-import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@@ -167,16 +160,7 @@ public class Settings {
info.setDescription(request.getParameter("descr"));
String avatarTmpPath = HttpUtils.receiveMultiPartFile(avatar, webApp.getTmpDir());
if (StringUtils.isNotEmpty(avatarTmpPath)) {
- String originalExtension = FilenameUtils.getExtension(avatarTmpPath);
- String originalName = String.format("%s.%s", visitor.getUid(), originalExtension);
- String targetName = String.format("%s.png", visitor.getUid());
- Path ao = Paths.get(webApp.getImgDir(), "ao", originalName);
- Path a = Paths.get(webApp.getImgDir(), "a", targetName);
- Path as = Paths.get(webApp.getImgDir(), "as", targetName);
- Files.move(Paths.get(webApp.getTmpDir(), avatarTmpPath), ao, StandardCopyOption.REPLACE_EXISTING);
- BufferedImage originalImage = ImageIO.read(ao.toFile());
- ImageIO.write(Scalr.resize(originalImage, 96), "png", a.toFile());
- ImageIO.write(Scalr.resize(originalImage, 32), "png", as.toFile());
+ ImageUtils.saveAvatar(avatarTmpPath, visitor.getUid(), webApp.getTmpDir(), webApp.getImgDir());
}
if (userService.updateUserInfo(visitor, info)) {
result = String.format("<p>Your info is updated.</p><p><a href='/%s/'>Back to blog</a>.</p>", visitor.getName());