Skip to content

Commit

Permalink
Merge pull request #99 from Developer-Wikis/feature/#97
Browse files Browse the repository at this point in the history
Feat : 프로필 이미지 변경 api 추가
  • Loading branch information
jhdl0157 authored Nov 25, 2022
2 parents 1e3956f + 1f6a15b commit 49581a4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/developer/wiki/oauth/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,8 @@ public void changeUserName(String newUserName){
if(newUserName.isEmpty()) throw new BadRequestException("userName is empty");
this.name=newUserName;
}
public void changeUserProfileUrl(String newUrl){
if(newUrl.isEmpty()) throw new BadRequestException("newUrl is empty");
this.profileUrl=newUrl;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,11 @@ public ResponseEntity<NicknameDto> changeUserName(@AuthenticationPrincipal User
}

@PostMapping("/image/{userId}")
public ResponseEntity<String> changeImg(@AuthenticationPrincipal User currentUser, @RequestParam("image") MultipartFile file, @PathVariable Long userId) throws IOException {
public ResponseEntity<ImageDto> changeImg(@AuthenticationPrincipal User currentUser, @RequestParam("image") MultipartFile file, @PathVariable Long userId) throws IOException {
if(!currentUser.getId().equals(userId)) throw new BadRequestException("Not Match Userid");
if(file.isEmpty()) throw new BadRequestException("파일은 Null이 될수 없습니다.");
String originalName=file.getOriginalFilename();
System.out.println("!!!!!!!!!!!!!!!오리지널"+originalName);
String[] name=originalName.split("\\.");
System.out.println(name.length);
final String ext = name[1];
final String saveFileName = getUuid() +"."+ ext;
System.out.println(saveFileName);
return ResponseEntity.ok(awsService.upload(saveFileName,file));
}
private static String getUuid() {
return UUID.randomUUID().toString().replaceAll("-", "");
String url=userService.updateUserProfile(file, userId);
return ResponseEntity.ok(new ImageDto(url));
}

}
22 changes: 22 additions & 0 deletions src/main/java/com/developer/wiki/oauth/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@
import com.developer.wiki.common.exception.NotFoundException;
import com.developer.wiki.oauth.User;
import com.developer.wiki.oauth.UserRepository;
import com.developer.wiki.oauth.util.AwsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.transaction.Transactional;
import java.io.IOException;
import java.util.Optional;
import java.util.UUID;

@Service
@RequiredArgsConstructor
public class UserService {
private final UserRepository userRepository;
private final AwsService awsService;
@Transactional
public void deleteUser(Long userId){
userRepository.deleteById(userId);
Expand All @@ -28,4 +33,21 @@ public String updateUserName(String userName,Long userId){
return user.getName();
}

@Transactional
public String updateUserProfile(MultipartFile file, Long userId) throws IOException {
User user=userRepository.findById(userId)
.orElseThrow(()-> new NotFoundException("유저가 없습니다."));
String originalName=file.getOriginalFilename();
String[] name=originalName.split("\\.");
final String ext = name[1];
final String saveFileName = getUuid() +"."+ ext;
System.out.println(saveFileName);
var newUrl=awsService.upload(saveFileName,file);
user.changeUserProfileUrl(newUrl);
return newUrl;
}
private static String getUuid() {
return UUID.randomUUID().toString().replaceAll("-", "");
}

}
6 changes: 4 additions & 2 deletions src/main/java/com/developer/wiki/oauth/util/AwsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@

import com.amazonaws.AmazonClientException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.Bucket;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.*;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.TransferManagerBuilder;
import com.amazonaws.services.s3.transfer.Upload;
Expand Down Expand Up @@ -48,6 +47,9 @@ public String upload(String saveFileName,MultipartFile multipartFile) throws IOE
final Upload upload = transferManager.upload(request);
try {
upload.waitForCompletion();
AccessControlList accessControlList = s3Config.amazonS3Client().getObjectAcl(bucketName, saveFileName);
accessControlList.grantPermission(GroupGrantee.AllUsers, Permission.Read);
s3Config.amazonS3Client().setObjectAcl(bucketName, saveFileName, accessControlList);
return defaultUrl+saveFileName;
} catch (AmazonClientException | InterruptedException amazonClientException) {
amazonClientException.printStackTrace();
Expand Down

0 comments on commit 49581a4

Please sign in to comment.