Skip to content

Commit

Permalink
[merge] 확장자에 따라 저장로직 변경 - #132
Browse files Browse the repository at this point in the history
[feat] 확장자에 따라 저장로직 변경 - #132
  • Loading branch information
rlarlgnszx authored Jul 16, 2024
2 parents 1f865d8 + b59eabb commit 4f4051c
Showing 1 changed file with 19 additions and 4 deletions.
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

0 comments on commit 4f4051c

Please sign in to comment.