Skip to content

Commit

Permalink
refactor: 이름 생성 공통 로직 ObjectPathGenerator로 통일
Browse files Browse the repository at this point in the history
  • Loading branch information
belljun3395 committed Jul 1, 2024
1 parent af75f56 commit 133166d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.few.api.domain.admin.document.dto.ConvertContentUseCaseIn
import com.few.api.domain.admin.document.dto.ConvertContentUseCaseOut
import com.few.api.domain.admin.document.service.GetUrlService
import com.few.api.domain.admin.document.service.dto.GetUrlQuery
import com.few.api.domain.admin.document.utils.ObjectPathGenerator

import com.few.api.repo.dao.document.DocumentDao
import com.few.api.repo.dao.document.command.InsertDocumentIfoCommand
Expand All @@ -12,8 +13,6 @@ import com.few.storage.document.service.PutDocumentService
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import java.io.File
import java.time.LocalDate
import kotlin.random.Random

@Component
class ConvertContentUseCase(
Expand All @@ -24,7 +23,6 @@ class ConvertContentUseCase(
) {
@Transactional
fun execute(useCaseIn: ConvertContentUseCaseIn): ConvertContentUseCaseOut {
val dateDir = LocalDate.now().toString()
val contentSource = useCaseIn.content

val documentSuffix = contentSource.originalFilename?.substringAfterLast(".") ?: "md"
Expand All @@ -33,7 +31,7 @@ class ConvertContentUseCase(
}.onSuccess {
contentSource.transferTo(it)
}.getOrThrow()
val documentName = "documents/$dateDir/${generateRandomName()}" + ".$documentSuffix"
val documentName = ObjectPathGenerator.documentPath(documentSuffix)

val originDownloadUrl = putDocumentService.execute(documentName, document)?.let { res ->
val source = res.`object`
Expand All @@ -55,15 +53,4 @@ class ConvertContentUseCase(

return ConvertContentUseCaseOut(html.replace("\n", "<br>"), originDownloadUrl)
}

private fun generateRandomName(): String {
return randomString()
}

private fun randomString(): String {
val charPool: List<Char> = ('a'..'z') + ('A'..'Z') + ('0'..'9')
return (1..16)
.map { Random.nextInt(0, charPool.size).let { charPool[it] } }
.joinToString("")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ import com.few.api.domain.admin.document.dto.PutImageUseCaseIn
import com.few.api.domain.admin.document.dto.PutImageUseCaseOut
import com.few.api.domain.admin.document.service.GetUrlService
import com.few.api.domain.admin.document.service.dto.GetUrlQuery
import com.few.api.domain.admin.document.utils.ObjectPathGenerator
import com.few.api.repo.dao.image.ImageDao
import com.few.api.repo.dao.image.command.InsertImageIfoCommand
import com.few.storage.image.service.PutImageService
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional
import java.io.File
import java.time.LocalDate
import kotlin.random.Random

@Component
class PutImageUseCase(
Expand All @@ -30,8 +29,7 @@ class PutImageUseCase(
}.onSuccess {
imageSource.transferTo(it)
}.getOrThrow()
val dateDir = LocalDate.now().toString()
val imageName = "images/$dateDir/${generateImageName()}" + ".$suffix"
val imageName = ObjectPathGenerator.imagePath(suffix)

val url = putImageService.execute(imageName, image)?.let { res ->
val source = res.`object`
Expand All @@ -47,15 +45,4 @@ class PutImageUseCase(

return PutImageUseCaseOut(url!!)
}

private fun generateImageName(): String {
return randomString()
}

private fun randomString(): String {
val charPool: List<Char> = ('a'..'z') + ('A'..'Z') + ('0'..'9')
return (1..16)
.map { Random.nextInt(0, charPool.size).let { charPool[it] } }
.joinToString("")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.few.api.domain.admin.document.utils

import java.time.LocalDate
import kotlin.random.Random

object ObjectPathGenerator {

fun imagePath(suffix: String): String {
val dateDir = LocalDate.now().toString()
return "images/$dateDir/${generateImageName()}" + ".$suffix"
}

fun documentPath(suffix: String): String {
val dateDir = LocalDate.now().toString()
return "documents/$dateDir/${generateImageName()}" + ".$suffix"
}

private fun generateImageName(): String {
return randomString()
}

private fun randomString(): String {
val charPool: List<Char> = ('a'..'z') + ('A'..'Z') + ('0'..'9')
return (1..16)
.map { Random.nextInt(0, charPool.size).let { charPool[it] } }
.joinToString("")
}
}

0 comments on commit 133166d

Please sign in to comment.