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

[FEAT] 확장자에 따라 저장로직 변경 - #132 #133

Merged
merged 1 commit into from
Jul 16, 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
23 changes: 19 additions & 4 deletions dateroad-external/src/main/java/org/dateroad/s3/S3Service.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.dateroad.s3;

import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Future;
import org.dateroad.code.FailureCode;
Expand All @@ -23,7 +24,7 @@
public class S3Service {
private final String bucketName;
private final AWSConfig awsConfig;
private static final List<String> IMAGE_EXTENSIONS = Arrays.asList("image/jpeg", "image/png", "image/jpg", "image/webp","image/heic","image/heif");
private static final List<String> IMAGE_EXTENSIONS = Arrays.asList("image/jpeg", "image/png", "image/jpg", "image/webp", "image/heic", "image/heif");
private static final Long MAX_FILE_SIZE = 5 * 1024 * 1024L;

public S3Service(@Value("${aws-property.s3-bucket-name}") final String bucketName, AWSConfig awsConfig) {
Expand All @@ -33,7 +34,7 @@ public S3Service(@Value("${aws-property.s3-bucket-name}") final String bucketNam

@Async
public Future<String> uploadImage(String directoryPath, MultipartFile image) throws IOException {
final String key = directoryPath + generateImageFileName();
final String key = directoryPath + generateImageFileName(image);
final S3Client s3Client = awsConfig.getS3Client();

validateExtension(image);
Expand Down Expand Up @@ -62,8 +63,22 @@ public void deleteImage(String imageUrl) throws IOException {
);
}

private String generateImageFileName() {
return UUID.randomUUID() + ".jpg";
private String generateImageFileName(MultipartFile image) {
String extension = getExtension(Objects.requireNonNull(image.getContentType()));
if (extension == null) {
throw new InvalidValueException(FailureCode.INVALID_IMAGE_TYPE);
}
return UUID.randomUUID() + extension;
}

private String getExtension(String contentType) {
return switch (contentType) {
case "image/png" -> ".png";
case "image/webp" -> ".webp";
case "image/heic" -> ".heic";
case "image/heif" -> ".heif";
default -> ".jpg";
};
}

private void validateExtension(MultipartFile image) {
Expand Down
Loading