Skip to content

Commit 0b094fc

Browse files
authored
Merge pull request #146 from prgrms-web-devcourse-final-project/feature/EA3-158-userimgupload
[EA3-158] fix:회원 이미지 수정 기능 수정
2 parents 2ffba36 + 83fb0fa commit 0b094fc

File tree

3 files changed

+52
-22
lines changed

3 files changed

+52
-22
lines changed

src/main/java/grep/neogul_coder/domain/users/controller/UserController.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
import grep.neogul_coder.domain.users.service.UserService;
88
import grep.neogul_coder.global.auth.Principal;
99
import grep.neogul_coder.global.response.ApiResponse;
10-
import grep.neogul_coder.global.utils.upload.FileUploadResponse;
11-
import grep.neogul_coder.global.utils.upload.FileUsageType;
12-
import grep.neogul_coder.global.utils.upload.uploader.GcpFileUploader;
1310
import jakarta.validation.Valid;
1411
import lombok.RequiredArgsConstructor;
1512
import org.springframework.http.HttpStatus;
@@ -26,7 +23,6 @@
2623
public class UserController implements UserSpecification {
2724

2825
private final UserService usersService;
29-
// private final GcpFileUploader fileManager;
3026

3127
@GetMapping("/me")
3228
public ApiResponse<UserResponse> get(@AuthenticationPrincipal Principal principal) {
@@ -46,20 +42,7 @@ public ApiResponse<Void> updateProfile(
4642
@RequestPart("nickname") String nickname,
4743
@RequestPart(value = "profileImage", required = false) MultipartFile profileImage
4844
) throws IOException {
49-
50-
// String profileImageUrl = null;
51-
//
52-
// if (profileImage != null && !profileImage.isEmpty()) {
53-
// FileUploadResponse response = fileManager.upload(
54-
// profileImage,
55-
// principal.getUserId(),
56-
// FileUsageType.PROFILE,
57-
// principal.getUserId()
58-
// );
59-
// profileImageUrl = response.fileUrl();
60-
// }
61-
//
62-
// usersService.updateProfile(principal.getUserId(), nickname, profileImageUrl);
45+
usersService.updateProfile(principal.getUserId(), nickname, profileImage);
6346
return ApiResponse.noContent();
6447
}
6548

src/main/java/grep/neogul_coder/domain/users/entity/User.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ public static User UserInit(String email, String password, String nickname) {
4343

4444
public void updateProfile(String nickname, String profileImageUrl) {
4545
this.nickname = nickname;
46-
this.profileImageUrl = profileImageUrl;
46+
if (profileImageUrl != null) {
47+
this.profileImageUrl = profileImageUrl;
48+
}
4749
}
4850

4951
public void updatePassword(String password) {

src/main/java/grep/neogul_coder/domain/users/service/UserService.java

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,18 @@
1616
import grep.neogul_coder.domain.users.exception.UserNotFoundException;
1717
import grep.neogul_coder.domain.users.exception.code.UserErrorCode;
1818
import grep.neogul_coder.domain.users.repository.UserRepository;
19+
import grep.neogul_coder.global.utils.upload.FileUploadResponse;
20+
import grep.neogul_coder.global.utils.upload.FileUsageType;
21+
import grep.neogul_coder.global.utils.upload.uploader.GcpFileUploader;
22+
import grep.neogul_coder.global.utils.upload.uploader.LocalFileUploader;
1923
import jakarta.transaction.Transactional;
24+
import java.io.IOException;
2025
import lombok.RequiredArgsConstructor;
26+
import org.springframework.beans.factory.annotation.Autowired;
27+
import org.springframework.core.env.Environment;
2128
import org.springframework.security.crypto.password.PasswordEncoder;
2229
import org.springframework.stereotype.Service;
30+
import org.springframework.web.multipart.MultipartFile;
2331

2432
@Transactional
2533
@Service
@@ -34,6 +42,15 @@ public class UserService {
3442
private final LinkService linkService;
3543
private final StudyManagementService studyManagementService;
3644

45+
@Autowired(required = false)
46+
private GcpFileUploader gcpFileUploader;
47+
48+
@Autowired(required = false)
49+
private LocalFileUploader localFileUploader;
50+
51+
@Autowired
52+
private Environment environment;
53+
3754

3855
public User get(Long id) {
3956
User user = findUser(id);
@@ -66,10 +83,24 @@ public void signUp(SignUpRequest request) {
6683
linkRepository.save(Link.LinkInit(user.getId(), null, null));
6784
}
6885

69-
public void updateProfile(Long id, String nickname, String profileImageUrl) {
70-
User user = findUser(id);
86+
@Transactional
87+
public void updateProfile(Long userId, String nickname, MultipartFile profileImage)
88+
throws IOException {
89+
90+
User user = findUser(userId);
7191
isDuplicateNickname(nickname);
72-
user.updateProfile(nickname, profileImageUrl);
92+
93+
String uploadedImageUrl;
94+
if (isProfileImgExists(profileImage)) {
95+
FileUploadResponse response = isProductionEnvironment()
96+
? gcpFileUploader.upload(profileImage, userId, FileUsageType.PROFILE, userId)
97+
: localFileUploader.upload(profileImage, userId, FileUsageType.PROFILE, userId);
98+
uploadedImageUrl = response.fileUrl();
99+
} else {
100+
uploadedImageUrl = user.getProfileImageUrl();
101+
}
102+
103+
user.updateProfile(nickname, uploadedImageUrl);
73104
}
74105

75106
public void updatePassword(Long id, String password, String newPassword,
@@ -162,6 +193,20 @@ private boolean isNotMatchCurrentPassword(String inputPassword, String storedPas
162193
private boolean isNotMatchPasswordCheck(String password, String passwordCheck) {
163194
return !password.equals(passwordCheck);
164195
}
196+
197+
private boolean isProductionEnvironment() {
198+
for (String profile : environment.getActiveProfiles()) {
199+
if ("prod".equals(profile)) {
200+
return true;
201+
}
202+
}
203+
return false;
204+
}
205+
206+
private boolean isProfileImgExists(MultipartFile profileImage) {
207+
return profileImage != null && !profileImage.isEmpty();
208+
}
209+
165210
}
166211

167212

0 commit comments

Comments
 (0)