From cfd873f5c5e52eb89b7985555afdb1a180e71620 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Mon, 5 Sep 2016 01:35:19 +0300 Subject: avatar uploading --- juick-www/src/main/java/com/juick/www/Main.java | 9 ++++++--- .../src/main/java/com/juick/www/Settings.java | 22 ++++++++++++++++++++++ juick-www/src/main/java/com/juick/www/Utils.java | 19 +++++++++++++++---- 3 files changed, 43 insertions(+), 7 deletions(-) (limited to 'juick-www/src/main/java/com/juick') diff --git a/juick-www/src/main/java/com/juick/www/Main.java b/juick-www/src/main/java/com/juick/www/Main.java index 231c7f89..fa016edd 100644 --- a/juick-www/src/main/java/com/juick/www/Main.java +++ b/juick-www/src/main/java/com/juick/www/Main.java @@ -69,7 +69,7 @@ public class Main extends HttpServlet implements Stream.StreamListener { VKontakteLogin loginVK = new VKontakteLogin(); TwitterAuth twitterAuth; SignUp signup = new SignUp(); - Settings settings = new Settings(); + Settings settings; RSS rss = new RSS(); ExecutorService executorService = Executors.newSingleThreadScheduledExecutor(); Execution execution; @@ -95,8 +95,11 @@ public class Main extends HttpServlet implements Stream.StreamListener { conf.getProperty("xmpp_password"), NumberUtils.toInt(conf.getProperty("xmpp_port", ""), 5347)); twitterAuth = new TwitterAuth(conf.getProperty("twitter_consumer_key"), conf.getProperty("twitter_consumer_secret")); - pagesNewMessage = new NewMessage(conf.getProperty("upload_tmp_dir", "/var/www/juick.com/i/tmp/"), - conf.getProperty("img_path", "/var/www/juick.com/i/")); + String tmpDir = conf.getProperty("upload_tmp_dir", "/var/www/juick.com/i/tmp/"); + Utils.setTmpDir(tmpDir); + String imgPath = conf.getProperty("img_path", "/var/www/juick.com/i/"); + pagesNewMessage = new NewMessage(tmpDir, imgPath); + settings = new Settings(imgPath); String sapeUser = conf.getProperty("sape_user", ""); if (!Objects.equals(sapeUser, "")) { PageTemplates.sape = new Sape(sapeUser, "juick.com", 2000, 3600); 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("

Your info is updated.

Back to blog.

", visitor.getUName()); } diff --git a/juick-www/src/main/java/com/juick/www/Utils.java b/juick-www/src/main/java/com/juick/www/Utils.java index ee2fa212..4be659cb 100644 --- a/juick-www/src/main/java/com/juick/www/Utils.java +++ b/juick-www/src/main/java/com/juick/www/Utils.java @@ -21,6 +21,7 @@ import com.juick.User; import com.mitchellbosecke.pebble.PebbleEngine; import org.springframework.jdbc.core.JdbcTemplate; +import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -28,6 +29,7 @@ import javax.servlet.http.Part; import java.io.*; import java.net.URL; import java.net.URLConnection; +import java.nio.file.Paths; import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; @@ -42,6 +44,7 @@ public class Utils { private static final Logger logger = Logger.getLogger(Utils.class.getName()); private static final PebbleEngine engine = new PebbleEngine.Builder().build(); + private static String tmpDir = "/var/www/juick.com/i/tmp/"; public static String getCookie(HttpServletRequest request, String name) { Cookie cookies[] = request.getCookies(); @@ -55,10 +58,10 @@ public class Utils { return null; } - public static String receiveMultiPartFile(HttpServletRequest request, String name) throws Exception { + public static String receiveMultiPartFile(HttpServletRequest request, String name) throws IOException, ServletException { String attachmentFName = null; - Part filePart = request.getPart("attach"); + Part filePart = request.getPart(name); if (filePart != null) { String partname = Utils.getPartFilename(filePart); if (partname != null && partname.length() > 0) { @@ -68,9 +71,9 @@ public class Utils { attachmentType = "jpg"; } attachmentFName = UUID.randomUUID().toString() + "." + attachmentType; - filePart.write("/var/www/juick.com/i/tmp/" + attachmentFName); + filePart.write(Paths.get(getTmpDir(), attachmentFName).toString()); } else { - throw new Exception("Wrong file type"); + throw new IOException("Wrong file type"); } } } @@ -299,4 +302,12 @@ public class Utils { public static PebbleEngine getEngine() { return engine; } + + public static String getTmpDir() { + return tmpDir; + } + + public static void setTmpDir(String tmpDir) { + Utils.tmpDir = tmpDir; + } } -- cgit v1.2.3