Skip to content

Commit

Permalink
refactor: extract thumbnail fetching logic into a separate function i…
Browse files Browse the repository at this point in the history
…n imageService
  • Loading branch information
ShiinaKin committed Dec 18, 2024
1 parent 4887bc1 commit 8e9dd70
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions app/src/main/kotlin/io/sakurasou/service/image/ImageServiceImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import io.sakurasou.model.dao.user.UserDao
import io.sakurasou.model.dto.ImageFileDTO
import io.sakurasou.model.dto.ImageInsertDTO
import io.sakurasou.model.dto.ImageUpdateDTO
import io.sakurasou.model.entity.Image
import io.sakurasou.model.entity.Strategy
import io.sakurasou.model.group.ImageType
import io.sakurasou.model.strategy.LocalStrategy
import io.sakurasou.model.strategy.S3Strategy
Expand Down Expand Up @@ -329,16 +331,7 @@ class ImageServiceImpl(
if (image.userId != userId) throw ImageAccessDeniedException()
val strategy = strategyDao.findStrategyById(image.strategyId) ?: throw StrategyNotFoundException()

when (strategy.config) {
is LocalStrategy -> ImageFileDTO(bytes = ImageUtils.fetchLocalImage(strategy, image.path, true))
is S3Strategy -> ImageFileDTO(url = ImageUtils.fetchS3Image(strategy, image.path, true))
}.also {
if (it.bytes == null && it.url.isNullOrBlank()) {
logger.debug { "thumbnail of image $imageId doesn't exist, will be generate later." }
ImageExecutor.rePersistThumbnail(strategy, image.path)
throw ImageThumbnailNotFoundException()
}
}
fetchThumbnailFile(strategy, image)
}
}

Expand All @@ -359,10 +352,7 @@ class ImageServiceImpl(
val image = imageDao.findImageById(imageId) ?: throw ImageNotFoundException()
val strategy = strategyDao.findStrategyById(image.strategyId) ?: throw StrategyNotFoundException()

when (strategy.config) {
is LocalStrategy -> ImageFileDTO(bytes = ImageUtils.fetchLocalImage(strategy, image.path, true))
is S3Strategy -> ImageFileDTO(url = ImageUtils.fetchS3Image(strategy, image.path, true))
}
fetchThumbnailFile(strategy, image)
}
}

Expand Down Expand Up @@ -402,4 +392,15 @@ class ImageServiceImpl(
)
}
}

private suspend fun fetchThumbnailFile(strategy: Strategy, image: Image) = when (strategy.config) {
is LocalStrategy -> ImageFileDTO(bytes = ImageUtils.fetchLocalImage(strategy, image.path, true))
is S3Strategy -> ImageFileDTO(url = ImageUtils.fetchS3Image(strategy, image.path, true))
}.also {
if (it.bytes == null && it.url.isNullOrBlank()) {
logger.debug { "thumbnail of image ${image.id} doesn't exist, will be generate later." }
ImageExecutor.rePersistThumbnail(strategy, image.path)
throw ImageThumbnailNotFoundException()
}
}
}

0 comments on commit 8e9dd70

Please sign in to comment.