From c742da63993d4b15b3968698273ef4f6c449ff13 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 20 Jul 2017 12:40:55 +0300 Subject: api: refactoring --- .../src/main/java/com/juick/api/ApiServer.java | 4 ---- .../main/java/com/juick/api/controllers/Post.java | 28 ++++++++++------------ 2 files changed, 13 insertions(+), 19 deletions(-) (limited to 'juick-api/src/main/java/com/juick') diff --git a/juick-api/src/main/java/com/juick/api/ApiServer.java b/juick-api/src/main/java/com/juick/api/ApiServer.java index b4a7be74..1b2df63f 100644 --- a/juick-api/src/main/java/com/juick/api/ApiServer.java +++ b/juick-api/src/main/java/com/juick/api/ApiServer.java @@ -36,10 +36,6 @@ public class ApiServer implements AutoCloseable { private ExternalComponent xmpp; - @Value("${upload_tmp_dir:/var/www/juick.com/i/tmp/}") - public String tmpDir; - @Value("${img_path:/var/www/juick.com/i/}") - public String imgDir; @Value("${xmpp_host:localhost}") private String xmppHost; @Value("${xmpp_password:secret}") 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 ffabe9ad..261cdd96 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 @@ -2,22 +2,17 @@ package com.juick.api.controllers; import com.juick.User; import com.juick.api.ApiServer; -import com.juick.server.util.HttpBadRequestException; -import com.juick.server.util.HttpForbiddenException; -import com.juick.server.util.HttpNotFoundException; -import com.juick.server.util.HttpUtils; -import com.juick.server.util.ImageUtils; +import com.juick.server.util.*; import com.juick.service.MessagesService; import com.juick.service.SubscriptionService; import com.juick.service.UserService; -import com.juick.server.util.UserUtils; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.mail.util.MimeMessageParser; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.core.env.Environment; +import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; @@ -38,6 +33,7 @@ import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.nio.file.Paths; import java.util.Properties; import java.util.UUID; @@ -56,8 +52,10 @@ public class Post { MessagesService messagesService; @Inject SubscriptionService subscriptionService; - @Inject - Environment env; + @Value("${upload_tmp_dir:/var/www/juick.com/i/tmp/}") + String tmpDir; + @Value("${img_path:/var/www/juick.com/i/}") + String imgDir; @RequestMapping(value = "/post", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE) @ResponseStatus(value = HttpStatus.OK) @@ -75,12 +73,12 @@ public class Post { } body = body.replace("\r", StringUtils.EMPTY); - String attachmentFName = HttpUtils.receiveMultiPartFile(attach, apiServer.tmpDir); + String attachmentFName = HttpUtils.receiveMultiPartFile(attach, tmpDir); if (StringUtils.isBlank(attachmentFName) && img != null && img.length() > 10) { try { URL imgUrl = new URL(img); - attachmentFName = HttpUtils.downloadImage(imgUrl, apiServer.tmpDir); + attachmentFName = HttpUtils.downloadImage(imgUrl, tmpDir); } catch (Exception e) { logger.error("DOWNLOAD ERROR", e); throw new HttpBadRequestException(); @@ -143,11 +141,11 @@ public class Post { throw new HttpForbiddenException(); } - String attachmentFName = HttpUtils.receiveMultiPartFile(attach, apiServer.tmpDir); + String attachmentFName = HttpUtils.receiveMultiPartFile(attach, tmpDir); if (StringUtils.isBlank(attachmentFName) && img != null && img.length() > 10) { try { - attachmentFName = HttpUtils.downloadImage(new URL(img), apiServer.tmpDir); + attachmentFName = HttpUtils.downloadImage(new URL(img), tmpDir); } catch (Exception e) { logger.error("DOWNLOAD ERROR", e); throw new HttpBadRequestException(); @@ -178,7 +176,7 @@ public class Post { String fname = mid + "-" + ridnew + "." + attachmentType; String attachmentURL = "http://i.juick.com/photos-1024/" + fname; - ImageUtils.saveImageWithPreviews(attachmentFName, fname, apiServer.tmpDir, apiServer.imgDir); + ImageUtils.saveImageWithPreviews(attachmentFName, fname, tmpDir, imgDir); body = attachmentURL + "\n" + body; try { @@ -247,7 +245,7 @@ public class Post { attachmentFName[0] = DigestUtils.md5Hex(UUID.randomUUID().toString()) + "." + attachmentType; try { logger.info("got inputstream: {}", a.getInputStream()); - FileOutputStream fos = new FileOutputStream("/var/www/juick.com/i/tmp/" + attachmentFName[0]); + FileOutputStream fos = new FileOutputStream(Paths.get(tmpDir, attachmentFName[0]).toString()); IOUtils.copy(a.getInputStream(), fos); fos.close(); } catch (IOException e) { -- cgit v1.2.3