From ecd871706e0cf9fae8728fca493a2c7857306546 Mon Sep 17 00:00:00 2001 From: JSoi Date: Fri, 5 Aug 2022 08:11:30 +0900 Subject: [PATCH] =?UTF-8?q?#33=20[Add]=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20?= =?UTF-8?q?=EC=98=88=EC=99=B8=EC=B2=98=EB=A6=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mpnp/baechelin/exception/ErrorCode.java | 3 ++- .../exception/GlobalExceptionHandler.java | 6 ++++++ .../com/mpnp/baechelin/util/AwsS3Manager.java | 20 ++++++++++++------- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/mpnp/baechelin/exception/ErrorCode.java b/src/main/java/com/mpnp/baechelin/exception/ErrorCode.java index a581a9d..968a6c6 100644 --- a/src/main/java/com/mpnp/baechelin/exception/ErrorCode.java +++ b/src/main/java/com/mpnp/baechelin/exception/ErrorCode.java @@ -30,7 +30,8 @@ public enum ErrorCode { NO_BOOKMARK_FOUND(500, "E-NBF", "북마크가 존재하지 않습니다."), NO_USER_FOUND(500, "E-NUF", "유저가 존재하지 않습니다."), NO_REVIEW_FOUND(500, "E-NRF", "리뷰가 존재하지 않습니다."), - + BAD_IMAGE_INPUT(400, "E-BII", "올바르지 않은 이미지 형식입니다"), + IMAGE_SIZE_EXCESS(400, "E-ISE", "이미지 크기(3MB)를 초과했습니다.") ; private final int status; diff --git a/src/main/java/com/mpnp/baechelin/exception/GlobalExceptionHandler.java b/src/main/java/com/mpnp/baechelin/exception/GlobalExceptionHandler.java index 8db9459..3783e0c 100644 --- a/src/main/java/com/mpnp/baechelin/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/mpnp/baechelin/exception/GlobalExceptionHandler.java @@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.client.RestClientException; +import org.springframework.web.multipart.MaxUploadSizeExceededException; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; @Slf4j @@ -56,4 +57,9 @@ protected ResponseEntity handleUnsupportedJwtException(RestClient log.error("API 로드에 실패했습니다"); return ErrorResponse.toResponseEntity(ErrorCode.API_LOAD_FAILURE); } + @ExceptionHandler(MaxUploadSizeExceededException.class) + public ResponseEntity handleMaxUploadSizeExceededException(Exception ex){ + log.warn("파일 용량 초과 문제: {}",ex.getMessage()); + return ErrorResponse.toResponseEntity(ErrorCode.IMAGE_SIZE_EXCESS); + } } diff --git a/src/main/java/com/mpnp/baechelin/util/AwsS3Manager.java b/src/main/java/com/mpnp/baechelin/util/AwsS3Manager.java index 34c1d7e..0f8c615 100644 --- a/src/main/java/com/mpnp/baechelin/util/AwsS3Manager.java +++ b/src/main/java/com/mpnp/baechelin/util/AwsS3Manager.java @@ -6,7 +6,10 @@ import com.amazonaws.services.s3.model.DeleteObjectRequest; import com.amazonaws.services.s3.model.ObjectMetadata; import com.amazonaws.services.s3.model.PutObjectRequest; +import com.mpnp.baechelin.exception.CustomException; +import com.mpnp.baechelin.exception.ErrorCode; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; @@ -15,13 +18,11 @@ import java.io.IOException; import java.io.InputStream; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.UUID; +import java.util.*; @Service @RequiredArgsConstructor +@Slf4j public class AwsS3Manager { @Value("${cloud.aws.s3.bucket}") @@ -32,10 +33,9 @@ public class AwsS3Manager { // 이미지 단건 저장 public String uploadFile(MultipartFile file) { if (Objects.equals(file.getOriginalFilename(), "")) { - return ""; + throw new CustomException(ErrorCode.BAD_IMAGE_INPUT); } - // 파일은 단건만 추가 가능 String fileName = createFileName(file.getOriginalFilename()); @@ -63,10 +63,16 @@ private String createFileName(String fileName) { // 먼저 파일 업로드 시, } private String getFileExtension(String fileName) { // file 형식이 잘못된 경우를 확인하기 위해 만들어진 로직이며, 파일 타입과 상관없이 업로드할 수 있게 하기 위해 .의 존재 유무만 판단하였습니다. + String fileEx; try { - return fileName.substring(fileName.lastIndexOf(".")); + fileEx = fileName.substring(fileName.lastIndexOf(".")); } catch (StringIndexOutOfBoundsException e) { throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "잘못된 형식의 파일(" + fileName + ") 입니다."); } + log.info("{} : ", fileEx.toLowerCase()); + if (!Arrays.asList(".jpg", ".png", ".jpeg", ".gif", ".bmp").contains(fileEx.toLowerCase())) { + throw new CustomException(ErrorCode.BAD_IMAGE_INPUT); + } + return fileEx; } }