Skip to content
This repository has been archived by the owner on Mar 28, 2024. It is now read-only.

Commit

Permalink
Merge pull request #178 from Z-Jais/master
Browse files Browse the repository at this point in the history
Update dependencies and add caching images
  • Loading branch information
Ziedelth authored Oct 9, 2023
2 parents 671115c + 393ee11 commit 2321391
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<description>api</description>

<properties>
<ktor_version>2.3.4</ktor_version>
<ktor_version>2.3.5</ktor_version>
<kotlin.code.style>official</kotlin.code.style>
<kotlin.version>1.9.10</kotlin.version>
<kotlin.compiler.jvmTarget>17</kotlin.compiler.jvmTarget>
Expand Down Expand Up @@ -102,6 +102,11 @@
<artifactId>ktor-server-netty-jvm</artifactId>
<version>${ktor_version}</version>
</dependency>
<dependency>
<groupId>io.ktor</groupId>
<artifactId>ktor-server-caching-headers-jvm</artifactId>
<version>${ktor_version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
Expand Down
44 changes: 26 additions & 18 deletions src/main/kotlin/fr/ziedelth/controllers/AttachmentController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -12,28 +14,34 @@ import java.util.*
@APIIgnore
open class AttachmentController<T : Serializable>(override val prefix: String) : AbstractController<T>(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))
}
}
}
}

0 comments on commit 2321391

Please sign in to comment.