Skip to content

Commit

Permalink
[#495] feat: cloudFront url로 가져오도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
jyajoo committed Nov 13, 2024
1 parent b330cf8 commit 87e6aaf
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void updateProfile(MemberInfoDto memberInfoDto, MemberRequest.ProfileUpda
Member member = memberInfoDto.getMember();
validateNickname(profileUpdateDto.getNickname(), member);
if (fileKey != null) {
S3ImageDto s3ImageDto = fileUploadService.getS3ImageUrls(fileKey);
S3ImageDto s3ImageDto = fileUploadService.getCloudImageUrls(fileKey);
member.getProfileImageFile().updateImageFile(s3ImageDto.getOriginUrl(), s3ImageDto.getThumbnailUrl());
}
member.updateProfile(profileUpdateDto);
Expand Down Expand Up @@ -176,6 +176,6 @@ public ImageFile saveRandomProfileImageFile() {

public S3ImageDto getRandomImageFile() {
String selectedProfile = defaultProfile.get(random.nextInt(defaultProfile.size()));
return fileUploadService.getS3ImageUrls(selectedProfile);
return fileUploadService.getCloudImageUrls(selectedProfile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class FileUploadService {

private final AmazonS3 amazonS3;
private final AmazonS3Client amazonS3Client;
@Value("${cloud.aws.cloudfront.domain}")
private String cloudFrontDomain;
@Value("${cloud.aws.s3.bucket}")
private String bucket;
@Value("${cloud.aws.s3.memberProfileDirectory}")
Expand Down Expand Up @@ -194,19 +196,20 @@ public void uploadComplete(@Valid FileRequest.CompleteCommandDto completeCommand
}
}

public S3ImageDto getS3ImageUrls(String fileKey) {
public S3ImageDto getCloudImageUrls(String fileKey) {
if (!amazonS3Client.doesObjectExist(bucket, fileKey)) {
throw new NotFoundException(FILE_S3_NOT_FOUNE.getMessage());
}
String originUrl = amazonS3.getUrl(bucket, fileKey).toString();
String originUrl = cloudFrontDomain + "/" + fileKey;

if (!amazonS3Client.doesObjectExist(bucket + THUMBNAIL_DIRECTORY, THUMBNAIL_PREFIX + fileKey)) {
return S3ImageDto.builder()
.originUrl(originUrl)
.thumbnailUrl(originUrl)
.build();
}
String thumbnailUrl = amazonS3.getUrl(bucket + THUMBNAIL_DIRECTORY, THUMBNAIL_PREFIX + fileKey).toString();

String thumbnailUrl = cloudFrontDomain + "/" + THUMBNAIL_DIRECTORY + "/" + THUMBNAIL_PREFIX + fileKey;
return S3ImageDto.builder()
.originUrl(originUrl)
.thumbnailUrl(thumbnailUrl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,15 @@ void updateProfileSuccess_multipartFileExists() {
.thumbnailUrl("thumbnailUrl")
.build();
doReturn(Optional.empty()).when(memberRepository).findByNickname(any(String.class));
doReturn(s3ImageDto).when(fileUploadService).getS3ImageUrls(any());
doReturn(s3ImageDto).when(fileUploadService).getCloudImageUrls(any());

// when: 유저 프로필 수정
memberProfileService.updateProfile(memberInfoDto, profileUpdateDto, "test/abc_test.jpg");

// then: 프로필 수정 확인, 이미지 변경 확인
assertThat(member.getNickname()).isEqualTo(profileUpdateDto.getNickname());
assertThat(member.getDescription()).isEqualTo(profileUpdateDto.getDescription());
verify(fileUploadService).getS3ImageUrls(any());
verify(fileUploadService).getCloudImageUrls(any());
}
}

Expand Down

0 comments on commit 87e6aaf

Please sign in to comment.