Skip to content
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

[NB-193] 프로필 이미지 저장 형식을 CDN URL로 변경 #72

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public String uploadImage(String uploadPath, MultipartFile image) {
}

public String getFullS3Path(String uploadPath) {
return String.format("https://%s.s3.%s.amazonaws.com/%s", bucketName, region, uploadPath);
return String.format("https://%s.s3.%s.amazonaws.com%s", bucketName, region, uploadPath);
}

}
7 changes: 5 additions & 2 deletions src/main/java/com/soyeon/nubim/domain/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.web.multipart.MultipartFile;

import com.soyeon.nubim.common.enums.Gender;
import com.soyeon.nubim.common.util.aws.S3AndCdnUrlConverter;
import com.soyeon.nubim.common.util.aws.S3ImageUploader;
import com.soyeon.nubim.domain.comment.CommentRepository;
import com.soyeon.nubim.domain.post.PostRepository;
Expand Down Expand Up @@ -62,6 +63,7 @@ public class UserService {
private final PostLikeRepository postLikeRepository;
private final PostRepository postRepository;
private final UserFollowRepository userFollowRepository;
private final S3AndCdnUrlConverter s3AndCdnUrlConverter;

public UserProfileResponseDto getCurrentUserProfile() {
User currentUser = loggedInUserService.getCurrentUser();
Expand Down Expand Up @@ -134,15 +136,16 @@ public User findUserByIdOrThrow(Long userId) {
public ProfileImageUpdateResponse updateProfileImage(MultipartFile profileImage) {
validateProfileImageContentType(profileImage.getContentType());

String uploadPath = "users/" + loggedInUserService.getCurrentUserId() + "/profile/" + UUID.randomUUID()
String uploadPath = "/users/" + loggedInUserService.getCurrentUserId() + "/profile/" + UUID.randomUUID()
.toString()
.substring(0, 4);
String uploadResponse = s3ImageUploader.uploadImage(uploadPath, profileImage);

if (uploadResponse.contains("fail")) {
return new ProfileImageUpdateResponse("profile image update fail", null);
}
userRepository.updateProfileImage(uploadResponse, loggedInUserService.getCurrentUserId());
String cdnUrl = s3AndCdnUrlConverter.convertPathToCdnUrl(uploadPath);
userRepository.updateProfileImage(cdnUrl, loggedInUserService.getCurrentUserId());
return new ProfileImageUpdateResponse("profile image update success", uploadResponse);
}

Expand Down