diff --git a/src/main/java/cc/fascinated/backend/controller/PasteController.java b/src/main/java/cc/fascinated/backend/controller/PasteController.java index 8119247..1fedb9e 100644 --- a/src/main/java/cc/fascinated/backend/controller/PasteController.java +++ b/src/main/java/cc/fascinated/backend/controller/PasteController.java @@ -3,6 +3,7 @@ package cc.fascinated.backend.controller; import cc.fascinated.backend.model.Paste; import cc.fascinated.backend.service.PasteService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -20,8 +21,8 @@ public class PasteController { } @PostMapping(value = "/upload") - public ResponseEntity uploadPaste(@RequestBody String content) { - String id = pasteService.createPaste(content); + public ResponseEntity uploadPaste(@RequestBody String content, @RequestHeader(HttpHeaders.CONTENT_TYPE) String contentType) { + String id = pasteService.createPaste(content, contentType); return ResponseEntity.ok(Map.of("id", id)); } diff --git a/src/main/java/cc/fascinated/backend/service/PasteService.java b/src/main/java/cc/fascinated/backend/service/PasteService.java index 3574f4e..e67b904 100644 --- a/src/main/java/cc/fascinated/backend/service/PasteService.java +++ b/src/main/java/cc/fascinated/backend/service/PasteService.java @@ -44,7 +44,7 @@ public class PasteService { * @param content The content of the paste. * @return The id of the paste. */ - public String createPaste(String content) { + public String createPaste(String content, String contentType) { int length = content.length(); long before = System.currentTimeMillis(); log.info("Creating a new paste. (characters: {})", length); @@ -52,12 +52,19 @@ public class PasteService { // Check if the content is too large. if (length > uploadSizeLimit && uploadSizeLimit != -1) { log.info("Paste didn't meet the size requirements. (characters: {})", length); - throw new BadRequestException("The paste content is too large, the limit is " + uploadSizeLimit + " characters"); + throw new BadRequestException("The paste content is too large, the limit is " + uploadSizeLimit + " characters, not uploading..."); + } + + // Ensure the paste content type is valid. + if (contentType.contains("image") || contentType.contains("video") || contentType.contains("audio")) { + log.info("Paste content type is not supported. (content type: {})", contentType); + throw new BadRequestException("The paste content type is not supported, not uploading..."); } // Ensure the paste content does not contain a file header. if (FileHeaderChecker.containsFileHeader(content)) { - throw new BadRequestException("The paste content contains a file header"); + log.info("Paste content contains a file header, not uploading..."); + throw new BadRequestException("The paste content contains a file header, not uploading..."); } // Save the paste to the database.