-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[feat] #25 이미지 업로드 및 게시글과 함께 저장
- Loading branch information
Showing
18 changed files
with
332 additions
and
35 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
11 changes: 11 additions & 0 deletions
11
src/main/java/com/leets/X/domain/image/dto/request/ImageDto.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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.leets.X.domain.image.dto.request; | ||
|
||
public record ImageDto( | ||
String name, | ||
String url | ||
) { | ||
public static ImageDto of(String name, String url) { | ||
return new ImageDto(name, url); | ||
} | ||
} | ||
|
13 changes: 13 additions & 0 deletions
13
src/main/java/com/leets/X/domain/image/dto/response/ImageResponse.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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.leets.X.domain.image.dto.response; | ||
|
||
import com.leets.X.domain.image.domain.Image; | ||
|
||
public record ImageResponse( | ||
Long id, | ||
String name, | ||
String url | ||
) { | ||
public static ImageResponse from(Image image) { | ||
return new ImageResponse(image.getId(), image.getName(), image.getUrl()); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/leets/X/domain/image/exception/ErrorMessage.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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.leets.X.domain.image.exception; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum ErrorMessage { | ||
|
||
S3_UPLOAD_FAIL(500, "이미지 업로드 중 에러가 발생했습니다."); | ||
|
||
private final int code; | ||
private final String message; | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/com/leets/X/domain/image/exception/S3UploadException.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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.leets.X.domain.image.exception; | ||
|
||
import com.leets.X.global.common.exception.BaseException; | ||
|
||
public class S3UploadException extends BaseException { | ||
public S3UploadException() { | ||
super(ErrorMessage.S3_UPLOAD_FAIL.getCode(), ErrorMessage.S3_UPLOAD_FAIL.getMessage()); | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/leets/X/domain/image/repository/ImageRepository.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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.leets.X.domain.image.repository; | ||
|
||
import com.leets.X.domain.image.domain.Image; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface ImageRepository extends JpaRepository<Image, Long> { | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/leets/X/domain/image/service/ImageService.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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.leets.X.domain.image.service; | ||
|
||
import com.leets.X.domain.image.domain.Image; | ||
import com.leets.X.domain.image.dto.request.ImageDto; | ||
import com.leets.X.domain.image.repository.ImageRepository; | ||
import com.leets.X.domain.post.domain.Post; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ImageService { | ||
|
||
private final ImageUploadService imageUploadService; | ||
private final ImageRepository imageRepository; | ||
|
||
@Transactional | ||
public List<Image> save(List<MultipartFile> file, Post post) throws IOException { | ||
List<ImageDto> dtoList = imageUploadService.uploadImages(file); | ||
|
||
List<Image> imageList = dtoList.stream() | ||
.map((ImageDto dto) -> Image.from(dto, post)) | ||
.toList(); | ||
|
||
return imageRepository.saveAll(imageList); | ||
} | ||
|
||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/com/leets/X/domain/image/service/ImageUploadService.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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.leets.X.domain.image.service; | ||
|
||
import com.leets.X.domain.image.dto.request.ImageDto; | ||
import com.leets.X.domain.image.exception.S3UploadException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
import software.amazon.awssdk.core.sync.RequestBody; | ||
import software.amazon.awssdk.services.s3.S3Client; | ||
import software.amazon.awssdk.services.s3.model.PutObjectRequest; | ||
import software.amazon.awssdk.services.s3.model.PutObjectResponse; | ||
import software.amazon.awssdk.services.s3.model.S3Exception; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class ImageUploadService { | ||
@Value("${aws.s3.bucketName}") | ||
private String bucketName; | ||
@Value("${aws.s3.region}") | ||
private String region; | ||
|
||
private final S3Client s3Client; | ||
|
||
public List<ImageDto> uploadImages(List<MultipartFile> files) throws IOException { | ||
|
||
List<ImageDto> images = new ArrayList<>(); | ||
|
||
for (MultipartFile file : files) { | ||
String originalName = file.getOriginalFilename(); | ||
String fileName = generateFileName(originalName); | ||
|
||
try { | ||
// PutObjectRequest 생성 및 설정 | ||
PutObjectRequest putObjectRequest = PutObjectRequest.builder() | ||
.bucket(bucketName) | ||
.key(fileName) | ||
.contentType(file.getContentType()) | ||
.build(); | ||
|
||
// S3에 파일 업로드 | ||
PutObjectResponse response = s3Client.putObject( | ||
putObjectRequest, | ||
RequestBody.fromInputStream(file.getInputStream(), file.getSize()) | ||
); | ||
|
||
// 업로드 성공 여부 확인 | ||
if (response.sdkHttpResponse().isSuccessful()) { | ||
// 업로드된 파일의 URL을 ImageDto로 추가 | ||
images.add(ImageDto.of(originalName, generateFileUrl(fileName))); | ||
} else { | ||
throw new S3UploadException(); | ||
} | ||
} catch (S3Exception e) { | ||
throw new S3UploadException(); | ||
} | ||
} | ||
|
||
return images; | ||
} | ||
// S3에 저장된 파일 URL 생성 | ||
private String generateFileUrl(String fileName) { | ||
return String.format("https://%s.s3.%s.amazonaws.com/%s", bucketName, region, fileName); | ||
} | ||
|
||
// 파일 이름을 고유하게 생성하는 메서드 | ||
private String generateFileName(String originalFileName) { | ||
String uuid = UUID.randomUUID().toString(); | ||
return uuid + "_" + originalFileName.replaceAll("\\s+", "_"); | ||
} | ||
|
||
} |
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
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.