diff --git a/app/src/main/kotlin/io/sakurasou/controller/CommonController.kt b/app/src/main/kotlin/io/sakurasou/controller/CommonController.kt index 259cddb1..b28077a4 100644 --- a/app/src/main/kotlin/io/sakurasou/controller/CommonController.kt +++ b/app/src/main/kotlin/io/sakurasou/controller/CommonController.kt @@ -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 /** @@ -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() { @@ -65,23 +61,27 @@ private fun Route.randomFetchImage(commonController: CommonController) { } private fun Route.anonymousGetImage(commonController: CommonController) { - get("{imageId}", { + get("{imageUniqueId}", { request { - pathParameter("imageId") { + pathParameter("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!!) } } @@ -96,7 +96,8 @@ class CommonController( } - suspend fun handleGetImage() { - + suspend fun handleFetchImage(imageUniqueName: String): ImageFileDTO { + val imageFileDTO = commonService.fetchImage(imageUniqueName) + return imageFileDTO } } \ No newline at end of file