aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/juick/www/api/Post.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/juick/www/api/Post.java')
-rw-r--r--src/main/java/com/juick/www/api/Post.java72
1 files changed, 35 insertions, 37 deletions
diff --git a/src/main/java/com/juick/www/api/Post.java b/src/main/java/com/juick/www/api/Post.java
index 18b02445..57c23703 100644
--- a/src/main/java/com/juick/www/api/Post.java
+++ b/src/main/java/com/juick/www/api/Post.java
@@ -48,7 +48,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.HttpStatus;
+import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@@ -74,38 +76,36 @@ public class Post {
@RequestMapping(value = "/api/post", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(value = HttpStatus.OK)
- public CommandResult doPostMessage(
+ public ResponseEntity<?> doPostMessage(
@Parameter(hidden = true) User visitor,
@RequestParam(required = false, defaultValue = StringUtils.EMPTY) String body,
@RequestParam(required = false) String img,
@RequestParam(required = false) MultipartFile attach) throws Exception {
body = body.replace("\r", StringUtils.EMPTY);
-
- URI attachmentFName = HttpUtils.receiveMultiPartFile(attach, storageService.getTemporaryDirectory());
-
- if (StringUtils.isBlank(attachmentFName.toString()) && img != null && img.length() > 10) {
- URI juickUri = URI.create(img);
- if (juickUri.getScheme().equals("juick")) {
- attachmentFName = juickUri;
- } else {
- try {
+ try {
+ URI attachmentFName = HttpUtils.receiveMultiPartFile(attach, storageService.getTemporaryDirectory());
+ if (StringUtils.isBlank(attachmentFName.toString()) && img != null && img.length() > 10) {
+ URI juickUri = URI.create(img);
+ if (juickUri.getScheme().equals("juick")) {
+ attachmentFName = juickUri;
+ } else {
URL imgUrl = new URL(img);
attachmentFName = HttpUtils.downloadImage(imgUrl, storageService.getTemporaryDirectory());
- } catch (Exception e) {
- logger.error("DOWNLOAD ERROR", e);
- throw new HttpBadRequestException();
}
}
+ if (StringUtils.isBlank(body) && StringUtils.isBlank(attachmentFName.toString())) {
+ // Should be there for compatibility
+ return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(CommandResult.fromString("Empty message"));
+ }
+ return ResponseEntity.ok(commandsManager.processCommand(visitor, body, attachmentFName));
+ } catch (Exception e) {
+ logger.error("DOWNLOAD ERROR", e);
+ return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(CommandResult.fromString(e.getMessage()));
}
- if (StringUtils.isBlank(body) && StringUtils.isBlank(attachmentFName.toString())) {
- // Should be there for compatibility
- throw new HttpBadRequestException();
- }
- return commandsManager.processCommand(visitor, body, attachmentFName);
}
@RequestMapping(value = "/api/comment", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
- public CommandResult doPostComment(
+ public ResponseEntity<?> doPostComment(
@Parameter(hidden = true) User visitor,
@RequestParam(defaultValue = "0") int mid,
@RequestParam(defaultValue = "0") int rid,
@@ -114,11 +114,11 @@ public class Post {
@RequestParam(required = false) MultipartFile attach)
throws Exception {
if (mid == 0) {
- throw new HttpBadRequestException();
+ return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(CommandResult.fromString("Invalid mid"));
}
Optional<Message> message = messagesService.getMessage(mid);
if (message.isEmpty()) {
- throw new HttpNotFoundException();
+ return ResponseEntity.status(HttpStatus.NOT_FOUND).body(CommandResult.fromString("Message not found"));
}
Message msg = message.get();
@@ -127,7 +127,7 @@ public class Post {
if (rid > 0) {
reply = messagesService.getReply(mid, rid);
if (reply == null) {
- throw new HttpNotFoundException();
+ return ResponseEntity.status(HttpStatus.NOT_FOUND).body(CommandResult.fromString("Reply not found"));
}
}
@@ -135,25 +135,23 @@ public class Post {
|| userService.isInBL(visitor.getUid(), msg.getUser().getUid())
|| (reply != null && userService.isInBL(visitor.getUid(), reply.getUser().getUid()))) {
// TODO: validator
- throw new HttpForbiddenException();
+ return ResponseEntity.status(HttpStatus.FORBIDDEN).body(CommandResult.fromString("Forbidden"));
}
-
- URI attachmentFName = HttpUtils.receiveMultiPartFile(attach, storageService.getTemporaryDirectory());
-
- if (StringUtils.isBlank(attachmentFName.toString()) && img != null && img.length() > 10) {
- try {
+ try {
+ URI attachmentFName = HttpUtils.receiveMultiPartFile(attach, storageService.getTemporaryDirectory());
+ if (StringUtils.isBlank(attachmentFName.toString()) && img != null && img.length() > 10) {
attachmentFName = HttpUtils.downloadImage(new URL(img), storageService.getTemporaryDirectory());
- } catch (Exception e) {
- logger.error("DOWNLOAD ERROR", e);
- throw new HttpBadRequestException();
}
+ if (StringUtils.isBlank(body) && StringUtils.isBlank(attachmentFName.toString())) {
+ // Should be there for compatibility
+ return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(CommandResult.fromString("Empty message"));
+ }
+ return ResponseEntity.ok(commandsManager.processCommand(visitor, String.format("#%d/%d %s", mid, rid, body),
+ attachmentFName));
+ } catch (Exception e) {
+ logger.error("DOWNLOAD ERROR", e);
+ return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(CommandResult.fromString(e.getMessage()));
}
- if (StringUtils.isBlank(body) && StringUtils.isBlank(attachmentFName.toString())) {
- // Should be there for compatibility
- throw new HttpBadRequestException();
- }
- return commandsManager.processCommand(visitor, String.format("#%d/%d %s", mid, rid, body),
- attachmentFName);
}
@PostMapping("/api/like")