Skip to content

Commit

Permalink
이미지 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Johyunik committed Dec 2, 2023
1 parent b4a608f commit 65992f4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/main/java/com/weatherfit/board/service/BoardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class BoardService {
private final ImageService imageService;
@Autowired
private LikeService likeService;

private final AmazonS3Client amazonS3Client;

// 게시글 전체 조회
public List<BoardListResponseDTO> findAll() {
Expand Down Expand Up @@ -156,7 +156,11 @@ public void patchBoard(int boardId, String boardJson, MultipartFile[] images, St
BoardEntity originalBoard = optionalBoard.orElseThrow(() -> new IllegalArgumentException("해당 게시글이 존재하지 않습니다. id=" + boardId));

ObjectMapper objectMapper = new ObjectMapper();
imageRepository.deleteByBoardId(originalBoard);

// 기존 이미지 삭제
for (ImageEntity image : originalBoard.getImages()) {
imageService.deleteImage(image.getImage_url());
}

BoardUpdateDTO boardUpdateDTO;
try {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/weatherfit/board/service/ImageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class ImageService {
@Value("${cloud.aws.s3.bucket}")
private String bucketName;


public String saveImage(MultipartFile file) {
try {
String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
Expand All @@ -48,6 +49,10 @@ 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 65992f4

Please sign in to comment.