From eaf87f0422af1a1f86526ea9218cd2193202af64 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 8 Dec 2016 23:08:23 +0300 Subject: juick-api: /post uses xmpp --- .../main/java/com/juick/api/controllers/Post.java | 100 +++------------------ .../main/java/com/juick/api/util/HttpUtils.java | 3 +- 2 files changed, 16 insertions(+), 87 deletions(-) (limited to 'juick-api') diff --git a/juick-api/src/main/java/com/juick/api/controllers/Post.java b/juick-api/src/main/java/com/juick/api/controllers/Post.java index 40c6207e..6d273f7a 100644 --- a/juick-api/src/main/java/com/juick/api/controllers/Post.java +++ b/juick-api/src/main/java/com/juick/api/controllers/Post.java @@ -1,6 +1,5 @@ package com.juick.api.controllers; -import com.juick.Tag; import com.juick.User; import com.juick.api.ApiServer; import com.juick.api.util.HttpBadRequestException; @@ -9,7 +8,6 @@ import com.juick.api.util.HttpNotFoundException; import com.juick.api.util.HttpUtils; import com.juick.service.MessagesService; import com.juick.service.SubscriptionService; -import com.juick.service.TagService; import com.juick.service.UserService; import com.juick.util.UserUtils; import net.coobird.thumbnailator.Thumbnails; @@ -17,12 +15,10 @@ import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.env.Environment; +import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import rocks.xmpp.addr.Jid; import rocks.xmpp.core.stanza.model.Message; @@ -38,8 +34,6 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.security.Principal; -import java.util.ArrayList; -import java.util.List; /** * Created by vt on 24/11/2016. @@ -54,8 +48,6 @@ public class Post { @Inject ApiServer apiServer; @Inject - TagService tagService; - @Inject MessagesService messagesService; @Inject SubscriptionService subscriptionService; @@ -63,10 +55,10 @@ public class Post { Environment env; @RequestMapping(value = "/post", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) - public com.juick.Message doPostMessage( + @ResponseStatus(value = HttpStatus.OK) + public void doPostMessage( Principal principal, @RequestParam String body, - @RequestParam(required = false) String tags, @RequestParam(required = false) String img, @RequestParam(required = false) MultipartFile attach) throws IOException { String name = UserUtils.getUsername(principal, null); @@ -80,24 +72,6 @@ public class Post { } body = body.replace("\r", ""); - List tagsList = new ArrayList<>(); - String tagsArr[] = new String[1]; - if (tags != null && !tags.isEmpty()) { - tagsArr = tags.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); - } - } - tagsList = tagService.getTags(tagsArr, true); - while (tagsList.size() > 5) { - tagsList.remove(5); - } - } - String attachmentFName = HttpUtils.receiveMultiPartFile(attach, env.getProperty("upload_tmp_dir", "/var/www/juick.com/i/tmp/")); @@ -111,67 +85,21 @@ public class Post { } } - String attachmentType = StringUtils.isNotEmpty(attachmentFName) ? attachmentFName.substring(attachmentFName.length() - 3) : null; - int mid = messagesService.createMessage(vuid, body, attachmentType, tagsList); - subscriptionService.subscribeMessage(mid, vuid); - com.juick.Message jmsg = messagesService.getMessage(mid); if (apiServer.getXmpp() != null) { Message xmsg = new Message(); - xmsg.setFrom(Jid.of("juick@juick.com")); - xmsg.setType(Message.Type.CHAT); - xmsg.setThread("juick-" + mid); - - xmsg.addExtension(jmsg); - xmsg.addExtension(new Nickname("@" + jmsg.getUser().getName())); - - if (StringUtils.isNotEmpty(attachmentFName)) { - String fname = mid + "." + attachmentType; - String attachmentURL = "http://i.juick.com/photos-1024/" + fname; - - Path origName = Paths.get(apiServer.imgDir, "p", fname); - Files.move(Paths.get(apiServer.tmpDir, attachmentFName), origName); - Thumbnails.of(origName.toFile()).size(1024, 1024).outputQuality(0.9) - .toFile(Paths.get(apiServer.imgDir, "photos-1024", fname).toFile()); - Thumbnails.of(origName.toFile()).size(512, 512).outputQuality(0.9) - .toFile(Paths.get(apiServer.imgDir, "photos-512", fname).toFile()); - Thumbnails.of(origName.toFile()).size(160, 120).outputQuality(0.9) - .toFile(Paths.get(apiServer.imgDir, "ps", fname).toFile()); - - body = attachmentURL + "\n" + body; - try { - OobX xoob = new OobX(new URI(attachmentURL)); - xmsg.addExtension(xoob); - } catch (URISyntaxException e) { - logger.error("invalid uri: {}, exception {}", attachmentURL, e); + xmsg.setFrom(Jid.of(String.valueOf(visitor.getUid()), "uid.juick.com", "perl")); + xmsg.setTo(Jid.of("juick@juick.com/Juick")); + xmsg.setBody(body); + try { + if (StringUtils.isNotEmpty(attachmentFName)) { + String attachmentUrl = String.format("juick://%s", attachmentFName); + xmsg.addExtension(new OobX(new URI(attachmentUrl), "!!!!Juick!!")); } + apiServer.getXmpp().sendMessage(xmsg); + } catch (URISyntaxException e1) { + logger.warn("attachment error", e1); } - - String tagsStr2 = ""; - for (String tag : tagsArr) { - tagsStr2 += " *" + tag; - } - xmsg.setBody("@" + jmsg.getUser().getName() + ":" + tagsStr2 + "\n" + body + "\n\n#" + mid + " http://juick.com/" + mid); - - xmsg.setTo(Jid.of("juick@s2s.juick.com")); - apiServer.getXmpp().send(xmsg); - - xmsg.setTo(Jid.of("juick@ws.juick.com")); - apiServer.getXmpp().send(xmsg); - - xmsg.setTo(Jid.of("juick@push.juick.com")); - apiServer.getXmpp().send(xmsg); - - xmsg.setTo(Jid.of("twitter@crosspost.juick.com")); - apiServer.getXmpp().send(xmsg); - xmsg.setTo(Jid.of("fb@crosspost.juick.com")); - apiServer.getXmpp().send(xmsg); - - xmsg.setTo(Jid.of("jubo@nologin.ru")); - apiServer.getXmpp().send(xmsg); - } else { - logger.error("XMPP unavailable"); } - return jmsg; } @RequestMapping(value = "/comment", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) diff --git a/juick-api/src/main/java/com/juick/api/util/HttpUtils.java b/juick-api/src/main/java/com/juick/api/util/HttpUtils.java index 93fb7874..ca75eae7 100644 --- a/juick-api/src/main/java/com/juick/api/util/HttpUtils.java +++ b/juick-api/src/main/java/com/juick/api/util/HttpUtils.java @@ -17,6 +17,7 @@ */ package com.juick.api.util; +import org.apache.commons.codec.digest.DigestUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.multipart.MultipartFile; @@ -46,7 +47,7 @@ public class HttpUtils { if (attachmentType.equals("peg")) { attachmentType = "jpg"; } - String attachmentFName = UUID.randomUUID().toString() + "." + attachmentType; + String attachmentFName = DigestUtils.md5Hex(UUID.randomUUID().toString()) + "." + attachmentType; try { Files.write(Paths.get(tmpDir, attachmentFName), attach.getBytes()); -- cgit v1.2.3