forked from SteamWar/SteamWar
Format code
This commit is contained in:
@@ -72,25 +72,28 @@ val nbt = Nbt()
|
||||
fun Route.configureSchematic() {
|
||||
route("/download/{code}") {
|
||||
get {
|
||||
val node = call.receiveSchematic() ?: return@get
|
||||
val node = call.receiveSchematic()
|
||||
?: return@get
|
||||
|
||||
val user = call.principal<SWAuthPrincipal>()?.user
|
||||
if(user != null && !node.accessibleByUser(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.getLatest(node) ?: run {
|
||||
call.respond(HttpStatusCode.InternalServerError)
|
||||
return@get
|
||||
}
|
||||
val data = NodeData.getLatest(node)
|
||||
?: run {
|
||||
call.respond(HttpStatusCode.InternalServerError)
|
||||
return@get
|
||||
}
|
||||
|
||||
call.response.header("Content-Disposition", "attachment; filename=\"${node.name}${data.nodeFormat.fileEnding}\"")
|
||||
call.respondBytes(data.schemData(false).readAllBytes(), contentType = ContentType.Application.OctetStream, status = HttpStatusCode.OK)
|
||||
}
|
||||
get("/info") {
|
||||
val node = call.receiveSchematic() ?: return@get
|
||||
val node = call.receiveSchematic()
|
||||
?: return@get
|
||||
|
||||
call.respond(ResponseSchematic(node))
|
||||
}
|
||||
@@ -103,18 +106,22 @@ fun Route.configureSchematic() {
|
||||
val schemName = file.name.substringBeforeLast(".")
|
||||
|
||||
if (SchematicNode.invalidSchemName(arrayOf(schemName))) {
|
||||
call.respond(HttpStatusCode.BadRequest, ResponseError(
|
||||
error = "INVALID_NAME"
|
||||
))
|
||||
call.respond(
|
||||
HttpStatusCode.BadRequest, ResponseError(
|
||||
error = "INVALID_NAME"
|
||||
)
|
||||
)
|
||||
return@post
|
||||
}
|
||||
|
||||
val schemType = file.name.substringAfterLast(".")
|
||||
|
||||
if (schemType != "schem" && schemType != "schematic") {
|
||||
call.respond(HttpStatusCode.BadRequest, ResponseError(
|
||||
error = "INVALID_SUFFIX"
|
||||
))
|
||||
call.respond(
|
||||
HttpStatusCode.BadRequest, ResponseError(
|
||||
error = "INVALID_SUFFIX"
|
||||
)
|
||||
)
|
||||
return@post
|
||||
}
|
||||
|
||||
@@ -149,39 +156,47 @@ fun Route.configureSchematic() {
|
||||
.value
|
||||
|
||||
if (fawe.equals("2.12.3-SNAPSHOT")) {
|
||||
SWException.log("Schematic with Bugged Version Uploaded", """
|
||||
SWException.log(
|
||||
"Schematic with Bugged Version Uploaded", """
|
||||
Schematic=$schemName
|
||||
User=${user.userName}
|
||||
Id=${user.id}
|
||||
""".trimIndent())
|
||||
""".trimIndent()
|
||||
)
|
||||
}
|
||||
} catch (_: Exception) {}
|
||||
} catch (_: Exception) {
|
||||
}
|
||||
}
|
||||
|
||||
NodeData.saveFromStream(node, content.inputStream(), version)
|
||||
|
||||
call.respond(ResponseSchematic(node))
|
||||
} catch (e: Exception) {
|
||||
call.respond(HttpStatusCode.BadRequest, ResponseError(
|
||||
error = e.message ?: "GENERIC", code = "UPLOAD_ERROR"
|
||||
))
|
||||
call.respond(
|
||||
HttpStatusCode.BadRequest, ResponseError(
|
||||
error = e.message
|
||||
?: "GENERIC", code = "UPLOAD_ERROR"
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun ApplicationCall.receiveSchematic(fieldName: String = "code", delete: Boolean = false): SchematicNode? {
|
||||
val code = parameters[fieldName] ?: run {
|
||||
respond(HttpStatusCode.BadRequest)
|
||||
return null
|
||||
}
|
||||
val code = parameters[fieldName]
|
||||
?: run {
|
||||
respond(HttpStatusCode.BadRequest)
|
||||
return null
|
||||
}
|
||||
|
||||
val dl = NodeDownload.get(code) ?: run {
|
||||
respond(HttpStatusCode.NotFound)
|
||||
return null
|
||||
}
|
||||
val dl = NodeDownload.get(code)
|
||||
?: run {
|
||||
respond(HttpStatusCode.NotFound)
|
||||
return null
|
||||
}
|
||||
|
||||
if(dl.timestamp.toInstant().plus(Duration.of(5, ChronoUnit.MINUTES)).isBefore(Instant.now())) {
|
||||
if (dl.timestamp.toInstant().plus(Duration.of(5, ChronoUnit.MINUTES)).isBefore(Instant.now())) {
|
||||
respond(HttpStatusCode.Gone)
|
||||
return null
|
||||
}
|
||||
@@ -190,10 +205,11 @@ suspend fun ApplicationCall.receiveSchematic(fieldName: String = "code", delete:
|
||||
dl.delete()
|
||||
}
|
||||
|
||||
val node = SchematicNode.getSchematicNode(dl.nodeId) ?: run {
|
||||
respond(HttpStatusCode.NotFound)
|
||||
return null
|
||||
}
|
||||
val node = SchematicNode.getSchematicNode(dl.nodeId)
|
||||
?: run {
|
||||
respond(HttpStatusCode.NotFound)
|
||||
return null
|
||||
}
|
||||
|
||||
return node
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user