Skip to content

Commit

Permalink
Refactor: 파일이 없는 경우에 대한 exception 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
ibaesuyeon committed Jan 13, 2024
1 parent 2d9c4ec commit b067dfb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@
public class DefaultNullPointerException extends NullPointerException{

private ErrorCode errorCode;
private String message;

public DefaultNullPointerException(ErrorCode errorCode) {
super(errorCode.getMessage());
this.errorCode = errorCode;
}
public DefaultNullPointerException(ErrorCode errorCode, String message) {
super(errorCode.getMessage());
this.errorCode = errorCode;
this.message = message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import depth.main.ideac.global.error.DefaultException;
import depth.main.ideac.global.payload.ErrorCode;
import depth.main.ideac.domain.user.domain.repository.UserRepository;
import depth.main.ideac.global.service.FileService;
import depth.main.ideac.global.service.S3Service;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
Expand All @@ -34,7 +34,7 @@ public class ClubPostService {
private final UserRepository userRepository;
private final ClubPostRepository clubPostRepository;
private final ClubPostImageRepository clubPostImageRepository;
private final FileService fileService;
private final S3Service s3Service;

// 전체 조회
public Page<ClubPostRes> getAllClubPosts(Pageable pageable) {
Expand Down Expand Up @@ -151,16 +151,16 @@ public void uploadFile(ClubPost clubPost, List<MultipartFile> images) throws IOE
boolean isFirstImage = true;

for (MultipartFile image : images) {
String s3ImageKey = fileService.uploadFile(image, getClass().getSimpleName());
String s3key = s3Service.uploadFile(image, getClass().getSimpleName());

// 첫 번째 이미지인 경우에만 thumbnail로 설정
boolean isThumbnail = isFirstImage;
isFirstImage = false;

clubPostImages.add(ClubPostImage.builder()
.imagePath(fileService.getUrl(s3ImageKey))
.imagePath(s3Service.getUrl(s3key))
.isThumbnail(isThumbnail)
.s3key(s3ImageKey)
.s3key(s3key)
.clubPost(clubPost)
.build());
}
Expand All @@ -171,7 +171,7 @@ public void uploadFile(ClubPost clubPost, List<MultipartFile> images) throws IOE
public void deleteFile(Long clubId){
List<ClubPostImage> images = clubPostImageRepository.findByClubPostId(clubId);
for(ClubPostImage image : images) {
fileService.deleteFile(image.getS3key()); //s3 삭제
s3Service.deleteFile(image.getS3key()); //s3 삭제
clubPostImageRepository.deleteById(image.getId()); //엔티티 삭제
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
import depth.main.ideac.domain.user.domain.repository.UserRepository;
import depth.main.ideac.global.error.DefaultException;
import depth.main.ideac.global.payload.ErrorCode;
import depth.main.ideac.global.service.FileService;
import depth.main.ideac.global.service.S3Service;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.*;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
Expand All @@ -35,7 +34,7 @@
@RequiredArgsConstructor
public class ProjectPostService {

private final FileService fileService;
private final S3Service s3Service;
private final ProjectPostRepository projectPostRepository;
private final ProjectPostImageRepository projectPostImageRepository;
private final UserRepository userRepository;
Expand Down Expand Up @@ -212,16 +211,16 @@ public void uploadFile(ProjectPost projectPost, List<MultipartFile> images) thro
boolean isFirstImage = true;

for (MultipartFile image : images) {
String s3ImageKey = fileService.uploadFile(image, getClass().getSimpleName());
String s3key = s3Service.uploadFile(image, getClass().getSimpleName());

// 첫 번째 이미지인 경우에만 thumbnail로 설정
boolean isThumbnail = isFirstImage;
isFirstImage = false;

projectPostImages.add(ProjectPostImage.builder()
.imagePath(fileService.getUrl(s3ImageKey))
.imagePath(s3Service.getUrl(s3key))
.isThumbnail(isThumbnail)
.s3key(s3ImageKey)
.s3key(s3key)
.projectPost(projectPost)
.build());
}
Expand All @@ -232,7 +231,7 @@ public void uploadFile(ProjectPost projectPost, List<MultipartFile> images) thro
public void deleteFile(Long projectId){
List<ProjectPostImage> images = projectPostImageRepository.findByProjectPostId(projectId);
for(ProjectPostImage image : images) {
fileService.deleteFile(image.getS3key()); //s3 삭제
s3Service.deleteFile(image.getS3key()); //s3 삭제
projectPostImageRepository.deleteById(image.getId()); //엔티티 삭제
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import com.amazonaws.services.s3.model.DeleteObjectRequest;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import depth.main.ideac.global.DefaultAssert;
import depth.main.ideac.global.error.DefaultNullPointerException;
import depth.main.ideac.global.payload.ErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
Expand All @@ -18,14 +21,17 @@
@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class FileService {
public class S3Service {
@Value("${cloud.aws.s3.bucket}")
private String BUCKET;

private final AmazonS3 amazonS3;

@Transactional
public String uploadFile(MultipartFile file, String className) throws IOException {
if(file == null){
throw new DefaultNullPointerException(ErrorCode.INVALID_PARAMETER, "파일을 첨부해주세요..");
}
String fileName = convertToRandomName(file.getOriginalFilename());

ObjectMetadata objectMetadata = new ObjectMetadata();
Expand All @@ -36,20 +42,26 @@ public String uploadFile(MultipartFile file, String className) throws IOExceptio
String filePath = BUCKET + "/image/" + className;
amazonS3.putObject(new PutObjectRequest(filePath, fileName, inputStream, objectMetadata)
.withCannedAcl(CannedAccessControlList.PublicRead));
String s3ImageKey = "image/" + className + "/" + fileName;
return s3ImageKey;
String s3key = "image/" + className + "/" + fileName;
return s3key;
}

public void deleteFile(String s3ImageKey) {
amazonS3.deleteObject(new DeleteObjectRequest(BUCKET, s3ImageKey));
@Transactional
public void deleteFile(String s3key) {
if(s3key == null){
throw new DefaultNullPointerException(ErrorCode.INVALID_PARAMETER, "삭제하려는 파일을 찾을 수 없습니다.");
}
amazonS3.deleteObject(new DeleteObjectRequest(BUCKET, s3key));
}

public String convertToRandomName(String originalFileName) {
String fileExtension = originalFileName.substring(originalFileName.lastIndexOf("."));
return UUID.randomUUID().toString().concat(fileExtension);
}

public String getUrl(String s3ImageKey) {
return amazonS3.getUrl(BUCKET, s3ImageKey).toString();
public String getUrl(String s3key) {
if(s3key == null){
throw new DefaultNullPointerException(ErrorCode.INVALID_PARAMETER, "파일을 찾을 수 없습니다.");
}
return amazonS3.getUrl(BUCKET, s3key).toString();
}
}

0 comments on commit b067dfb

Please sign in to comment.