aboutsummaryrefslogtreecommitdiff
path: root/juick-www/src/main/java/com/juick/www/Utils.java
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2016-12-08 10:36:41 +0300
committerGravatar Vitaly Takmazov2016-12-08 15:24:32 +0300
commit70b1e26036175b78213b4350b0f9a504c6d7e9df (patch)
tree8fc9448977a8eb1e8fc5fab9647b1bc81fc204a3 /juick-www/src/main/java/com/juick/www/Utils.java
parente4f1bdaf728e2189d1002a175fe744b80caf8dee (diff)
juick-www: /post2 from perl
Diffstat (limited to 'juick-www/src/main/java/com/juick/www/Utils.java')
-rw-r--r--juick-www/src/main/java/com/juick/www/Utils.java72
1 files changed, 49 insertions, 23 deletions
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 611563c7..b860504e 100644
--- a/juick-www/src/main/java/com/juick/www/Utils.java
+++ b/juick-www/src/main/java/com/juick/www/Utils.java
@@ -17,8 +17,12 @@
*/
package com.juick.www;
+import com.juick.Tag;
import com.juick.User;
+import com.juick.server.TagQueries;
import com.mitchellbosecke.pebble.PebbleEngine;
+import org.apache.commons.codec.digest.DigestUtils;
+import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
@@ -28,13 +32,15 @@ import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
-import java.io.*;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
+import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
-import java.util.UUID;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
+import java.util.*;
/**
*
@@ -50,19 +56,15 @@ public class Utils {
public static String getCookie(HttpServletRequest request, String name) {
Cookie cookies[] = request.getCookies();
if (cookies != null) {
- for (int i = 0; i < cookies.length; i++) {
- if (cookies[i].getName().equals(name)) {
- return cookies[i].getValue();
- }
- }
+ return Arrays.stream(cookies).filter(c ->
+ c.getName().equals(name)).findAny().map(Cookie::getValue).orElse(null);
}
return null;
}
- public static String receiveMultiPartFile(HttpServletRequest request, String name) throws IOException, ServletException {
+ public static String receiveMultiPartFile(Part filePart) throws IOException, ServletException {
String attachmentFName = null;
- Part filePart = request.getPart(name);
if (filePart != null) {
String partname = Utils.getPartFilename(filePart);
if (partname != null && partname.length() > 0) {
@@ -71,7 +73,7 @@ public class Utils {
if (attachmentType.equals("peg")) {
attachmentType = "jpg";
}
- attachmentFName = UUID.randomUUID().toString() + "." + attachmentType;
+ attachmentFName = DigestUtils.md5Hex(UUID.randomUUID().toString()) + "." + attachmentType;
filePart.write(Paths.get(getTmpDir(), attachmentFName).toString());
} else {
throw new IOException("Wrong file type");
@@ -145,16 +147,9 @@ public class Utils {
public static String fetchURL(String url) {
try {
URLConnection c = new URL(url).openConnection();
- BufferedReader in = new BufferedReader(new InputStreamReader(c.getInputStream()));
- String inputLine;
- StringBuilder b = new StringBuilder();
- while ((inputLine = in.readLine()) != null) {
- b.append(inputLine).append("\n");
- }
- in.close();
- return b.toString();
- } catch (Exception e) {
- logger.error("fetchURL" , e);
+ return IOUtils.toString(c.getInputStream(), StandardCharsets.UTF_8);
+ } catch (IOException e) {
+ logger.warn("fetchURL", e);
return null;
}
}
@@ -179,7 +174,7 @@ public class Utils {
throw new Exception("Wrong file type");
}
- attachmentFName = UUID.randomUUID().toString() + "." + attachmentType;
+ attachmentFName = DigestUtils.md5Hex(UUID.randomUUID().toString()) + "." + attachmentType;
fos = new FileOutputStream("/var/www/juick.com/i/tmp/" + attachmentFName);
byte[] buffer = new byte[10240];
int len;
@@ -208,6 +203,37 @@ public class Utils {
}
}
+ public static List<Tag> parseTags(JdbcTemplate sql, String tagsStr) {
+ List<com.juick.Tag> tags = new ArrayList<>();
+ String tagsArr[];
+ if (tagsStr != null && !tagsStr.isEmpty()) {
+ tagsArr = tagsStr.split("[ \\,]");
+ for (int i = 0; i < tagsArr.length; i++) {
+ if (tagsArr[i].startsWith("*")) {
+ tagsArr[i] = tagsArr[i].substring(1);
+ }
+ if (tagsArr[i].length() > 64) {
+ tagsArr[i] = tagsArr[i].substring(0, 64);
+ }
+ }
+ tags = TagQueries.getTags(sql, tagsArr, true);
+ while (tags.size() > 5) {
+ tags.remove(5);
+ }
+ }
+ return tags;
+ }
+
+ public static String receiveAttachment(Part part, String paramImg) throws Exception {
+ String attachmentFName = receiveMultiPartFile(part);
+
+ if (attachmentFName == null && paramImg != null && paramImg.length() > 10) {
+ URL imgUrl = new URL(paramImg);
+ attachmentFName = downloadImage(imgUrl);
+ }
+ return attachmentFName;
+ }
+
public static PebbleEngine getEngine() {
return engine;
}