-
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.
* build: s3 sdk 추가 * feat: s3 config 추가 * feat: ImageType 추가 * feat: yaml s3 이미지 파일 업로드 관련 설정 추가 * feat: ImageStorageService 추가 * feat: ProductReviewContent 추가 * feat: ProductReviewContent 추가 반영 * feat: ImageType 추가 * test: S3 가짜 객체 추가 * feat: ProductReviewFacadeService 후기 작성 기능 추가 * feat: ProductReviewController 후기 작성 api 추가 * feat: swagger 요청 처리를 위한 converter 추가 * refactor: 사용하지 않는 코드 삭제 * feat: swagger api의 파라미터 설명 추가 * style: 주석 추가 * refactor: 이미지 업로드 시 @ModelAttribute 를 사용하도록 변경 * refactor: ImageType 패키지 및 구현 코드 변경 * refactor: UrlConverter 에게 url 변환 로직 위임 * refactor: 이미지 업로드 시 이름 표기 방식 변경
- Loading branch information
Showing
29 changed files
with
1,853 additions
and
27 deletions.
There are no files selected for viewing
Submodule backend-submodule
updated
from 226f2f to 3ec722
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
28 changes: 28 additions & 0 deletions
28
src/main/kotlin/com/petqua/application/image/ImageStorageService.kt
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,28 @@ | ||
package com.petqua.application.image | ||
|
||
import com.amazonaws.services.s3.AmazonS3 | ||
import com.amazonaws.services.s3.model.ObjectMetadata | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.stereotype.Service | ||
import org.springframework.web.multipart.MultipartFile | ||
|
||
@Service | ||
class ImageStorageService( | ||
private val amazonS3: AmazonS3, | ||
private val urlConverter: UrlConverter, | ||
|
||
@Value("\${cloud.aws.s3.bucket}") | ||
private val bucket: String, | ||
) { | ||
|
||
fun upload(path: String, image: MultipartFile): String { | ||
val metadata = ObjectMetadata() | ||
metadata.contentType = image.contentType | ||
metadata.contentLength = image.size | ||
|
||
amazonS3.putObject(bucket, path, image.inputStream, metadata) | ||
|
||
val storedUrl = amazonS3.getUrl(bucket, path).toString() | ||
return urlConverter.convertToAccessibleUrl(path, storedUrl) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/kotlin/com/petqua/application/image/UrlConverter.kt
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,26 @@ | ||
package com.petqua.application.image | ||
|
||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class UrlConverter( | ||
@Value("\${image.common.domain}") | ||
private val domain: String, | ||
) { | ||
|
||
fun convertToAccessibleUrl(filePath: String, storedUrl: String): String { | ||
/* | ||
* example) | ||
* filePath = "root/directory/image.jpeg" | ||
* storedUrl = "https://storedUrl.com/root/directory/image.jpeg" | ||
* | ||
* pathIndex = 21 | ||
* storedPath = "/root/directory/image.jpeg" | ||
* return "https://domain.com/root/directory/image.jpeg" | ||
* */ | ||
val pathIndex = storedUrl.indexOf("/$filePath") | ||
val storedPath = storedUrl.substring(pathIndex) | ||
return "$domain$storedPath" | ||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
src/main/kotlin/com/petqua/application/product/review/ProductReviewFacadeService.kt
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.petqua.application.product.review | ||
|
||
import com.petqua.application.product.dto.ProductReviewCreateCommand | ||
import com.petqua.application.product.dto.ProductReviewReadQuery | ||
import com.petqua.application.product.dto.ProductReviewStatisticsResponse | ||
import com.petqua.application.product.dto.ProductReviewsResponse | ||
import com.petqua.application.product.dto.UpdateReviewRecommendationCommand | ||
import org.springframework.stereotype.Service | ||
|
||
@Service | ||
class ProductReviewFacadeService( | ||
private val productReviewService: ProductReviewService, | ||
private val productReviewImageUploader: ProductReviewImageUploader, | ||
) { | ||
|
||
fun create(command: ProductReviewCreateCommand): Long { | ||
val productReview = command.toProductReview() | ||
val reviewImageUrls = productReviewImageUploader.uploadAll(command.images) | ||
return productReviewService.create(productReview, reviewImageUrls) | ||
} | ||
|
||
fun readAll(query: ProductReviewReadQuery): ProductReviewsResponse { | ||
return productReviewService.readAll(query) | ||
} | ||
|
||
fun readReviewCountStatistics(productId: Long): ProductReviewStatisticsResponse { | ||
return productReviewService.readReviewCountStatistics(productId) | ||
} | ||
|
||
fun updateReviewRecommendation(command: UpdateReviewRecommendationCommand) { | ||
productReviewService.updateReviewRecommendation(command) | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/kotlin/com/petqua/application/product/review/ProductReviewImageUploader.kt
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,53 @@ | ||
package com.petqua.application.product.review | ||
|
||
import com.petqua.application.image.ImageStorageService | ||
import com.petqua.domain.product.review.ProductReviewImageType | ||
import com.petqua.exception.product.review.ProductReviewException | ||
import com.petqua.exception.product.review.ProductReviewExceptionType.FAILED_REVIEW_IMAGE_UPLOAD | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.multipart.MultipartFile | ||
import java.util.UUID | ||
|
||
@Component | ||
class ProductReviewImageUploader( | ||
private val imageStorageService: ImageStorageService, | ||
|
||
@Value("\${image.product-review.directory}") | ||
private val directory: String, | ||
) { | ||
|
||
fun uploadAll(images: List<MultipartFile>): List<String> { | ||
if (images.isEmpty()) { | ||
return listOf() | ||
} | ||
return images.map { upload(it) } | ||
} | ||
|
||
private fun upload(image: MultipartFile): String { | ||
ProductReviewImageType.validateSupported(image.contentType) | ||
val fileName = UUID.randomUUID() | ||
val path = "$directory$fileName${parseFileExtension(image)}" | ||
|
||
return uploadOrThrow(path, image) | ||
} | ||
|
||
private fun parseFileExtension(image: MultipartFile): String { | ||
return image.originalFilename?.let { | ||
".${it.substringAfterLast(FILE_EXTENSION_DELIMITER)}" | ||
} ?: EMPTY_EXTENSION | ||
} | ||
|
||
private fun uploadOrThrow(path: String, image: MultipartFile): String { | ||
try { | ||
return imageStorageService.upload(path, image) | ||
} catch (e: Exception) { | ||
throw ProductReviewException(FAILED_REVIEW_IMAGE_UPLOAD) | ||
} | ||
} | ||
|
||
companion object { | ||
private const val FILE_EXTENSION_DELIMITER = '.' | ||
private const val EMPTY_EXTENSION = "" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.petqua.common.config | ||
|
||
import com.amazonaws.auth.EC2ContainerCredentialsProviderWrapper | ||
import com.amazonaws.regions.Regions.AP_NORTHEAST_2 | ||
import com.amazonaws.services.s3.AmazonS3 | ||
import com.amazonaws.services.s3.AmazonS3ClientBuilder | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.context.annotation.Profile | ||
|
||
@Configuration | ||
@Profile("!test") | ||
class S3Config { | ||
|
||
@Bean | ||
fun amazonS3(): AmazonS3 { | ||
return AmazonS3ClientBuilder.standard() | ||
.withRegion(AP_NORTHEAST_2) | ||
.withCredentials(EC2ContainerCredentialsProviderWrapper()) | ||
.build() | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/kotlin/com/petqua/common/converter/MultipartJackson2HttpMessageConverter.kt
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,25 @@ | ||
package com.petqua.common.converter | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import org.springframework.http.MediaType | ||
import org.springframework.http.MediaType.APPLICATION_OCTET_STREAM | ||
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter | ||
import org.springframework.stereotype.Component | ||
import java.lang.reflect.Type | ||
|
||
@Component | ||
class MultipartJackson2HttpMessageConverter(objectMapper: ObjectMapper) : | ||
AbstractJackson2HttpMessageConverter(objectMapper, APPLICATION_OCTET_STREAM) { | ||
|
||
override fun canWrite(clazz: Class<*>, mediaType: MediaType?): Boolean { | ||
return false | ||
} | ||
|
||
override fun canWrite(type: Type?, clazz: Class<*>, mediaType: MediaType?): Boolean { | ||
return false | ||
} | ||
|
||
override fun canWrite(mediaType: MediaType?): Boolean { | ||
return false | ||
} | ||
} |
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.