Skip to content

Commit

Permalink
feat(CommonController): add caching to random image fetch with 6 hour…
Browse files Browse the repository at this point in the history
…s duration

- Support caching for /random route with specific parameters
- Extend get request for random image to include optional 'id'
- Implement new response types for image bytes and redirect
- Modify controller to return ImageFileDTO for image handling
  • Loading branch information
ShiinaKin committed Oct 20, 2024
1 parent f15bcaa commit 15bc866
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions app/src/main/kotlin/io/sakurasou/controller/CommonController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ import io.sakurasou.exception.controller.param.WrongParameterException
import io.sakurasou.extension.success
import io.sakurasou.model.dto.ImageFileDTO
import io.sakurasou.service.common.CommonService
import kotlin.time.Duration.Companion.hours

/**
* @author Shiina Kin
* 2024/9/9 09:03
*/
fun Route.commonRoute(commonService: CommonService) {
val commonController = CommonController(commonService)
cacheOutput(6.hours, listOf("id"), true) { route("random") { randomFetchImage(commonController) } }
cacheOutput { route("s") { anonymousGetImage(commonController) } }
}

Expand All @@ -41,22 +43,26 @@ fun Route.siteInitRoute() {
}

private fun Route.randomFetchImage(commonController: CommonController) {
get("random", {
get({
description = "return random image if setting allow"
request {
queryParameter<String>("id") {
description = "identify resources"
required = false
}
}
response {
HttpStatusCode.OK to {
description = "strategy local: direct, S3: redirect"
}
HttpStatusCode.Forbidden to {
description = "setting not allow"
description = "LOCAL: byte array"
}
HttpStatusCode.NotFound to {
description = "no image found"
HttpStatusCode.Found to {
description = "S3: redirect"
}
}
}) {
commonController.handleRandomFetchImage()

val fileDTO = commonController.handleRandomFetchImage()
if (fileDTO.bytes != null) call.respondBytes(fileDTO.bytes, ContentType.Image.Any)
else call.respondRedirect(fileDTO.url!!)
}
}

Expand Down Expand Up @@ -92,8 +98,9 @@ class CommonController(
commonService.initSite(siteInitRequest)
}

suspend fun handleRandomFetchImage() {

suspend fun handleRandomFetchImage(): ImageFileDTO {
val imageFileDTO = commonService.fetchRandomImage()
return imageFileDTO
}

suspend fun handleFetchImage(imageUniqueName: String): ImageFileDTO {
Expand Down

0 comments on commit 15bc866

Please sign in to comment.