Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,16 @@ public ResponseEntity<?> uploadMenuImage(
}

MenuImageUploadResponse response = menuImageService.save(menuId, file);
return ResponseEntity.status(HttpStatus.CREATED).body(ApiUtils.success(response));
return ResponseEntity
.status(
HttpStatus.CREATED
)
.body(
ApiUtils
.success(
response
)
);
}

@DeleteMapping("/images/{menuImageId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public MenuImageUploadResponse save(Long menuId, MultipartFile file) {
// MenuImage 엔티티 생성 및 저장
MenuImage menuImage = MenuImage.builder()
.menu(menu)
.imageUrl(uploadResult.url())
.imageUrl(uploadResult.resizedUrl())
.fileKey(uploadResult.key())
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public ResponseEntity<?> uploadStoreProfileImage(
description = "주점 이미지를 삭제합니다. 이미지 ID를 사용하여 특정 이미지를 삭제할 수 있습니다."
)
@ApiResponse(responseCode = "200", description = "주점 이미지 삭제 성공")
public ResponseEntity<?> deleteStoreImage(@PathVariable Long imageId) {
storeImageService.delete(imageId);
public ResponseEntity<?> deleteStoreImage(@PathVariable Long storeImageId) {
storeImageService.delete(storeImageId);
return ResponseEntity
.status(HttpStatus.OK)
.body(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public List<StoreImageUploadResponse> saveAll(Long storeId, List<MultipartFile>
if (files == null || files.isEmpty())
throw new StoreImageEmptyException();

String type = "store";
String type = "banner";
Store store = storeRepository.findById(storeId)
.orElseThrow(StoreNotFoundException::new);

Expand All @@ -61,7 +61,7 @@ public List<StoreImageUploadResponse> saveAll(Long storeId, List<MultipartFile>
for (S3Service.S3UploadResult uploadResult : uploadResults) {
StoreImage storeImage = StoreImage.builder()
.store(store)
.imageUrl(uploadResult.url())
.imageUrl(uploadResult.resizedUrl())
.fileKey(uploadResult.key())
.imageType(ImageType.BANNER)
.build();
Expand All @@ -76,7 +76,7 @@ public List<StoreImageUploadResponse> saveAll(Long storeId, List<MultipartFile>
@Transactional
public StoreImageUploadResponse saveProfileImage(Long storeId, MultipartFile file) {

String type = "store";
String type = "profile";
Store store = storeRepository.findById(storeId)
.orElseThrow(StoreNotFoundException::new);

Expand All @@ -93,7 +93,7 @@ public StoreImageUploadResponse saveProfileImage(Long storeId, MultipartFile fil
// StoreImage 엔티티 생성 및 저장
StoreImage storeImage = StoreImage.builder()
.store(store)
.imageUrl(uploadResult.url())
.imageUrl(uploadResult.resizedUrl())
.fileKey(uploadResult.key())
.imageType(ImageType.PROFILE)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,40 @@ public class S3Service {
private final AmazonS3Client amazonS3Client;

@Value("${cloud.aws.s3.bucket}")
private String bucket;
private String originalBucket;

public record S3UploadResult(String key, String url) {
@Value("${cloud.aws.s3.resize-bucket}")
private String resizeBucket;

public record S3UploadResult(String key, String originalUrl, String resizedUrl) {
}

@Bulkhead(name = "s3UploadBulkhead", type = Bulkhead.Type.THREADPOOL)
@Async("s3UploadExecutor")
public CompletableFuture<S3UploadResult> upload(String type, Long refId, MultipartFile file) { // TODO MultipartFile 분리 필요 (Spring에 의존하면 안 됨)
public CompletableFuture<S3UploadResult> upload(String type, Long refId,
MultipartFile file) { // TODO MultipartFile 분리 필요 (Spring에 의존하면 안 됨)
try (InputStream inputStream = file.getInputStream()) {
String key = createFileKey(type, refId, file.getOriginalFilename());
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(file.getSize());

amazonS3Client.putObject(bucket, key, inputStream, metadata);
String url = amazonS3Client.getUrl(bucket, key).toString();
// 1) 원본 버킷에 이미지 업로드
amazonS3Client.putObject(originalBucket, key, inputStream, metadata);

// 2) 각 버킷의 URL 생성
String originalUrl = amazonS3Client.getUrl(originalBucket, key).toString();
String resizedUrl = amazonS3Client.getUrl(resizeBucket, key).toString();

return CompletableFuture.completedFuture(new S3UploadResult(key, url));
return CompletableFuture.completedFuture(new S3UploadResult(key, originalUrl, resizedUrl));
} catch (Exception e) {
throw new RuntimeException("S3 업로드 실패", e);
}
}

public void delete(String filename) {
public void delete(String key) {
try {
amazonS3Client.deleteObject(bucket, filename);
amazonS3Client.deleteObject(originalBucket, key);
amazonS3Client.deleteObject(resizeBucket, key);
} catch (Exception e) {
throw new RuntimeException("S3 파일 삭제 실패", e);
}
Expand Down