Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LIME-155] 이미지 업로드 할 경우 타입 검증 기능 추가 #82

Merged
merged 2 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,27 @@ public void createReview(
}

private List<String> uploadReviewImages(final List<MultipartFile> multipartReviewImages) {
if(multipartReviewImages == null || multipartReviewImages.isEmpty()) {
if (multipartReviewImages == null || multipartReviewImages.isEmpty()) {
return Collections.emptyList();
}

if (multipartReviewImages.size() > 5) {
throw new BusinessException(ErrorCode.REVIEW_IMAGE_COUNT_EXCEEDED);
}

return multipartReviewImages.stream()
.filter(multipartFile -> {
if(multipartFile.getOriginalFilename() == null) {
if (multipartFile.getOriginalFilename() == null) {
return false;
}
return !multipartFile.getOriginalFilename().isEmpty();
})
.map(multipartFile -> {
try {
String fileType = StringUtils.getFilenameExtension(multipartFile.getOriginalFilename());
String imageName = UUID.randomUUID() + fileType;
s3Manager.uploadFile(multipartFile, DIRECTORY, imageName);
String imageName = UUID.randomUUID() + "." + fileType;

s3Manager.uploadFile(multipartFile, DIRECTORY, imageName);
return s3Manager.getUrl(DIRECTORY, imageName).toString();
} catch (IOException e) {
throw new BusinessException(ErrorCode.S3_UPLOAD_FAIL);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public enum ErrorCode {
MESSAGE_DOMAIN_TYPE_NOT_FOUND("COMMON_018", "메시지 도메인 타입을 찾을 수 없습니다."),
SUBSCRIPTION_DESTINATION_NOT_FOUND("COMMON_019", "구독 대상을 찾을 수 없습니다."),
INVALID_ACCESSOR("COMMON_020", "잘못된 Accessor 입니다."),

UNSUPPORTED_IMAGE_TYPE("COMMON_021", "업로드 할 수 있는 이미지 확장자가 아닙니다."),
NOT_CONVERTIBLE_IMAGE("COMMON_022", "이미지 변환에 실패했습니다."),

// Member
MEMBER_LOGIN_FAIL("MEMBER_001", "로그인 정보가 잘못 되었습니다."),
Expand Down Expand Up @@ -68,7 +69,7 @@ public enum ErrorCode {
NOT_REVIEW_LIKED("REVIEW_006", "좋아요를 누르지 않은 리뷰입니다."),
REVIEW_ALREADY_EXIST("REVIEW_007", "이미 리뷰를 작성한 아이템입니다."),
REVIEW_IMAGE_NOT_EXIST("REVIEW_008", "리뷰 이미지가 존재하지 않습니다."),

REVIEW_IMAGE_COUNT_EXCEEDED("REVIEW_009", "리뷰 이미지는 최대 5개까지만 등록할 수 있습니다."),

// Vote
VOTE_NOT_FOUND("VOTE_001", "투표를 찾을 수 없습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.amazonaws.services.s3.AmazonS3;
Expand All @@ -26,16 +27,45 @@ public class S3Manager {

private final AmazonS3 amazonS3;

private static final String[] allowedExtensions = {
"jpeg", "jpg", "png", "gif",
"bmp", "svg", "tiff", "tif",
"webp"
};

@Value("${cloud.aws.s3.bucket}")
private String bucket;

private static void validateType(final String fileName) {
String type = StringUtils.getFilenameExtension(fileName);

if (type == null || type.isEmpty()) {
throw new IllegalArgumentException("업로드 할 수 있는 이미지 확장자가 아닙니다.");
}

boolean isAllowed = false;
type = type.toLowerCase();
for (String extension : allowedExtensions) {
if (type.equals(extension)) {
isAllowed = true;
break;
}
}

if (!isAllowed) {
throw new IllegalArgumentException("업로드 할 수 있는 이미지 확장자가 아닙니다.");
}
}

public void uploadFile(
final MultipartFile multipartFile,
final String directory,
final String fileName
) throws IOException {
validateType(fileName);

final File uploadFile = convert(multipartFile)
.orElseThrow(() -> new IllegalArgumentException("MultipartFile -> File로 전환이 실패했습니다."));
.orElseThrow(() -> new IllegalArgumentException("이미지 변환에 실패했습니다."));

upload(uploadFile, directory, fileName);
}
Expand Down
Loading