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

[BE] fix: .jpg 이외의 이미지도 업로드할 수 있게 수정 #605

Merged
merged 5 commits into from
Oct 10, 2024
Merged
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 @@ -2,6 +2,7 @@

import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.INTERNAL_SERVER_ERROR;
import static org.springframework.util.StringUtils.getFilenameExtension;

import com.happy.friendogly.common.ErrorCode;
import com.happy.friendogly.exception.FriendoglyException;
Expand All @@ -27,6 +28,7 @@
@Profile("!local")
public class S3StorageManager implements FileStorageManager {

private static final String IMAGE_MIME_TYPE_PREFIX = "image/";
private static final int FILE_SIZE_LIMIT = 1;
private static final int MB = 1_048_576;

Expand All @@ -51,37 +53,69 @@ public S3StorageManager() {

@Override
public String uploadFile(MultipartFile file) {
if (file == null || file.isEmpty()) {
throw new FriendoglyException("이미지 파일은 비어 있을 수 없습니다.");
}
validateNullOrEmpty(file);
validateFileSize(file);

if (file.getSize() > FILE_SIZE_LIMIT * MB) {
throw new FriendoglyException(
String.format("%dMB 미만의 사진만 업로드 가능합니다.", FILE_SIZE_LIMIT),
ErrorCode.FILE_SIZE_EXCEED,
BAD_REQUEST
);
}
String fileName = file.getOriginalFilename();
String contentType = file.getContentType();
validateImageType(contentType);

// TODO: 실제 파일명에서 확장자 가져오기
// TODO: jpg 이외의 이미지 파일도 가져올 수 있도록 수정하기
// String fileName = file.getOriginalFilename();
String newFilename = UUID.randomUUID() + ".jpg";
String newFilename = generateRandomFileName(fileName);

PutObjectRequest putObjectRequest = PutObjectRequest.builder()
.bucket(BUCKET_NAME)
.key(KEY_PREFIX + newFilename)
.contentType("image/jpg")
.contentType(contentType)
Comment on lines -74 to +68
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://www.iana.org/assignments/media-types/media-types.xhtml#image

순수한 궁금증.. image/jpg 라는 MIME 타입은 없는데 그동안 어떻게 동작하고 있던걸까요..?
자동으로 image/jpeg로 변환되는걸까요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

그러게요 ㄷ ㄷ ㄷ

.build();

RequestBody requestBody = RequestBody.fromFile(convertMultiPartFileToFile(file));

try {
s3Client.putObject(putObjectRequest, requestBody);
} catch (SdkException e) {
throw new FriendoglyException("s3전송 과정중에 에러 발생", INTERNAL_SERVER_ERROR);
}

return S3_ENDPOINT + newFilename;
}

private void validateNullOrEmpty(MultipartFile file) {
if (file == null || file.isEmpty()) {
throw new FriendoglyException("이미지 파일은 비어 있을 수 없습니다.");
}
}

private void validateFileSize(MultipartFile file) {
if (file.getSize() > FILE_SIZE_LIMIT * MB) {
throw new FriendoglyException(
String.format("%dMB 미만의 사진만 업로드 가능합니다.", FILE_SIZE_LIMIT),
ErrorCode.FILE_SIZE_EXCEED,
BAD_REQUEST
);
}
}

private void validateImageType(String contentType) {
if (StringUtils.isBlank(contentType)) {
throw new FriendoglyException("업로드한 파일의 Content-Type을 알 수 없습니다.");
}

if (!contentType.startsWith(IMAGE_MIME_TYPE_PREFIX)) {
throw new FriendoglyException(
String.format("이미지 파일만 업로드할 수 있습니다. %s는 이미지 Content-Type이 아닙니다.", contentType));
}
}

private String generateRandomFileName(String fileName) {
String fileExtension = getFilenameExtension(fileName);

if (fileExtension == null) {
return UUID.randomUUID().toString();
}

return UUID.randomUUID() + "." + fileExtension;
}

private File convertMultiPartFileToFile(MultipartFile multipartFile) {
try {
File file = File.createTempFile("tmp", null);
Expand Down
Loading