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 3a07112 commit 52d3ec3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/weatherfit/board/service/BoardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class BoardService {
@Value("${cloud.aws.s3.bucket}")
private String bucketName;


// 게시글 전체 조회
public List<BoardListResponseDTO> findAll() {
List<BoardEntity> entities = boardRepository.findAll();
Expand Down Expand Up @@ -171,15 +172,19 @@ public void patchBoard(int boardId, String boardJson, MultipartFile[] images, St
throw new RuntimeException(e);
}


// 게시글에 연결된 모든 이미지를 S3에서 삭제합니다.
for (ImageEntity imageEntity : originalBoard.getImages()) {
System.out.println("Deleting image with file name: " + imageEntity.getFileName());
imageService.deleteImage(imageEntity);
imageRepository.delete(imageEntity);
}
originalBoard.getImages().clear();

System.out.println("Number of uploaded images: " + (images != null ? images.length : 0));
for (MultipartFile image : images) {
String fileName = imageService.saveImage(image); // saveImage 메소드에서 반환받은 파일 이름
System.out.println("Saved image with file name: " + fileName);
String imageUrl = "https://" + bucketName + ".s3.amazonaws.com/" + fileName;

// 이미지가 이미 저장되어 있는지 확인
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 @@ -52,7 +52,7 @@ public String saveImage(MultipartFile file) {
amazonS3Client.putObject(bucketName, fileName, file.getInputStream(), metadata);
}

return fileName;
return fileUrl;
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Failed to upload image to S3", e);
Expand All @@ -61,9 +61,11 @@ public String saveImage(MultipartFile file) {


public void deleteImage(ImageEntity imageEntity) {
String fileName = imageEntity.getFileName();
String imageUrl = imageEntity.getImageUrl();
String fileName = imageUrl.substring(imageUrl.lastIndexOf("/") + 1);
amazonS3Client.deleteObject(bucketName, fileName);
}

}


Expand Down

0 comments on commit 52d3ec3

Please sign in to comment.