diff options
author | Vitaly Takmazov | 2016-09-05 01:35:19 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-09-05 01:46:49 +0300 |
commit | cfd873f5c5e52eb89b7985555afdb1a180e71620 (patch) | |
tree | 79a8dead3e255215c929fa06172adf42ea8a7254 /juick-www/src | |
parent | 7523a0a69ebe45afc98394f52ce14ac530ca9b18 (diff) |
avatar uploading
Diffstat (limited to 'juick-www/src')
-rw-r--r-- | juick-www/src/main/java/com/juick/www/Main.java | 9 | ||||
-rw-r--r-- | juick-www/src/main/java/com/juick/www/Settings.java | 22 | ||||
-rw-r--r-- | juick-www/src/main/java/com/juick/www/Utils.java | 19 |
3 files changed, 43 insertions, 7 deletions
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("<p>Your info is updated.</p><p><a href='/%s/'>Back to blog</a>.</p>", 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; + } } |