Skip to content

Commit

Permalink
Merge pull request #33 from KONKUK-MAP-Service/feat-cloudfront
Browse files Browse the repository at this point in the history
💡 [Feat] : 사진 응답속도 개선 #33
  • Loading branch information
evergreenn authored May 1, 2024
2 parents 66c2a07 + 5e4cdb3 commit a188fc7
Show file tree
Hide file tree
Showing 13 changed files with 85 additions and 13 deletions.
1 change: 1 addition & 0 deletions .github/workflows/CD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ jobs:
cloud.aws.credentials.accessKey: ${{secrets.S3_ACCESSKEY}}
cloud.aws.credentials.secretKey: ${{secrets.S3_SECRETKEY}}
cloud.aws.s3.bucketName: ${{secrets.S3_BUCKETNAME}}
cloud.aws.cloudfront.domainName: ${{secrets.CLOUDFRONT_NAME}}

- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ jobs:
cloud.aws.credentials.accessKey: ${{secrets.S3_ACCESSKEY}}
cloud.aws.credentials.secretKey: ${{secrets.S3_SECRETKEY}}
cloud.aws.s3.bucketName: ${{secrets.S3_BUCKETNAME}}
cloud.aws.cloudfront.domainName: ${{secrets.CLOUDFRONT_NAME}}




Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ public record CommentListResponseDto(
int totalPages
) {

public static CommentListResponseDto of (Comment comment, PageInfo pageInfo){
public static CommentListResponseDto of(Comment comment, PageInfo pageInfo) {
String spotImageurl = comment.getSpot().getImageUrls().isEmpty() ? "" : comment.getSpot().getImageUrls().get(0);
return CommentListResponseDto.builder()
.spotName(comment.getSpot().getSpotName())
.spotImageurl(comment.getSpot().getImageUrls().get(0))
.spotImageurl(spotImageurl)
.commentId(comment.getId())
.CommentcreateDate(comment.getCreatedDate())
.spotId(comment.getId())
Expand All @@ -45,4 +46,5 @@ public static CommentListResponseDto of (Comment comment, PageInfo pageInfo){
}



}
44 changes: 44 additions & 0 deletions src/main/java/com/cona/KUsukKusuk/global/s3/ImageUrlConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.cona.KUsukKusuk.global.s3;

import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


@Component
public class ImageUrlConverter {

private static String s3BaseUrl;
private static String cloudFrontBaseUrl;

@Value("${cloud.aws.s3.bucketName}")
public void setS3BaseUrl(String bucket) {
s3BaseUrl = "https://" + bucket + ".s3.ap-northeast-2.amazonaws.com/";
}

@Value("${cloud.aws.cloudfront.domainName}")
public void setCloudFrontBaseUrl(String cloudfront) {
cloudFrontBaseUrl = "https://" + cloudfront + "/";
}

public static String convertToCloudFrontUrl(String imageUrl) {
return imageUrl.replace(s3BaseUrl, cloudFrontBaseUrl);
}

public static List<String> convertToCloudFrontUrls(List<String> imageUrls) {
return imageUrls.stream()
.map(ImageUrlConverter::convertToCloudFrontUrl)
.collect(Collectors.toList());
}

public static String convertToS3Url(String imageUrl) {
return imageUrl.replace(cloudFrontBaseUrl, s3BaseUrl);
}

public static List<String> convertToS3Urls(List<String> imageUrls) {
return imageUrls.stream()
.map(ImageUrlConverter::convertToS3Url)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import com.cona.KUsukKusuk.global.response.HttpResponse;
import com.cona.KUsukKusuk.global.s3.ImageUrlConverter;
import com.cona.KUsukKusuk.profile.dto.UploadImage;
import com.cona.KUsukKusuk.profile.exception.ImageUploadException;
import com.cona.KUsukKusuk.profile.service.ProfileService;
Expand Down Expand Up @@ -36,6 +37,8 @@ public HttpResponse<String> deleteProfileImage() {
public HttpResponse<String> updateProfileImage(UploadImage imageDto) {
try {
String imageUrl = profileService.updateProfileImage(imageDto);
// S3 URL을 CloudFront URL로 변환
String cloudFrontUrl = ImageUrlConverter.convertToCloudFrontUrl(imageUrl);
return HttpResponse.okBuild(imageUrl);
} catch (IOException e) {
throw new ImageUploadException();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cona.KUsukKusuk.spot.dto;

import com.cona.KUsukKusuk.global.s3.ImageUrlConverter;
import com.cona.KUsukKusuk.spot.domain.Spot;
import java.time.LocalDateTime;
import java.util.List;
Expand All @@ -21,11 +22,14 @@ public record SpotDetailResponse(Long spotId,
String review) {

public static SpotDetailResponse fromSpot(Spot spot,Boolean isBookmark, Boolean isLike) {

List<String> cloudFrontImageUrls = ImageUrlConverter.convertToCloudFrontUrls(spot.getImageUrls());

return SpotDetailResponse.builder()
.spotId(spot.getId())

.spotName(spot.getSpotName())
.images(spot.getImageUrls())
.images(cloudFrontImageUrls)
.longitude(spot.getLongitude())
.latitude(spot.getLatitude())
.review(spot.getReview())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cona.KUsukKusuk.spot.dto;

import com.cona.KUsukKusuk.bookmark.domain.Bookmark;
import com.cona.KUsukKusuk.global.s3.ImageUrlConverter;
import com.cona.KUsukKusuk.spot.domain.Spot;
import io.swagger.v3.oas.annotations.media.Schema;
import java.time.LocalDateTime;
Expand All @@ -24,11 +25,13 @@ public record SpotGetResponse(
LocalDateTime createDate
) {
public static SpotGetResponse of(Spot spot,Boolean isUsersOwnSpot,Boolean isBookmark,Boolean isLike) {

List<String> cloudFrontImageUrls = ImageUrlConverter.convertToCloudFrontUrls(spot.getImageUrls());
return SpotGetResponse.builder()
.spotId(spot.getId())
.isUsersOwnSpot(isUsersOwnSpot)
.spotName(spot.getSpotName())
.images(spot.getImageUrls())
.images(cloudFrontImageUrls)
.longtitude(spot.getLongitude())
.latitude(spot.getLatitude())
.review(spot.getReview())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cona.KUsukKusuk.spot.dto;

import com.cona.KUsukKusuk.global.s3.ImageUrlConverter;
import com.cona.KUsukKusuk.spot.domain.Spot;
import com.cona.KUsukKusuk.user.domain.User;
import com.cona.KUsukKusuk.user.dto.UserJoinResponse;
Expand All @@ -11,10 +12,12 @@ public record SpotJoinResponse(
Long spotid,String spotName, List<String> images, String longtitude, String latitude, String review
) {
public static SpotJoinResponse of(Spot spot){
List<String> cloudFrontImageUrls = ImageUrlConverter.convertToCloudFrontUrls(spot.getImageUrls());

return SpotJoinResponse.builder()
.spotid(spot.getId())
.spotName(spot.getSpotName())
.images(spot.getImageUrls())
.images(cloudFrontImageUrls)
.longtitude(spot.getLongitude())
.latitude(spot.getLatitude())
.review(spot.getReview())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cona.KUsukKusuk.spot.dto;

import com.cona.KUsukKusuk.global.s3.ImageUrlConverter;
import com.cona.KUsukKusuk.spot.domain.Spot;
import java.util.List;
import lombok.Builder;
Expand All @@ -10,10 +11,12 @@ public record SpotUpdatResponse(

) {
public static SpotUpdatResponse of(Spot spot){
List<String> cloudFrontImageUrls = ImageUrlConverter.convertToCloudFrontUrls(spot.getImageUrls());

return SpotUpdatResponse.builder()
.spotid(spot.getId())
.spotName(spot.getSpotName())
.images(spot.getImageUrls())
.images(cloudFrontImageUrls)
.longtitude(spot.getLongitude())
.latitude(spot.getLatitude())
.review(spot.getReview())
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/cona/KUsukKusuk/spot/service/SpotService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.cona.KUsukKusuk.bookmark.repository.BookmarkRepository;
import com.cona.KUsukKusuk.comment.domain.Comment;
import com.cona.KUsukKusuk.global.exception.HttpExceptionCode;
import com.cona.KUsukKusuk.global.s3.ImageUrlConverter;
import com.cona.KUsukKusuk.global.s3.S3Service;
import com.cona.KUsukKusuk.like.UserLike;
import com.cona.KUsukKusuk.like.repository.UserLikeRepository;
Expand Down Expand Up @@ -130,17 +131,17 @@ public Spot updateSpot(Long spotId,List<MultipartFile> images, SpotUpdateRequest
if (!spot.getUser().equals(user)) {
throw new UserNotFoundException(HttpExceptionCode.USER_NOT_MATCH);
}
String[] deleteImageUrls = spotUpdateRequest.deleteImageUrls();

String[] deleteImageUrls = spotUpdateRequest.deleteImageUrls();
List<String> imageUrls = spot.getImageUrls();

for (String deleteImageUrl : deleteImageUrls) {
if (imageUrls.contains(deleteImageUrl)) {
imageUrls.remove(deleteImageUrl);

String s3ImageUrl = ImageUrlConverter.convertToS3Url(deleteImageUrl);
if (imageUrls.contains(s3ImageUrl)) {
imageUrls.remove(s3ImageUrl);
spot.setImageUrls(imageUrls);
// S3에서 이미지 삭제
s3Service.deleteImagebyUrl(user, deleteImageUrl);
s3Service.deleteImagebyUrl(user, s3ImageUrl);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ public record BoomarkLikeResponseDto(
int totalPages
) {
public static BoomarkLikeResponseDto of(Spot spot, Boolean isBookmark, Boolean isLike, PageInfo pageInfo) {
String spotImageurl = spot.getImageUrls().isEmpty() ? "" : spot.getImageUrls().get(0);
return BoomarkLikeResponseDto.builder()
.spotName(spot.getSpotName())
.spotImageurl(spot.getImageUrls().get(0))
.spotImageurl(spotImageurl)
.createDate(spot.getCreatedDate())
.spotId(spot.getId())
.review(spot.getReview())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cona.KUsukKusuk.user.dto;

import com.cona.KUsukKusuk.global.s3.ImageUrlConverter;
import com.cona.KUsukKusuk.user.domain.User;
import lombok.Builder;

Expand All @@ -12,12 +13,14 @@ public record UserProfileResponse(
String profileImage
) {
public static UserProfileResponse of(User user) {
// S3 URL을 CloudFront URL로 변환
String cloudFrontUrl = ImageUrlConverter.convertToCloudFrontUrl(user.getProfileimage());
return UserProfileResponse.builder()
.userId(user.getUserId())
.password(user.getNoCryptpassword())
.nickname(user.getNickname())
.email(user.getEmail())
.profileImage(user.getProfileimage())
.profileImage(cloudFrontUrl)
.build();
}
}
2 changes: 2 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ cloud:
secretKey: ${s3_secretkey}
s3:
bucketName: ${s3_bucketname}
cloudfront:
domainName: ${cloudfront_name}
region:
static: ap-northeast-2
stack:
Expand Down

0 comments on commit a188fc7

Please sign in to comment.