From 932ad60f0b1a98dc83f328904f92f4940b6c2bab Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Wed, 14 Aug 2024 11:52:05 +0200 Subject: [PATCH] Update Ktor --- build.gradle.kts | 12 +-- gradle/wrapper/gradle-wrapper.properties | 2 +- .../kotlin/de/steamwar/routes/Schematic.kt | 88 ++++++++++++------- 3 files changed, 61 insertions(+), 41 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 76e1476..3797a12 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,11 +1,11 @@ -val ktor_version: String by project -val kotlin_version: String by project -val logback_version: String by project +val ktor_version: String = "2.3.12" +val kotlin_version: String = "2.0.10" +val logback_version: String = "1.5.6" plugins { - kotlin("jvm") version "1.9.22" - id("io.ktor.plugin") version "2.3.7" - id("org.jetbrains.kotlin.plugin.serialization") version "1.9.22" + kotlin("jvm") version "2.0.10" + id("io.ktor.plugin") version "2.3.12" + id("org.jetbrains.kotlin.plugin.serialization") version "2.0.10" application } diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 068cdb2..19cfad9 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,5 +1,5 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.3-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists diff --git a/src/main/kotlin/de/steamwar/routes/Schematic.kt b/src/main/kotlin/de/steamwar/routes/Schematic.kt index 67297aa..8e37bac 100644 --- a/src/main/kotlin/de/steamwar/routes/Schematic.kt +++ b/src/main/kotlin/de/steamwar/routes/Schematic.kt @@ -72,43 +72,63 @@ data class SchematicCode(val id: Int, val code: String, val expires: Long) data class UploadSchematic(val name: String, val content: String) fun Route.configureSchematic() { - get("/download/{code}") { - val code = call.parameters["code"] ?: run { - call.respond(HttpStatusCode.BadRequest) - return@get + route("/download/{code}") { + get { + val code = call.parameters["code"] ?: run { + call.respond(HttpStatusCode.BadRequest) + return@get + } + + val dl = NodeDownload.get(code) ?: run { + call.respond(HttpStatusCode.NotFound) + return@get + } + + dl.delete() + + if(dl.timestamp.toInstant().plus(Duration.of(5, ChronoUnit.MINUTES)).isBefore(Instant.now())) { + call.respond(HttpStatusCode.Gone) + return@get + } + + val node = SchematicNode.getSchematicNode(dl.nodeId) ?: run { + call.respond(HttpStatusCode.NotFound) + return@get + } + + val user = call.principal()?.user + if(user != null && !node.accessibleByUser(user)) { + call.respond(HttpStatusCode.Forbidden) + SWException.log("User ${user.userName} tried to download schematic ${node.name} without permission", user.id.toString()) + return@get + } + + val data = NodeData.get(node) ?: run { + call.respond(HttpStatusCode.InternalServerError) + return@get + } + + call.response.header("Content-Disposition", "attachment; filename=\"${node.name}.${if (data.nodeFormat) "schem" else "schematic"}\"") + call.respondBytes(data.schemData().readAllBytes(), contentType = ContentType.Application.OctetStream, status = HttpStatusCode.OK) } + get("/info") { + val code = call.parameters["code"] ?: run { + call.respond(HttpStatusCode.BadRequest) + return@get + } - val dl = NodeDownload.get(code) ?: run { - call.respond(HttpStatusCode.NotFound) - return@get + val dl = NodeDownload.get(code) ?: run { + call.respond(HttpStatusCode.NotFound) + return@get + } + + val node = SchematicNode.getSchematicNode(dl.nodeId) ?: run { + call.respond(HttpStatusCode.NotFound) + return@get + } + + call.respond(ResponseSchematic(node)) } - - dl.delete() - - if(dl.timestamp.toInstant().plus(Duration.of(5, ChronoUnit.MINUTES)).isBefore(Instant.now())) { - call.respond(HttpStatusCode.Gone) - return@get - } - - val node = SchematicNode.getSchematicNode(dl.nodeId) ?: run { - call.respond(HttpStatusCode.NotFound) - return@get - } - - val user = call.principal()?.user - if(user != null && !node.accessibleByUser(user)) { - call.respond(HttpStatusCode.Forbidden) - SWException.log("User ${user.userName} tried to download schematic ${node.name} without permission", user.id.toString()) - return@get - } - - val data = NodeData.get(node) ?: run { - call.respond(HttpStatusCode.InternalServerError) - return@get - } - - call.response.header("Content-Disposition", "attachment; filename=\"${node.name}.${if (data.nodeFormat) "schem" else "schematic"}\"") - call.respondBytes(data.schemData().readAllBytes(), contentType = ContentType.Application.OctetStream, status = HttpStatusCode.OK) } route("/schem") { install(SWPermissionCheck)