Skip to content

Commit

Permalink
refactor: UrlConverter 에게 url 변환 로직 위임
Browse files Browse the repository at this point in the history
  • Loading branch information
Combi153 committed Apr 26, 2024
1 parent 3ea1387 commit 65123db
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ 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,

@Value("\${image.common.domain}")
private val domain: String,
) {

fun upload(path: String, image: MultipartFile): String {
Expand All @@ -25,7 +23,6 @@ class ImageStorageService(
amazonS3.putObject(bucket, path, image.inputStream, metadata)

val storedUrl = amazonS3.getUrl(bucket, path).toString()
val pathIndex = storedUrl.indexOf("/$path")
return "$domain${storedUrl.substring(pathIndex)}" // cloudfront 로 우회해 이미지에 접근할 수 있는 URL
return urlConverter.convertToAccessibleUrl(path, storedUrl)
}
}
26 changes: 26 additions & 0 deletions src/main/kotlin/com/petqua/application/image/UrlConverter.kt
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"
}
}
18 changes: 18 additions & 0 deletions src/test/kotlin/com/petqua/application/image/UrlConverterTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.petqua.application.image

import io.kotest.core.spec.style.StringSpec
import io.kotest.matchers.shouldBe

class UrlConverterTest : StringSpec({

"파일 경로와 파일이 저장된 url 을 입력해 접근 가능한 url로 변환한다" {
val domainUrl = "https://domain.com"
val filePath = "root/directory/image.jpeg"
val storedUrl = "https://storedUrl.com/root/directory/image.jpeg"
val urlConverter = UrlConverter(domainUrl)

val accessibleUrl = urlConverter.convertToAccessibleUrl(filePath, storedUrl)

accessibleUrl shouldBe "https://domain.com/root/directory/image.jpeg"
}
})

0 comments on commit 65123db

Please sign in to comment.