Add Backend to Monorepo

This commit is contained in:
2024-08-18 11:15:54 +02:00
parent b8e50dc139
commit fd7fe8c305
37 changed files with 2703 additions and 26 deletions
@@ -0,0 +1,229 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2024 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.routes
import de.steamwar.plugins.SWAuthPrincipal
import de.steamwar.plugins.SWPermissionCheck
import de.steamwar.sql.NodeData
import de.steamwar.sql.NodeDownload
import de.steamwar.sql.NodeMember
import de.steamwar.sql.SWException
import de.steamwar.sql.SchematicNode
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.auth.*
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.serialization.Serializable
import java.security.MessageDigest
import java.time.Duration
import java.time.Instant
import java.time.temporal.ChronoUnit
import java.util.*
@Serializable
data class ResponseSchematic(val name: String, val id: Int, val type: String?, val owner: Int, val item: String, val lastUpdate: Long, val rank: Int, val replaceColor: Boolean, val allowReplay: Boolean) {
constructor(node: SchematicNode) : this(node.name, node.id, node.schemtype?.name(), node.owner, node.item, node.lastUpdate.time, node.rank, node.replaceColor(), node.allowReplay())
}
@Serializable
data class ResponseSchematicLong(val members: List<ResponseUser>, val path: String, val schem: ResponseSchematic) {
constructor(node: SchematicNode, path: String): this(NodeMember.getNodeMembers(node.id).map { ResponseUser.get(it.member) }, path, ResponseSchematic(node))
}
@Serializable
data class ResponseSchematicList(val breadcrumbs: List<ResponseBreadcrumb>, val schematics: List<ResponseSchematic>, val players: Map<Int, ResponseUser>) {
constructor(schematics: List<ResponseSchematic>, breadcrumbs: List<ResponseBreadcrumb>) : this(breadcrumbs, schematics, schematics.map { it.owner }.distinct().map { ResponseUser.get(it) }.associateBy { it.id })
}
@Serializable
data class ResponseBreadcrumb(val name: String, val id: Int)
fun generateCode(): String {
val md = MessageDigest.getInstance("SHA-256")
val random = ByteArray(64).map { (0..255).random().toByte() }.toByteArray()
val code = md.digest(random)
return code.joinToString("") { "%02x".format(it) }
}
@Serializable
data class SchematicCode(val id: Int, val code: String, val expires: Long)
@Serializable
data class UploadSchematic(val name: String, val content: String)
fun Route.configureSchematic() {
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<SWAuthPrincipal>()?.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 node = SchematicNode.getSchematicNode(dl.nodeId) ?: run {
call.respond(HttpStatusCode.NotFound)
return@get
}
call.respond(ResponseSchematic(node))
}
}
route("/schem") {
install(SWPermissionCheck)
get {
val user = call.principal<SWAuthPrincipal>()!!.user
call.respond(ResponseSchematicList(SchematicNode.list(user, null).filter { it.name != "//copy" }.sortedWith { o1, o2 ->
if (o1.isDir || o2.isDir) {
o2.isDir.compareTo(o1.isDir)
} else {
o1.name.compareTo(o2.name)
}
}.map { ResponseSchematic(it) }, listOf()))
}
post {
val file = call.receive<UploadSchematic>()
val schemName = file.name.substringBeforeLast(".")
val schemType = file.name.substringAfterLast(".")
if (schemType != "schem" && schemType != "schematic") {
call.respond(HttpStatusCode.BadRequest)
return@post
}
val user = call.principal<SWAuthPrincipal>()!!.user
val content = Base64.getDecoder().decode(file.content)
var node = SchematicNode.getSchematicNode(user.id, schemName, 0)
if (node == null) {
node = SchematicNode.createSchematic(user.id, schemName, 0)
}
val data = NodeData(node.id, false)
data.saveFromStream(content.inputStream(), schemType == "schem")
call.respond(ResponseSchematic(node))
}
route("/{id}") {
get {
val user = call.principal<SWAuthPrincipal>()!!.user
val parentId = call.parameters["id"]?.toIntOrNull()
if(parentId == null) {
call.respond(HttpStatusCode.BadRequest)
return@get
}
val parent = SchematicNode.getSchematicNode(parentId)
if(parent == null) {
call.respond(HttpStatusCode.NotFound)
return@get
}
if(!parent.accessibleByUser(user)) {
call.respond(HttpStatusCode.Forbidden)
return@get
}
call.respond(ResponseSchematicLong(parent, parent.generateBreadcrumbs(user)))
}
get("/list") {
val user = call.principal<SWAuthPrincipal>()!!.user
val parentId = call.parameters["id"]?.toIntOrNull()
if(parentId == null) {
call.respond(HttpStatusCode.BadRequest)
return@get
}
val parent = SchematicNode.getSchematicNode(parentId)
if(parent == null) {
call.respond(HttpStatusCode.NotFound)
return@get
}
if(!parent.isDir) {
call.respond(HttpStatusCode.BadRequest)
return@get
}
if(!parent.accessibleByUser(user)) {
call.respond(HttpStatusCode.Forbidden)
return@get
}
call.respond(ResponseSchematicList(SchematicNode.list(user, parent.id).filter { it.name != "//copy" }.sortedWith { o1, o2 ->
if (o1.isDir || o2.isDir) {
o2.isDir.compareTo(o1.isDir)
} else {
o1.name.compareTo(o2.name)
}
}.map { ResponseSchematic(it) }, parent.generateBreadcrumbsMap(user).map { ResponseBreadcrumb(it.key, it.value) }))
}
}
}
}