-
Notifications
You must be signed in to change notification settings - Fork 0
[Refactor] 이미지 도메인 객체 도입 #101
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1a9799e
feat : 이미지 도메인 객체 구현
leegwichan ec6995a
feat : 이미지 업로드 담당 Client 구현
leegwichan 3bf5be6
refactor : ExternalImageStorage 리팩토링 실시
leegwichan 13aaf22
refactor : `Cheer`, `Story` 에서 `ImageKey` 를 사용하도록 변경
leegwichan d246067
test: DocumentTest 에서 이미지 형식 지정
leegwichan 00e4f3d
refactor : Article 에 ImageKey 객체 도입
leegwichan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,65 @@ | ||
| package eatda.client.file; | ||
|
|
||
| import eatda.exception.BusinessErrorCode; | ||
| import eatda.exception.BusinessException; | ||
| import java.time.Duration; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| 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.GetObjectRequest; | ||
| import software.amazon.awssdk.services.s3.model.PutObjectRequest; | ||
| import software.amazon.awssdk.services.s3.presigner.S3Presigner; | ||
| import software.amazon.awssdk.services.s3.presigner.model.GetObjectPresignRequest; | ||
|
|
||
| @Component | ||
| public class FileClient { | ||
|
|
||
| private final S3Client s3Client; | ||
| private final String bucket; | ||
| private final S3Presigner s3Presigner; | ||
|
|
||
| public FileClient(S3Client s3Client, | ||
| @Value("${spring.cloud.aws.s3.bucket}") String bucket, | ||
| S3Presigner s3Presigner) { | ||
| this.s3Client = s3Client; | ||
| this.bucket = bucket; | ||
| this.s3Presigner = s3Presigner; | ||
| } | ||
|
|
||
| public String upload(MultipartFile file, String fileKey) { | ||
| PutObjectRequest request = PutObjectRequest.builder() | ||
| .bucket(bucket) | ||
| .key(fileKey) | ||
| .contentType(file.getContentType()) | ||
| .build(); | ||
|
|
||
| try { | ||
| s3Client.putObject(request, RequestBody.fromInputStream(file.getInputStream(), file.getSize())); | ||
| return fileKey; | ||
| } catch (Exception exception) { | ||
| throw new BusinessException(BusinessErrorCode.FILE_UPLOAD_FAILED); | ||
| } | ||
| // TODO 발생 예외 별로 처리하기 | ||
| // InvalidRequestException, InvalidWriteOffsetException, TooManyPartsException, EncryptionTypeMismatchException, | ||
| // AwsServiceException, SdkClientException, S3Exception | ||
| } | ||
|
|
||
| public String getPreSignedUrl(String fileKey, Duration signatureDuration) { | ||
| GetObjectRequest getObjectRequest = GetObjectRequest.builder() | ||
| .bucket(bucket) | ||
| .key(fileKey) | ||
| .build(); | ||
| GetObjectPresignRequest presignRequest = GetObjectPresignRequest.builder() | ||
| .getObjectRequest(getObjectRequest) | ||
| .signatureDuration(signatureDuration) | ||
| .build(); | ||
|
|
||
| try { | ||
| return s3Presigner.presignGetObject(presignRequest).url().toString(); | ||
| } catch (Exception exception) { | ||
| throw new BusinessException(BusinessErrorCode.PRESIGNED_URL_GENERATION_FAILED); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,50 @@ | ||
| package eatda.domain; | ||
|
|
||
| import eatda.exception.BusinessErrorCode; | ||
| import eatda.exception.BusinessException; | ||
| import java.util.Set; | ||
| import lombok.Getter; | ||
| import org.springframework.lang.Nullable; | ||
| import org.springframework.web.multipart.MultipartFile; | ||
|
|
||
| @Getter | ||
| public class Image { | ||
|
|
||
| private static final Set<String> ALLOWED_CONTENT_TYPES = Set.of("image/jpg", "image/jpeg", "image/png"); | ||
| private static final String EXTENSION_DELIMITER = "."; | ||
| private static final String DEFAULT_CONTENT_TYPE = "bin"; | ||
|
|
||
| private final ImageDomain domain; | ||
| private final MultipartFile file; | ||
|
|
||
| public Image(ImageDomain domain, @Nullable MultipartFile file) { | ||
| validateContentType(file); | ||
| this.domain = domain; | ||
| this.file = file; | ||
| } | ||
|
|
||
| private void validateContentType(MultipartFile file) { | ||
| if (file != null && !ALLOWED_CONTENT_TYPES.contains(file.getContentType())) { | ||
| throw new BusinessException(BusinessErrorCode.INVALID_IMAGE_TYPE); | ||
| } | ||
| } | ||
|
|
||
| public String getExtension() { | ||
| String filename = file.getOriginalFilename(); | ||
| if (filename == null | ||
| || filename.lastIndexOf(EXTENSION_DELIMITER) == -1 | ||
| || filename.startsWith(EXTENSION_DELIMITER) | ||
| || filename.endsWith(EXTENSION_DELIMITER)) { | ||
| return DEFAULT_CONTENT_TYPE; | ||
| } | ||
| return filename.substring(filename.lastIndexOf(EXTENSION_DELIMITER) + 1); | ||
| } | ||
|
|
||
| public String getDomainName() { | ||
| return domain.getName(); | ||
| } | ||
|
|
||
| public boolean isEmpty() { | ||
| return file == null || file.isEmpty(); | ||
| } | ||
| } |
This file contains hidden or 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,42 @@ | ||
| package eatda.domain; | ||
|
|
||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Embeddable; | ||
| import java.util.Objects; | ||
| import lombok.AccessLevel; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
|
|
||
| @Getter | ||
| @Embeddable | ||
| @NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
| public class ImageKey { | ||
|
|
||
| @Column(name = "image_key", length = 511) | ||
| private String value; | ||
|
|
||
| public ImageKey(String value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public boolean isEmpty() { | ||
| return value == null || value.isBlank(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object object) { | ||
| if (this == object) { | ||
| return true; | ||
| } | ||
| if (object == null || getClass() != object.getClass()) { | ||
| return false; | ||
| } | ||
| ImageKey imageKey = (ImageKey) object; | ||
| return Objects.equals(value, imageKey.value); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hashCode(value); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.