diff --git a/backend/moment/moment-server/core/src/main/java/com/moment/core/controller/UserController.java b/backend/moment/moment-server/core/src/main/java/com/moment/core/controller/UserController.java index a8ded17d62..87762a7d58 100644 --- a/backend/moment/moment-server/core/src/main/java/com/moment/core/controller/UserController.java +++ b/backend/moment/moment-server/core/src/main/java/com/moment/core/controller/UserController.java @@ -36,7 +36,7 @@ public class UserController { public ResponseEntity registerUser( @RequestBody UserRequestDTO.registerUser request ) { - s3Service.createFolder(request.getId().toString()); +// s3Service.createFolder(request.getId().toString()); userService.save(request); return ResponseEntity.ok(APIResponse.of(SuccessCode.INSERT_SUCCESS)); } diff --git a/backend/moment/moment-server/core/src/main/java/com/moment/core/service/CardViewService.java b/backend/moment/moment-server/core/src/main/java/com/moment/core/service/CardViewService.java index 5237a5a563..f7dedd98c4 100644 --- a/backend/moment/moment-server/core/src/main/java/com/moment/core/service/CardViewService.java +++ b/backend/moment/moment-server/core/src/main/java/com/moment/core/service/CardViewService.java @@ -150,6 +150,7 @@ public void deleteRecord(Long cardViewId) { CardView cardView = cardViewRepository.findById(cardViewId).orElseThrow(() -> new IllegalArgumentException("존재하지 않는 카드뷰입니다.")); // cardView에 엮인 사진들 먼저 삭제 imageFileService.deleteAll(cardView); + s3Service.deleteFile(cardView.getRecordFileName()); boolean isAnalyzed = cardView.getRecordFileStatus().equals("WAIT"); // 만약 tripfile의 Trip이 untitled일 경우 // 만약 tripfile의 크기가 1이라면 tripFile과 cardView 전부 삭제, untitledTrip의 analyzingCount 감소 diff --git a/backend/moment/moment-server/core/src/main/java/com/moment/core/service/ImageFileService.java b/backend/moment/moment-server/core/src/main/java/com/moment/core/service/ImageFileService.java index 92a800767f..c68406d18d 100644 --- a/backend/moment/moment-server/core/src/main/java/com/moment/core/service/ImageFileService.java +++ b/backend/moment/moment-server/core/src/main/java/com/moment/core/service/ImageFileService.java @@ -13,6 +13,7 @@ import java.io.IOException; import java.util.List; +import java.util.Objects; import java.util.UUID; @Service @@ -22,6 +23,7 @@ public class ImageFileService { private final ImageFileRepository imageFileRepository; private final S3Service s3Service; private final CardViewRepository cardViewRepository; + private final UserService userService; @Transactional public void deleteAll(CardView cardView) { @@ -37,7 +39,8 @@ public void uploadAll(List imageFiles, Long cardViewId, Long user for (MultipartFile imageFile : imageFiles) { CardView cv = cardViewRepository.findById(cardViewId).orElseThrow(() -> new IllegalArgumentException("해당 카드뷰가 없습니다.")); String filename = UUID.randomUUID().toString(); - String url = s3Service.uploadFile(imageFile, userId, filename, false); + String extension = Objects.requireNonNull(imageFile.getOriginalFilename()).substring(imageFile.getOriginalFilename().lastIndexOf(".")); + String url = s3Service.uploadFile(imageFile, userId, filename + extension, false); ImageFile image = ImageFile.builder() .fileUrl(url) .fileName(filename) @@ -48,4 +51,13 @@ public void uploadAll(List imageFiles, Long cardViewId, Long user } + @Transactional + public void deleteImages(List images, Long userId) { + for (Long imageId : images) { + ImageFile image = imageFileRepository.findById(imageId).orElseThrow(() -> new IllegalArgumentException("해당 이미지가 없습니다.")); + userService.validateUserWithCardView(userId, image.getCardView().getId()); + s3Service.deleteFile(image.getFileName()); + imageFileRepository.delete(image); + } + } } diff --git a/backend/moment/moment-server/core/src/main/java/com/moment/core/service/S3Service.java b/backend/moment/moment-server/core/src/main/java/com/moment/core/service/S3Service.java index 0eff7dce8d..5810f6e840 100644 --- a/backend/moment/moment-server/core/src/main/java/com/moment/core/service/S3Service.java +++ b/backend/moment/moment-server/core/src/main/java/com/moment/core/service/S3Service.java @@ -39,16 +39,16 @@ public String uploadFile(MultipartFile image, Long userId, String fileName, bool } public String uploadToS3(MultipartFile image, Long userId, String fileName, boolean isRecord) throws IOException { String s3FileName = s3folderPath + userId.toString() + "/" + fileName; //S3에 저장될 파일 명 - String extention = Objects.requireNonNull(image.getOriginalFilename()).substring(image.getOriginalFilename().lastIndexOf(".")); + String extension = Objects.requireNonNull(image.getOriginalFilename()).substring(image.getOriginalFilename().lastIndexOf(".")); InputStream is = image.getInputStream(); byte[] bytes = IOUtils.toByteArray(is); //image를 byte[]로 변환 ObjectMetadata metadata = new ObjectMetadata(); //metadata 생성 if (isRecord) { - metadata.setContentType("audio/" + extention); + metadata.setContentType("audio/" + extension); } else { - metadata.setContentType("image/" + extention); + metadata.setContentType("image/" + extension); } metadata.setContentLength(bytes.length);