aboutsummaryrefslogtreecommitdiff
path: root/juick-www/src/main/java/com/juick/www/Settings.java
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-09-05 01:35:19 +0300
committerGravatar Vitaly Takmazov2016-09-05 01:46:49 +0300
commitcfd873f5c5e52eb89b7985555afdb1a180e71620 (patch)
tree79a8dead3e255215c929fa06172adf42ea8a7254 /juick-www/src/main/java/com/juick/www/Settings.java
parent7523a0a69ebe45afc98394f52ce14ac530ca9b18 (diff)
avatar uploading
Diffstat (limited to 'juick-www/src/main/java/com/juick/www/Settings.java')
-rw-r--r--juick-www/src/main/java/com/juick/www/Settings.java22
1 files changed, 22 insertions, 0 deletions
diff --git a/juick-www/src/main/java/com/juick/www/Settings.java b/juick-www/src/main/java/com/juick/www/Settings.java
index c04a63fb..0602bff1 100644
--- a/juick-www/src/main/java/com/juick/www/Settings.java
+++ b/juick-www/src/main/java/com/juick/www/Settings.java
@@ -25,6 +25,8 @@ import com.juick.server.helpers.NotifyOpts;
import com.juick.server.helpers.UserInfo;
import com.mitchellbosecke.pebble.error.PebbleException;
import com.mitchellbosecke.pebble.template.PebbleTemplate;
+import net.coobird.thumbnailator.Thumbnails;
+import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
@@ -39,8 +41,13 @@ import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.Part;
+import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@@ -56,6 +63,11 @@ import java.util.stream.IntStream;
*/
public class Settings {
private static final Logger logger = Logger.getLogger(Settings.class.getName());
+ private final String imgPath;
+
+ public Settings(String avatarsPath) {
+ this.imgPath = avatarsPath;
+ }
protected void doGet(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, PebbleException {
com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
@@ -143,6 +155,16 @@ public class Settings {
info.setCountry(request.getParameter("country"));
info.setUrl(request.getParameter("url"));
info.setDescription(request.getParameter("descr"));
+ String avatarTmpPath = Utils.receiveMultiPartFile(request, "avatar");
+ if (StringUtils.isNotEmpty(avatarTmpPath)) {
+ String targetName = String.format("%s.%s", visitor.getUID(), FilenameUtils.getExtension(avatarTmpPath));
+ Path ao = Paths.get(imgPath, "ao", targetName);
+ Path a = Paths.get(imgPath, "a", targetName);
+ Path as = Paths.get(imgPath, "as", targetName);
+ Files.move(Paths.get(Utils.getTmpDir(), avatarTmpPath), ao);
+ Thumbnails.of(ao.toFile()).size(96, 96).toFile(a.toFile());
+ Thumbnails.of(ao.toFile()).size(32, 32).toFile(as.toFile());
+ }
if (UserQueries.updateUserInfo(sql, visitor, info)) {
result = String.format("<p>Your info is updated.</p><p><a href='/%s/'>Back to blog</a>.</p>", visitor.getUName());
}