diff --git a/pom.xml b/pom.xml index 60d33e0..bd92c3b 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ api - 2.3.4 + 2.3.5 official 1.9.10 17 @@ -102,6 +102,11 @@ ktor-server-netty-jvm ${ktor_version} + + io.ktor + ktor-server-caching-headers-jvm + ${ktor_version} + ch.qos.logback logback-classic diff --git a/src/main/kotlin/fr/ziedelth/controllers/AttachmentController.kt b/src/main/kotlin/fr/ziedelth/controllers/AttachmentController.kt index e43d5ec..bf7df8e 100644 --- a/src/main/kotlin/fr/ziedelth/controllers/AttachmentController.kt +++ b/src/main/kotlin/fr/ziedelth/controllers/AttachmentController.kt @@ -3,7 +3,9 @@ package fr.ziedelth.controllers import fr.ziedelth.utils.ImageCache import fr.ziedelth.utils.routes.APIIgnore import io.ktor.http.* +import io.ktor.http.content.* import io.ktor.server.application.* +import io.ktor.server.plugins.cachingheaders.* import io.ktor.server.response.* import io.ktor.server.routing.* import java.io.Serializable @@ -12,28 +14,34 @@ import java.util.* @APIIgnore open class AttachmentController(override val prefix: String) : AbstractController(prefix) { fun Route.attachmentByUUID() { - get("/attachment/{uuid}") { - val string = call.parameters["uuid"]!! - val uuidRegex = - "^[0-9(a-f|A-F)]{8}-[0-9(a-f|A-F)]{4}-4[0-9(a-f|A-F)]{3}-[89ab][0-9(a-f|A-F)]{3}-[0-9(a-f|A-F)]{12}\$".toRegex() - - if (!uuidRegex.matches(string)) { - println("GET $prefix/attachment/$string : Invalid UUID") - return@get call.respond(HttpStatusCode.BadRequest) + route("/attachment/{uuid}") { + install(CachingHeaders) { + options { _, _ -> CachingOptions(CacheControl.MaxAge(maxAgeSeconds = 172800)) } } - val uuid = UUID.fromString(string) - println("GET ${prefix}/attachment/$uuid") + get { + val string = call.parameters["uuid"]!! + val uuidRegex = + "^[0-9(a-f|A-F)]{8}-[0-9(a-f|A-F)]{4}-4[0-9(a-f|A-F)]{3}-[89ab][0-9(a-f|A-F)]{3}-[0-9(a-f|A-F)]{12}\$".toRegex() - if (!ImageCache.contains(uuid)) { - println("Attachment $uuid not found") - call.respond(HttpStatusCode.NoContent) - return@get - } + if (!uuidRegex.matches(string)) { + println("GET $prefix/attachment/$string : Invalid UUID") + return@get call.respond(HttpStatusCode.BadRequest) + } + + val uuid = UUID.fromString(string) + println("GET ${prefix}/attachment/$uuid") - val image = ImageCache.get(uuid)!! - println("Attachment $uuid found (${image.bytes.size} bytes)") - call.respondBytes(image.bytes, ContentType("image", image.type)) + if (!ImageCache.contains(uuid)) { + println("Attachment $uuid not found") + call.respond(HttpStatusCode.NoContent) + return@get + } + + val image = ImageCache.get(uuid)!! + println("Attachment $uuid found (${image.bytes.size} bytes)") + call.respondBytes(image.bytes, ContentType("image", image.type)) + } } } } \ No newline at end of file