-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: 이미지 업로드, 삭제 실패시 재시도 추가 * refactor: 이미지 여러건 삭제 메소드 제거 * feat: 이미지 삭제 요청 비동기 처리 * style: 탭 오류 삭제 * feat: S3, 데이터베이스 불일치 해결 스케줄링 기능 구현
- Loading branch information
Showing
16 changed files
with
413 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 106 additions & 3 deletions
109
server/src/main/java/server/haengdong/application/EventImageFacadeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,128 @@ | ||
package server.haengdong.application; | ||
|
||
import java.time.Instant; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
import server.haengdong.application.response.EventImageSaveAppResponse; | ||
import server.haengdong.application.response.ImageInfo; | ||
import server.haengdong.exception.HaengdongErrorCode; | ||
import server.haengdong.exception.HaengdongException; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class EventImageFacadeService { | ||
|
||
private final EventService eventService; | ||
private final ImageService imageService; | ||
private final ExecutorService executorService; | ||
|
||
public void uploadImages(String token, List<MultipartFile> images) { | ||
eventService.validateImageCount(token, images.size()); | ||
List<String> imageNames = imageService.uploadImages(images); | ||
eventService.saveImages(token, imageNames); | ||
List<EventImageSaveAppResponse> imageNames = eventService.saveImages(token, getOriginalNames(images)); | ||
List<CompletableFuture<String>> futures = createFutures(images, imageNames); | ||
CompletableFuture<List<String>> allFutures = createAllFutures(token, futures, imageNames); | ||
|
||
try { | ||
allFutures.join(); | ||
} catch (CompletionException e) { | ||
throw new HaengdongException(HaengdongErrorCode.IMAGE_UPLOAD_FAIL, e); | ||
} | ||
} | ||
|
||
private List<CompletableFuture<String>> createFutures( | ||
List<MultipartFile> images, List<EventImageSaveAppResponse> imageNames | ||
) { | ||
return IntStream.range(0, imageNames.size()) | ||
.mapToObj(i -> CompletableFuture.supplyAsync(() -> { | ||
imageService.uploadImage(images.get(i), imageNames.get(i).name()); | ||
return imageNames.get(i).name(); | ||
}, executorService)) | ||
.toList(); | ||
} | ||
|
||
private CompletableFuture<List<String>> createAllFutures( | ||
String token, List<CompletableFuture<String>> futures, List<EventImageSaveAppResponse> imageNames | ||
) { | ||
return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])) | ||
.thenApply(v -> applyFutures(futures)) | ||
.exceptionally(ex -> { | ||
eventService.deleteImages(token, getImageIds(imageNames)); | ||
getSuccessUploadImages(futures).forEach(imageService::deleteImage); | ||
|
||
throw new HaengdongException(HaengdongErrorCode.IMAGE_UPLOAD_FAIL, ex); | ||
}); | ||
} | ||
|
||
private List<String> applyFutures(List<CompletableFuture<String>> futures) { | ||
return futures.stream() | ||
.map(CompletableFuture::join) | ||
.toList(); | ||
} | ||
|
||
private List<String> getSuccessUploadImages(List<CompletableFuture<String>> futures) { | ||
return futures.stream() | ||
.filter(future -> future.isDone() && !future.isCompletedExceptionally()) | ||
.map(CompletableFuture::join) | ||
.toList(); | ||
} | ||
|
||
private List<String> getOriginalNames(List<MultipartFile> images) { | ||
return images.stream() | ||
.map(MultipartFile::getOriginalFilename) | ||
.toList(); | ||
} | ||
|
||
private List<Long> getImageIds(List<EventImageSaveAppResponse> imageNames) { | ||
return imageNames.stream() | ||
.map(EventImageSaveAppResponse::id) | ||
.toList(); | ||
} | ||
|
||
public void deleteImage(String token, Long imageId) { | ||
String imageName = eventService.deleteImage(token, imageId); | ||
imageService.deleteImage(imageName); | ||
} | ||
|
||
@Scheduled(cron = "0 0 0 * * MON") | ||
public void removeUnmatchedImage() { | ||
Instant endDate = Instant.now().minus(1, ChronoUnit.DAYS); | ||
|
||
List<EventImageSaveAppResponse> savedEventImages = eventService.findImagesDateBefore(endDate); | ||
List<ImageInfo> foundImages = imageService.findImages(); | ||
|
||
removeImageNotInS3(savedEventImages, foundImages); | ||
removeImageNotInRepository(savedEventImages, foundImages); | ||
} | ||
|
||
private void removeImageNotInS3(List<EventImageSaveAppResponse> eventImages, List<ImageInfo> images) { | ||
Set<String> imageNames = images.stream() | ||
.map(ImageInfo::name) | ||
.collect(Collectors.toSet()); | ||
|
||
eventImages.stream() | ||
.filter(eventImage -> !imageNames.contains(eventImage.name())) | ||
.map(EventImageSaveAppResponse::id) | ||
.forEach(eventService::deleteImage); | ||
} | ||
|
||
private void removeImageNotInRepository(List<EventImageSaveAppResponse> eventImages, List<ImageInfo> images) { | ||
Set<String> imageNames = eventImages.stream() | ||
.map(EventImageSaveAppResponse::name) | ||
.collect(Collectors.toSet()); | ||
|
||
images.stream() | ||
.filter(imageInfo -> imageInfo.createAt().isBefore(Instant.now().minus(2, ChronoUnit.DAYS))) | ||
.map(ImageInfo::name) | ||
.filter(name -> !imageNames.contains(name)) | ||
.forEach(imageService::deleteImage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.