-
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: gitignore 추가 * feat: 이미지 업로드/다운로드 기능 추가 * feat: 이미지 업로드/다운로드 기능 추가 * feat: 운영, 개발 image 설정 분리 * feat: 서브모듈 삭제 * test: imageUploadService 추가 * test: 행사 이미지 저장 및 조회 기능 테스트 * feat: 이미지 삭제 기능 구현 * test: 이미지 삭제 테스트 추가 --------- Co-authored-by: juha <[email protected]>
- Loading branch information
Showing
40 changed files
with
526 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -216,7 +216,7 @@ $RECYCLE.BIN/ | |
*.lnk | ||
|
||
### Gradle ### | ||
.gradle | ||
server/.gradle | ||
**/build/ | ||
!src/**/build/ | ||
|
||
|
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
72 changes: 72 additions & 0 deletions
72
server/src/main/java/server/haengdong/application/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,72 @@ | ||
package server.haengdong.application; | ||
|
||
import static software.amazon.awssdk.core.sync.RequestBody.fromInputStream; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
import java.util.UUID; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.multipart.MultipartFile; | ||
import server.haengdong.application.response.ImageNameAppResponse; | ||
import server.haengdong.exception.HaengdongErrorCode; | ||
import server.haengdong.exception.HaengdongException; | ||
import software.amazon.awssdk.services.s3.S3Client; | ||
import software.amazon.awssdk.services.s3.model.DeleteObjectRequest; | ||
import software.amazon.awssdk.services.s3.model.PutObjectRequest; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Service | ||
public class ImageService { | ||
|
||
@Value("${image.bucket}") | ||
private String bucketName; | ||
|
||
@Value("${image.directory}") | ||
private String directoryPath; | ||
|
||
private final S3Client s3Client; | ||
|
||
public List<String> uploadImages(List<MultipartFile> images) { | ||
return images.stream() | ||
.map(this::uploadImage) | ||
.toList(); | ||
} | ||
|
||
private String uploadImage(MultipartFile image) { | ||
try (InputStream inputStream = image.getInputStream()) { | ||
return uploadImageToStorage(inputStream, image); | ||
} catch (IOException e) { | ||
throw new HaengdongException(HaengdongErrorCode.IMAGE_UPLOAD_FAIL); | ||
} | ||
} | ||
|
||
private String uploadImageToStorage(InputStream inputStream, MultipartFile image) { | ||
String imageName = UUID.randomUUID() + image.getOriginalFilename(); | ||
String key = directoryPath + imageName; | ||
long contentLength = image.getSize(); | ||
|
||
PutObjectRequest putObjectRequest = PutObjectRequest.builder() | ||
.bucket(bucketName) | ||
.key(key) | ||
.contentLength(contentLength) | ||
.contentType(image.getContentType()) | ||
.build(); | ||
|
||
s3Client.putObject(putObjectRequest, fromInputStream(inputStream, contentLength)); | ||
return imageName; | ||
} | ||
|
||
public void deleteImage(String imageName) { | ||
DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder() | ||
.bucket(bucketName) | ||
.key(directoryPath + imageName) | ||
.build(); | ||
|
||
s3Client.deleteObject(deleteObjectRequest); | ||
} | ||
} |
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
7 changes: 7 additions & 0 deletions
7
server/src/main/java/server/haengdong/application/response/EventImageAppResponse.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 server.haengdong.application.response; | ||
|
||
public record EventImageAppResponse( | ||
Long id, | ||
String url | ||
) { | ||
} |
11 changes: 11 additions & 0 deletions
11
server/src/main/java/server/haengdong/application/response/ImageNameAppResponse.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 server.haengdong.application.response; | ||
|
||
import server.haengdong.domain.event.Event; | ||
import server.haengdong.domain.event.EventImage; | ||
|
||
public record ImageNameAppResponse(String name) { | ||
|
||
public EventImage toEventImage(Event event) { | ||
return new EventImage(event, name); | ||
} | ||
} |
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
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
17 changes: 17 additions & 0 deletions
17
server/src/main/java/server/haengdong/config/S3Config.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,17 @@ | ||
package server.haengdong.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.services.s3.S3Client; | ||
|
||
@Configuration | ||
public class S3Config { | ||
|
||
@Bean | ||
public S3Client s3Client() { | ||
return S3Client.builder() | ||
.region(Region.AP_NORTHEAST_2) | ||
.build(); | ||
} | ||
} |
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
server/src/main/java/server/haengdong/domain/event/EventImageRepository.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 server.haengdong.domain.event; | ||
|
||
import java.util.List; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface EventImageRepository extends JpaRepository<EventImage, Long> { | ||
|
||
List<EventImage> findAllByEvent(Event event); | ||
} |
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.