aboutsummaryrefslogtreecommitdiff
path: root/juick-www/src/main/java/com/juick/www
diff options
context:
space:
mode:
Diffstat (limited to 'juick-www/src/main/java/com/juick/www')
-rw-r--r--juick-www/src/main/java/com/juick/www/Main.java9
-rw-r--r--juick-www/src/main/java/com/juick/www/NewMessage.java50
-rw-r--r--juick-www/src/main/java/com/juick/www/Settings.java2
-rw-r--r--juick-www/src/main/java/com/juick/www/Utils.java72
-rw-r--r--juick-www/src/main/java/com/juick/www/XMPPPost.java79
5 files changed, 146 insertions, 66 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 99d06fe6b..27ecf3ec6 100644
--- a/juick-www/src/main/java/com/juick/www/Main.java
+++ b/juick-www/src/main/java/com/juick/www/Main.java
@@ -64,6 +64,7 @@ public class Main extends HttpServlet {
User pagesUser = new User();
UserThread pagesUserThread = new UserThread();
NewMessage pagesNewMessage;
+ XMPPPost xmppPost = new XMPPPost();
FacebookLogin loginFacebook;
VKontakteLogin loginVK = new VKontakteLogin();
TwitterAuth twitterAuth;
@@ -377,6 +378,14 @@ public class Main extends HttpServlet {
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
break;
+ case "/post2":
+ com.juick.User visitor = Utils.getVisitorUser(sql, request, response);
+ if (visitor.getUid() > 0 && !visitor.isBanned()) {
+ xmppPost.doPostMessage(sql, request, response, xmpp, visitor);
+ } else {
+ response.sendError(HttpServletResponse.SC_FORBIDDEN);
+ }
+ break;
default:
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
break;
diff --git a/juick-www/src/main/java/com/juick/www/NewMessage.java b/juick-www/src/main/java/com/juick/www/NewMessage.java
index ca783779e..60ac08def 100644
--- a/juick-www/src/main/java/com/juick/www/NewMessage.java
+++ b/juick-www/src/main/java/com/juick/www/NewMessage.java
@@ -45,7 +45,6 @@ import java.net.URLEncoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
-import java.util.ArrayList;
import java.util.List;
/**
@@ -156,46 +155,17 @@ public class NewMessage {
}
body = body.replace("\r", "");
- String tagsStr = request.getParameter("tags");
- List<com.juick.Tag> tags = new ArrayList<Tag>();
- String tagsArr[] = new String[1];
- 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);
- }
- }
+ List<Tag> tags = Utils.parseTags(sql, request.getParameter("tags"));
- String attachmentFName = null;
+ String attachmentFName;
try {
- attachmentFName = Utils.receiveMultiPartFile(request, "attach");
+ attachmentFName = Utils.receiveAttachment(request.getPart("attach"), request.getParameter("img"));
} catch (Exception e) {
logger.error("MULTIPART ERROR", e);
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
- String paramImg = request.getParameter("img");
- if (attachmentFName == null && paramImg != null && paramImg.length() > 10) {
- try {
- URL imgUrl = new URL(paramImg);
- attachmentFName = Utils.downloadImage(imgUrl);
- } catch (Exception e) {
- logger.error("DOWNLOAD ERROR", e);
- response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
- return;
- }
- }
-
String attachmentType = attachmentFName != null ? attachmentFName.substring(attachmentFName.length() - 3) : null;
int mid = MessagesQueries.createMessage(sql, visitor.getUid(), body, attachmentType, tags);
SubscriptionsQueries.subscribeMessage(sql, mid, visitor.getUid());
@@ -230,11 +200,7 @@ public class NewMessage {
}
if (xmpp != null) {
- String tagsStr2 = "";
- for (String tag : tagsArr) {
- tagsStr2 += " *" + tag;
- }
- xmsg.setBody("@" + jmsg.getUser().getName() + ":" + tagsStr2 + "\n" + body + "\n\n#" + mid + " http://juick.com/" + mid);
+ xmsg.setBody("@" + jmsg.getUser().getName() + ":" + jmsg.getTagsString() + "\n" + body + "\n\n#" + mid + " http://juick.com/" + mid);
xmsg.setTo(Jid.of("juick@s2s.juick.com"));
xmpp.send(xmsg);
@@ -265,13 +231,13 @@ public class NewMessage {
String hashtags = "";
String tagscomma = "";
- for (int i = 0; i < tagsArr.length; i++) {
+ for (int i = 0; i < jmsg.getTags().size(); i++) {
if (i > 0) {
hashtags += " ";
tagscomma += ",";
}
- hashtags += "#" + tagsArr[i];
- tagscomma += tagsArr[i];
+ hashtags += "#" + jmsg.getTags().get(i);
+ tagscomma += jmsg.getTags().get(i);
}
String url = URLEncoder.encode("http://juick.com/" + mid, "utf-8");
@@ -340,7 +306,7 @@ public class NewMessage {
String attachmentFName = null;
try {
- attachmentFName = Utils.receiveMultiPartFile(request, "attach");
+ attachmentFName = Utils.receiveMultiPartFile(request.getPart("attach"));
} catch (Exception e) {
logger.error("MULTIPART ERROR", e);
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
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 89eb67187..56cece344 100644
--- a/juick-www/src/main/java/com/juick/www/Settings.java
+++ b/juick-www/src/main/java/com/juick/www/Settings.java
@@ -155,7 +155,7 @@ public class Settings {
info.setCountry(request.getParameter("country"));
info.setUrl(request.getParameter("url"));
info.setDescription(request.getParameter("descr"));
- String avatarTmpPath = Utils.receiveMultiPartFile(request, "avatar");
+ String avatarTmpPath = Utils.receiveMultiPartFile(request.getPart("avatar"));
if (StringUtils.isNotEmpty(avatarTmpPath)) {
String originalExtension = FilenameUtils.getExtension(avatarTmpPath);
String originalName = String.format("%s.%s", visitor.getUid(), originalExtension);
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 611563c71..b860504ed 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;
}
diff --git a/juick-www/src/main/java/com/juick/www/XMPPPost.java b/juick-www/src/main/java/com/juick/www/XMPPPost.java
new file mode 100644
index 000000000..b2fda6e2d
--- /dev/null
+++ b/juick-www/src/main/java/com/juick/www/XMPPPost.java
@@ -0,0 +1,79 @@
+package com.juick.www;
+
+import com.juick.Tag;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.math.NumberUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.jdbc.core.JdbcTemplate;
+import rocks.xmpp.addr.Jid;
+import rocks.xmpp.core.session.XmppSession;
+import rocks.xmpp.core.stanza.model.Message;
+import rocks.xmpp.extensions.oob.model.x.OobX;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Created by vitalyster on 08.12.2016.
+ */
+public class XMPPPost {
+ private final static Logger logger = LoggerFactory.getLogger(XMPPPost.class);
+
+ public void doPostMessage(JdbcTemplate sql, HttpServletRequest request,
+ HttpServletResponse response, XmppSession xmpp, com.juick.User visitor)
+ throws ServletException, IOException {
+ if (visitor.getUid() == 0) {
+ response.sendError(HttpServletResponse.SC_FORBIDDEN);
+ return;
+ }
+ String body = request.getParameter("body").replace("\r", "");
+ int mid = NumberUtils.toInt(request.getParameter("mid"), 0);
+ int rid = NumberUtils.toInt(request.getParameter("rid"), 0);
+ if (mid > 0 && rid > 0) {
+ body = String.format("#%d/%d %s", mid, rid, body);
+ } else if (mid > 0) {
+ body = String.format("#%d %s", mid, body);
+ } else {
+ // is a post
+ List<Tag> tags = Utils.parseTags(sql, request.getParameter("tags"));
+ body = String.format("%s %s", tags.stream()
+ .map(t -> "*" + t.getName()).collect(Collectors.joining(" ")), body);
+ }
+ String attachmentFName;
+ try {
+ attachmentFName = Utils.receiveAttachment(request.getPart("attach"), request.getParameter("img"));
+ } catch (Exception e) {
+ logger.error("MULTIPART ERROR", e);
+ response.sendError(HttpServletResponse.SC_BAD_REQUEST);
+ return;
+ }
+ Message msg = new Message();
+ msg.setType(Message.Type.CHAT);
+ msg.setFrom(Jid.of(String.valueOf(visitor.getUid()), "uid.juick.com", "perl"));
+ msg.setTo(Jid.of("juick@juick.com/Juick"));
+ msg.setBody(body);
+ try {
+ if (attachmentFName != null) {
+ String attachmentUrl = String.format("juick://%s", attachmentFName);
+ msg.addExtension(new OobX(new URI(attachmentUrl), "!!!!Juick!!"));
+ }
+ xmpp.sendMessage(msg);
+ } catch (URISyntaxException e1) {
+ logger.warn("attachment error", e1);
+ }
+ String referer = request.getHeader("referer");
+ if (StringUtils.isBlank(referer) || referer.substring(0, 21).equals("http://juick.com/post")
+ || referer.substring(0, 22).equals("https://juick.com/post")) {
+ response.sendRedirect("/?show=my");
+ return;
+ }
+ response.sendRedirect(referer);
+ }
+}