This repository has been archived by the owner on Mar 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor controllers and repositories for dependency injection
- Loading branch information
Showing
35 changed files
with
478 additions
and
369 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/kotlin/fr/ziedelth/controllers/AttachmentController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package fr.ziedelth.controllers | ||
|
||
import fr.ziedelth.utils.ImageCache | ||
import fr.ziedelth.utils.routes.APIIgnore | ||
import io.ktor.http.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
import java.io.Serializable | ||
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) | ||
} | ||
|
||
val uuid = UUID.fromString(string) | ||
println("GET ${prefix}/attachment/$uuid") | ||
|
||
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)) | ||
} | ||
} | ||
} |
Oops, something went wrong.