1616import grep .neogul_coder .domain .users .exception .UserNotFoundException ;
1717import grep .neogul_coder .domain .users .exception .code .UserErrorCode ;
1818import 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 ;
1923import jakarta .transaction .Transactional ;
24+ import java .io .IOException ;
2025import lombok .RequiredArgsConstructor ;
26+ import org .springframework .beans .factory .annotation .Autowired ;
27+ import org .springframework .core .env .Environment ;
2128import org .springframework .security .crypto .password .PasswordEncoder ;
2229import 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