Skip to content

Commit

Permalink
refactor: 이미지 삭제 로직을 서비스로 이동
Browse files Browse the repository at this point in the history
  • Loading branch information
hgo641 committed Oct 18, 2023
1 parent f8848fa commit 395c781
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
import com.amazonaws.services.s3.model.ObjectMetadata;
import hanglog.global.exception.ImageException;
import hanglog.image.domain.ImageFile;
import hanglog.image.domain.S3ImageEvent;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

@Component
Expand All @@ -24,7 +22,6 @@ public class ImageUploader {
private static final String CACHE_CONTROL_VALUE = "max-age=3153600";

private final AmazonS3 s3Client;
private final ApplicationEventPublisher publisher;

@Value("${cloud.aws.s3.bucket}")
private String bucket;
Expand All @@ -33,14 +30,9 @@ public class ImageUploader {
private String folder;

public List<String> uploadImages(final List<ImageFile> imageFiles) {
try {
return imageFiles.stream()
.map(this::uploadImage)
.toList();
} catch (final ImageException e) {
imageFiles.forEach(imageFile -> publisher.publishEvent(new S3ImageEvent(imageFile.getHashedName())));
throw e;
}
return imageFiles.stream()
.map(this::uploadImage)
.toList();
}

private String uploadImage(final ImageFile imageFile) {
Expand Down
14 changes: 13 additions & 1 deletion backend/src/main/java/hanglog/image/service/ImageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

import hanglog.global.exception.ImageException;
import hanglog.image.domain.ImageFile;
import hanglog.image.domain.S3ImageEvent;
import hanglog.image.dto.ImagesResponse;
import hanglog.image.infrastructure.ImageUploader;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

Expand All @@ -20,16 +22,26 @@ public class ImageService {
private static final int EMPTY_LIST_SIZE = 0;

private final ImageUploader imageUploader;
private final ApplicationEventPublisher publisher;

public ImagesResponse save(final List<MultipartFile> images) {
validateSizeOfImages(images);
final List<ImageFile> imageFiles = images.stream()
.map(ImageFile::new)
.toList();
final List<String> imageNames = imageUploader.uploadImages(imageFiles);
final List<String> imageNames = uploadImages(imageFiles);
return new ImagesResponse(imageNames);
}

private List<String> uploadImages(final List<ImageFile> imageFiles) {
try {
return imageUploader.uploadImages(imageFiles);
} catch (final ImageException e) {
imageFiles.forEach(imageFile -> publisher.publishEvent(new S3ImageEvent(imageFile.getHashedName())));
throw e;
}
}

private void validateSizeOfImages(final List<MultipartFile> images) {
if (images.size() > MAX_IMAGE_LIST_SIZE) {
throw new ImageException(EXCEED_IMAGE_LIST_SIZE);
Expand Down

0 comments on commit 395c781

Please sign in to comment.