Skip to content

Commit

Permalink
#33 [Add] 이미지 예외처리 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
JSoi committed Aug 4, 2022
1 parent a202d03 commit ecd8717
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/mpnp/baechelin/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,4 +57,9 @@ protected ResponseEntity<ErrorResponse> handleUnsupportedJwtException(RestClient
log.error("API 로드에 실패했습니다");
return ErrorResponse.toResponseEntity(ErrorCode.API_LOAD_FAILURE);
}
@ExceptionHandler(MaxUploadSizeExceededException.class)
public ResponseEntity<ErrorResponse> handleMaxUploadSizeExceededException(Exception ex){
log.warn("파일 용량 초과 문제: {}",ex.getMessage());
return ErrorResponse.toResponseEntity(ErrorCode.IMAGE_SIZE_EXCESS);
}
}
20 changes: 13 additions & 7 deletions src/main/java/com/mpnp/baechelin/util/AwsS3Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}")
Expand All @@ -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());

Expand Down Expand Up @@ -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;
}
}

0 comments on commit ecd8717

Please sign in to comment.