From 7a559c07358ed49fa851dc35fe1311bcd90c77c9 Mon Sep 17 00:00:00 2001 From: WhitePiano Date: Tue, 6 Feb 2024 19:39:35 +0900 Subject: [PATCH] [Fix] Local Image Store (#63) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: `PhotoLocalManager`가 로컬 Server를 base로 image path 생성 * fix: Spring이 static resources를 `local.image.root`에서 serving --- .../gdsc/plantory/common/config/WebMvcConfiguration.kt | 10 ++++++++++ .../plantory/common/support/photo/PhotoLocalManager.kt | 3 +++ .../presentation/dto/CompanionPlantCreateRequest.kt | 4 +--- .../kotlin/gdsc/plantory/plant/service/PlantService.kt | 4 +--- src/main/resources/application.yml | 1 + src/test/resources/application.yml | 1 + 6 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/gdsc/plantory/common/config/WebMvcConfiguration.kt b/src/main/kotlin/gdsc/plantory/common/config/WebMvcConfiguration.kt index 006c7f9..97eb416 100644 --- a/src/main/kotlin/gdsc/plantory/common/config/WebMvcConfiguration.kt +++ b/src/main/kotlin/gdsc/plantory/common/config/WebMvcConfiguration.kt @@ -1,16 +1,26 @@ package gdsc.plantory.common.config import gdsc.plantory.common.support.DeviceUserResolver +import org.springframework.beans.factory.annotation.Value import org.springframework.context.annotation.Configuration import org.springframework.web.method.support.HandlerMethodArgumentResolver import org.springframework.web.servlet.config.annotation.CorsRegistry +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry import org.springframework.web.servlet.config.annotation.WebMvcConfigurer @Configuration class WebMvcConfiguration( + @Value("\${local.image.root}") + val imageRoot: String, + val deviceUserResolver: DeviceUserResolver, ) : WebMvcConfigurer { + override fun addResourceHandlers(registry: ResourceHandlerRegistry) { + registry.addResourceHandler("/**") + .addResourceLocations("file:${imageRoot}/") + } + override fun addArgumentResolvers(resolvers: MutableList) { resolvers.add(deviceUserResolver) } diff --git a/src/main/kotlin/gdsc/plantory/common/support/photo/PhotoLocalManager.kt b/src/main/kotlin/gdsc/plantory/common/support/photo/PhotoLocalManager.kt index 286b17f..27a29e1 100644 --- a/src/main/kotlin/gdsc/plantory/common/support/photo/PhotoLocalManager.kt +++ b/src/main/kotlin/gdsc/plantory/common/support/photo/PhotoLocalManager.kt @@ -10,6 +10,8 @@ private const val SLASH = "/" @Component class PhotoLocalManager( + @Value("\${local.url}") + private val serverURL: String, @Value("\${local.image.root}") private val localPath: String, @Value("\${companionPlant.image.directory}") @@ -30,6 +32,7 @@ class PhotoLocalManager( uploadFileInLocal(multipartFile, uploadPath) StringBuilder() + .append(serverURL) .append(SLASH) .append(workingDirectory) .append(SLASH) diff --git a/src/main/kotlin/gdsc/plantory/plant/presentation/dto/CompanionPlantCreateRequest.kt b/src/main/kotlin/gdsc/plantory/plant/presentation/dto/CompanionPlantCreateRequest.kt index bfff546..c5a360e 100644 --- a/src/main/kotlin/gdsc/plantory/plant/presentation/dto/CompanionPlantCreateRequest.kt +++ b/src/main/kotlin/gdsc/plantory/plant/presentation/dto/CompanionPlantCreateRequest.kt @@ -18,10 +18,8 @@ data class CompanionPlantCreateRequest( @DateTimeFormat(pattern = "yyyy-MM-dd") val lastWaterDate: LocalDate, ) { fun toEntity(imagePath: String, memberId: Long, waterCycle: Int, plantInformationId: Long): CompanionPlant { - // TODO : Cloud 환경으로 이전 후 제거, 로컬 사진 저장 테스트 용도 - val baseUrl = "https://nongsaro.go.kr/" return CompanionPlant( - _imageUrl = baseUrl + imagePath, + _imageUrl = imagePath, _shortDescription = this.shortDescription, _nickname = this.nickname, nextWaterDate = this.lastWaterDate.plusDays(waterCycle.toLong()), diff --git a/src/main/kotlin/gdsc/plantory/plant/service/PlantService.kt b/src/main/kotlin/gdsc/plantory/plant/service/PlantService.kt index 2c84d8b..616a265 100644 --- a/src/main/kotlin/gdsc/plantory/plant/service/PlantService.kt +++ b/src/main/kotlin/gdsc/plantory/plant/service/PlantService.kt @@ -90,9 +90,7 @@ class PlantService( companionPlantRepository.findByIdAndMemberIdOrThrow(companionPlantId, findMember.getId) val imagePath: String = saveImageAndGetPath(image, findCompanionPlant.getImageUrl) - // TODO : Cloud 환경으로 이전 후 제거, 로컬 사진 저장 테스트 용도 - val baseUrl = "https://nongsaro.go.kr/" - findCompanionPlant.saveRecord(request.comment, baseUrl + imagePath) + findCompanionPlant.saveRecord(request.comment, imagePath) } @Transactional(readOnly = true) diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 37017cc..4ad9eea 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -25,6 +25,7 @@ spring: max-request-size: 25MB local: + url: ${SERVER_URL} image: root: resources diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index 0665b37..c1b7536 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -20,6 +20,7 @@ spring: max-request-size: 25MB local: + url: http://127.0.0.1:8080 image: root: src/test/resources