Skip to content

Commit

Permalink
이미지서비스 뱐걍
Browse files Browse the repository at this point in the history
  • Loading branch information
Johyunik committed Dec 4, 2023
1 parent d19d710 commit 3db1e81
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
1 change: 0 additions & 1 deletion src/main/java/com/weatherfit/board/dto/BoardUpdateDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@ public class BoardUpdateDTO {
private List<String> hashTag;
private List<ImageEntity> images;
private Integer[] deletedImages;

}
16 changes: 10 additions & 6 deletions src/main/java/com/weatherfit/board/service/BoardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -172,16 +172,20 @@ public void patchBoard(int boardId, String boardJson, MultipartFile[] images, St

for (MultipartFile image : images) {
String imageUrl = imageService.saveImage(image);
ImageEntity imageEntity = ImageEntity.builder()
.imageUrl(imageUrl)
.boardId(originalBoard)
.build();
imageRepository.save(imageEntity);

originalBoard.getImages().add(imageEntity);
// 이미지가 이미 저장되어 있는지 확인
if (!imageRepository.existsByImageUrl(imageUrl)) {
ImageEntity imageEntity = ImageEntity.builder()
.imageUrl(imageUrl)
.boardId(originalBoard)
.build();
imageRepository.save(imageEntity);

originalBoard.getImages().add(imageEntity);
}
}


if (boardUpdateDTO.getDeletedImages() != null) {
for (Integer imageId : boardUpdateDTO.getDeletedImages()) {
Optional<ImageEntity> optionalImageEntity = imageRepository.findById(imageId);
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/weatherfit/board/service/ImageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ public String saveImage(MultipartFile file) {
String originalFilename = file.getOriginalFilename();
String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));

// 파일 이름에 원래 이름을 사용합니다.
String fileName = originalFilename;
// UUID를 생성하고, 이를 파일 이름에 포함합니다.
String uuid = UUID.randomUUID().toString();
String fileName = uuid + "_" + originalFilename;
String fileUrl = "https://" + bucketName + ".s3.amazonaws.com/" + fileName;

if (!amazonS3Client.doesObjectExist(bucketName, fileName)) {
Expand All @@ -49,6 +50,7 @@ public String saveImage(MultipartFile file) {
throw new RuntimeException("Failed to upload image to S3", e);
}
}

public void deleteImage(String imageUrl) {
String imageName = imageUrl.substring(imageUrl.lastIndexOf("/") + 1);
amazonS3Client.deleteObject(bucketName, imageName);
Expand Down

0 comments on commit 3db1e81

Please sign in to comment.