Skip to content

Commit

Permalink
refactor(CommonController): anonymousGetImage
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiinaKin committed Oct 16, 2024
1 parent 5b3fcb1 commit ab77827
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions app/src/main/kotlin/io/sakurasou/controller/CommonController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import io.github.smiley4.ktorswaggerui.dsl.routing.route
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.sakurasou.config.InstanceCenter.commonService
import io.sakurasou.controller.request.SiteInitRequest
import io.sakurasou.exception.controller.param.WrongParameterException
import io.sakurasou.extension.success
import io.sakurasou.model.dto.ImageFileDTO
import io.sakurasou.service.common.CommonService

/**
Expand All @@ -19,14 +22,7 @@ import io.sakurasou.service.common.CommonService
*/
fun Route.commonRoute(commonService: CommonService) {
val commonController = CommonController(commonService)
cacheOutput {
route("fetch") {
randomFetchImage(commonController)
}
route("s") {
anonymousGetImage(commonController)
}
}
cacheOutput { route("s") { anonymousGetImage(commonController) } }
}

fun Route.siteInitRoute() {
Expand Down Expand Up @@ -65,23 +61,27 @@ private fun Route.randomFetchImage(commonController: CommonController) {
}

private fun Route.anonymousGetImage(commonController: CommonController) {
get("{imageId}", {
get("{imageUniqueId}", {
request {
pathParameter<String>("imageId") {
pathParameter<String>("imageUniqueId") {
required = true
}
}
response {
HttpStatusCode.OK to {
description = "strategy local: direct, S3: redirect"
description = "LOCAL: byte array"
}
HttpStatusCode.NotFound to {
description = "image not found"
HttpStatusCode.Found to {
description = "S3: redirect"
}
}
}) {
commonController.handleGetImage()
val imageUniqueName = call.parameters["imageUniqueId"]
if (imageUniqueName == null || imageUniqueName.length != 32) throw WrongParameterException()

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

Expand All @@ -96,7 +96,8 @@ class CommonController(

}

suspend fun handleGetImage() {

suspend fun handleFetchImage(imageUniqueName: String): ImageFileDTO {
val imageFileDTO = commonService.fetchImage(imageUniqueName)
return imageFileDTO
}
}

0 comments on commit ab77827

Please sign in to comment.