-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
285 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
matzip-app-external-api/src/main/java/com/woowacourse/matzip/application/ImageUploader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.woowacourse.matzip.application; | ||
|
||
import com.woowacourse.matzip.domain.image.UnusedImage; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.util.List; | ||
|
||
public interface ImageUploader { | ||
|
||
String uploadImage(final MultipartFile file); | ||
|
||
void deleteImage(final String imageUrl); | ||
|
||
void deleteImages(final List<UnusedImage> unusedImages); | ||
} |
109 changes: 109 additions & 0 deletions
109
...ip-app-external-api/src/main/java/com/woowacourse/matzip/application/S3ImageUploader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package com.woowacourse.matzip.application; | ||
|
||
import com.woowacourse.matzip.domain.image.UnusedImage; | ||
import com.woowacourse.matzip.exception.UploadFailedException; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.multipart.MultipartFile; | ||
import software.amazon.awssdk.core.sync.RequestBody; | ||
import software.amazon.awssdk.services.s3.S3Client; | ||
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest; | ||
import software.amazon.awssdk.services.s3.model.DeleteObjectsRequest; | ||
import software.amazon.awssdk.services.s3.model.ObjectIdentifier; | ||
import software.amazon.awssdk.services.s3.model.PutObjectRequest; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
import java.util.stream.Collectors; | ||
|
||
import static com.woowacourse.matzip.environment.ProfileUtil.LOCAL; | ||
import static com.woowacourse.matzip.environment.ProfileUtil.PROD; | ||
|
||
@Profile({LOCAL, PROD}) | ||
@Component | ||
public class S3ImageUploader implements ImageUploader { | ||
|
||
private static final String EXTENSION_DELIMITER = "."; | ||
private static final String URL_DELIMITER = "/"; | ||
|
||
private final S3Client s3Client; | ||
private final String bucketName; | ||
private final String cloudFrontUrl; | ||
|
||
public S3ImageUploader(final S3Client s3Client, | ||
@Value("${cloud.aws.s3.bucket}") final String bucketName, | ||
@Value("${cloud.aws.s3.cloud-front-url}") final String cloudFrontUrl) { | ||
this.s3Client = s3Client; | ||
this.bucketName = bucketName; | ||
this.cloudFrontUrl = cloudFrontUrl; | ||
} | ||
|
||
@Override | ||
public String uploadImage(final MultipartFile file) { | ||
String key = createKey(file); | ||
PutObjectRequest request = createPutRequest(file, key); | ||
try { | ||
s3Client.putObject(request, RequestBody.fromBytes(file.getBytes())); | ||
} catch (IOException e) { | ||
throw new UploadFailedException(); | ||
} | ||
|
||
return cloudFrontUrl + key; | ||
} | ||
|
||
private PutObjectRequest createPutRequest(final MultipartFile file, final String key) { | ||
return PutObjectRequest.builder() | ||
.bucket(bucketName) | ||
.key(key) | ||
.contentLength(file.getSize()) | ||
.build(); | ||
} | ||
|
||
private String createKey(final MultipartFile file) { | ||
String fileName = file.getOriginalFilename(); | ||
Objects.requireNonNull(fileName); | ||
String extension = fileName.substring(fileName.lastIndexOf(EXTENSION_DELIMITER + 1)); | ||
String uuid = UUID.randomUUID().toString(); | ||
return uuid + EXTENSION_DELIMITER + extension; | ||
} | ||
|
||
@Override | ||
public void deleteImage(final String imageUrl) { | ||
DeleteObjectRequest request = createDeleteRequest(imageUrl); | ||
s3Client.deleteObject(request); | ||
} | ||
|
||
private DeleteObjectRequest createDeleteRequest(final String imageUrl) { | ||
String key = imageUrl.substring(imageUrl.lastIndexOf(URL_DELIMITER) + 1); | ||
return DeleteObjectRequest.builder() | ||
.bucket(bucketName) | ||
.key(key) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public void deleteImages(final List<UnusedImage> unusedImages) { | ||
|
||
List<ObjectIdentifier> identifiers = toIdentifiers(unusedImages); | ||
s3Client.deleteObjects(createDeleteObjectsRequest(identifiers)); | ||
} | ||
|
||
private List<ObjectIdentifier> toIdentifiers(final List<UnusedImage> unusedImages) { | ||
return unusedImages.stream() | ||
.map(UnusedImage::getImageUrl) | ||
.map(key -> ObjectIdentifier.builder() | ||
.key(key) | ||
.build()) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private DeleteObjectsRequest createDeleteObjectsRequest(final List<ObjectIdentifier> identifiers) { | ||
return DeleteObjectsRequest.builder() | ||
.bucket(bucketName) | ||
.delete(it -> it.objects(identifiers).build()) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...-app-external-api/src/test/java/com/woowacourse/matzip/application/TestImageUploader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.woowacourse.matzip.application; | ||
|
||
import com.woowacourse.matzip.domain.image.UnusedImage; | ||
import org.springframework.context.annotation.Profile; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.util.List; | ||
|
||
import static com.woowacourse.matzip.environment.ProfileUtil.TEST; | ||
|
||
@Component | ||
@Profile(TEST) | ||
public class TestImageUploader implements ImageUploader{ | ||
|
||
@Override | ||
public String uploadImage(MultipartFile file) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void deleteImage(String imageUrl) { | ||
|
||
} | ||
|
||
@Override | ||
public void deleteImages(List<UnusedImage> unusedImages) { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.