From 00359147d18c8cb1c5f2ee964c9b6cfaa5fa3685 Mon Sep 17 00:00:00 2001 From: peageon Date: Tue, 28 May 2024 02:28:18 +0900 Subject: [PATCH] feat: 5MB limit to image and make uploading image optional --- .../backend/GlobalExceptionHandler.java | 10 +++++---- .../exception/InvalidFileTypeException.java | 11 ++++++++++ .../posts/service/GcsFileStorageService.java | 22 +++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/runningmate/backend/exception/InvalidFileTypeException.java diff --git a/src/main/java/com/runningmate/backend/GlobalExceptionHandler.java b/src/main/java/com/runningmate/backend/GlobalExceptionHandler.java index 1d3b721..6c73e5d 100644 --- a/src/main/java/com/runningmate/backend/GlobalExceptionHandler.java +++ b/src/main/java/com/runningmate/backend/GlobalExceptionHandler.java @@ -1,9 +1,6 @@ package com.runningmate.backend; -import com.runningmate.backend.exception.ErrorMessageResponseDTO; -import com.runningmate.backend.exception.ExistsConflictException; -import com.runningmate.backend.exception.FieldExistsException; -import com.runningmate.backend.exception.ResourceNotFoundException; +import com.runningmate.backend.exception.*; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.validation.FieldError; @@ -33,6 +30,11 @@ public ResponseEntity handleResourceNotFoundException(R return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ErrorMessageResponseDTO(e.getMessage())); } + @ExceptionHandler(InvalidFileTypeException.class) + public ResponseEntity handleInvalidFileTypeException(InvalidFileTypeException e) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ErrorMessageResponseDTO(e.getMessage())); + } + @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity> handleValidationExceptions(MethodArgumentNotValidException e) { Map errors = new HashMap<>(); diff --git a/src/main/java/com/runningmate/backend/exception/InvalidFileTypeException.java b/src/main/java/com/runningmate/backend/exception/InvalidFileTypeException.java new file mode 100644 index 0000000..d1b3c23 --- /dev/null +++ b/src/main/java/com/runningmate/backend/exception/InvalidFileTypeException.java @@ -0,0 +1,11 @@ +package com.runningmate.backend.exception; + +public class InvalidFileTypeException extends RuntimeException{ + public InvalidFileTypeException(String fileType) { + super("Invalid File Type Given: " + fileType); + } + + public InvalidFileTypeException(String fileType, String extension) { + super("File type of " + fileType + " was given but file's extension is " + extension); + } +} diff --git a/src/main/java/com/runningmate/backend/posts/service/GcsFileStorageService.java b/src/main/java/com/runningmate/backend/posts/service/GcsFileStorageService.java index e3edf4f..c8afc56 100644 --- a/src/main/java/com/runningmate/backend/posts/service/GcsFileStorageService.java +++ b/src/main/java/com/runningmate/backend/posts/service/GcsFileStorageService.java @@ -4,6 +4,7 @@ import com.google.auth.oauth2.GoogleCredentials; import com.google.cloud.WriteChannel; import com.google.cloud.storage.*; +import com.runningmate.backend.exception.InvalidFileTypeException; import org.springframework.stereotype.Service; import org.springframework.beans.factory.annotation.Value; import org.springframework.util.ResourceUtils; @@ -13,10 +14,13 @@ import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.List; import java.util.UUID; @Service public class GcsFileStorageService { + private static final List ACCEPTED_CONTENT_TYPES = Arrays.asList("image/jpeg", "image/png", "image/gif"); @Value("${spring.cloud.gcp.storage.credentials.location}") private String keyFileName; @@ -25,6 +29,10 @@ public class GcsFileStorageService { private String bucketName; public String storeFile(MultipartFile file) throws RuntimeException, IOException { + if (!validateFile(file)) { + return null; + } + InputStream keyFile = ResourceUtils.getURL(keyFileName).openStream(); String fileName = UUID.randomUUID().toString(); @@ -44,4 +52,18 @@ public String storeFile(MultipartFile file) throws RuntimeException, IOException throw new RuntimeException("Failed to store file in GCS", e); // throws 500 } } + + private boolean validateFile(MultipartFile file) { + if (file == null || file.isEmpty()) { + return false; + } + if (!ACCEPTED_CONTENT_TYPES.contains(file.getContentType())) { + throw new InvalidFileTypeException(file.getContentType()); + } + String filename = file.getOriginalFilename(); + if (filename != null && !filename.toLowerCase().matches(".*\\.(jpg|jpeg|png|gif)$")) { + throw new InvalidFileTypeException(file.getContentType(), filename.toLowerCase()); + } + return true; + } } \ No newline at end of file