From f16fea1f498f7e0c7e618ad76edaeb751bcaaa1b Mon Sep 17 00:00:00 2001 From: kageru Date: Sun, 29 Sep 2019 17:10:02 +0200 Subject: [PATCH] Refactor GET handling --- .../kotlin/moe/kageru/kodeshare/Routes.kt | 47 ++++++++----------- .../moe/kageru/kodeshare/pages/PastePage.kt | 2 +- 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/src/main/kotlin/moe/kageru/kodeshare/Routes.kt b/src/main/kotlin/moe/kageru/kodeshare/Routes.kt index 98fff7d..5a3eed8 100644 --- a/src/main/kotlin/moe/kageru/kodeshare/Routes.kt +++ b/src/main/kotlin/moe/kageru/kodeshare/Routes.kt @@ -48,10 +48,10 @@ object Routes { call.respond(HttpStatusCode.OK, AboutPage.content) } get { req -> - call.handleGet(req) + call.handleGet(req, raw = false) } get { req -> - call.handleRaw(req) + call.handleGet(req, raw = true) } head("/") { call.respond(HttpStatusCode.OK) @@ -64,15 +64,6 @@ object Routes { } } - private suspend fun ApplicationCall.handleHead(paste: PasteRequest, raw: Boolean = false) { - val uri = splitPath(paste.uri).first - if (PasteDao.selectByUri(uri) != null) { - respond(HttpStatusCode.OK, if (raw) ContentType.Text.Plain else ContentType.Text.Html) - } else { - respond(HttpStatusCode.NotFound) - } - } - private fun splitPath(uri: String): Pair { return if (uri.contains('.')) { val (name, ext) = uri.split(".", limit = 2) @@ -82,19 +73,28 @@ object Routes { } } + private suspend fun ApplicationCall.handleHead(paste: PasteRequest, raw: Boolean = false) { + val uri = splitPath(paste.uri).first + if (PasteDao.selectByUri(uri) != null) { + respond(HttpStatusCode.OK, if (raw) ContentType.Text.Plain else ContentType.Text.Html) + } else { + respond(HttpStatusCode.NotFound) + } + } + @ExperimentalStdlibApi private suspend fun ApplicationCall.handlePost() { receiveMultipart().forEachPart { part -> when (part) { is PartData.FileItem -> { val content = part.streamProvider().use { it.readBytes().decodeToString() } - respondToUpload(content)?.let { uri -> + processUpload(content)?.let { uri -> Log.info("Saving new file paste with uri $uri") } } is PartData.FormItem -> { val content = part.value - respondToUpload(content)?.let { uri -> + processUpload(content)?.let { uri -> Log.info("Saving new text paste with uri $uri") } } @@ -105,7 +105,7 @@ object Routes { } } - private suspend fun ApplicationCall.respondToUpload(content: String): String? { + private suspend fun ApplicationCall.processUpload(content: String): String? { content.ifBlank { Log.info("Rejecting blank paste") respond(HttpStatusCode.BadRequest, "Empty pastes are not allowed") @@ -125,22 +125,15 @@ object Routes { return uri } - private suspend fun ApplicationCall.handleGet(req: PasteRequest) { + private suspend fun ApplicationCall.handleGet(req: PasteRequest, raw: Boolean) { val (uri, ext) = splitPath(req.uri) Log.info("Retrieving paste $uri") PasteDao.selectByUri(uri)?.data?.let { paste -> - respond( - HttpStatusCode.OK, - PastePage.build(paste.content, ext) - ) - } ?: respond(HttpStatusCode.NotFound, "nothing found for id $uri") - } - - private suspend fun ApplicationCall.handleRaw(req: RawPasteRequest) { - val uri = req.uri - Log.info("Retrieving raw paste $uri") - PasteDao.selectByUri(uri)?.data?.let { paste -> - respondText(paste.content, ContentType.Text.Plain) + if (raw) { + respondText(paste.content, ContentType.Text.Plain) + } else { + respond(HttpStatusCode.OK, PastePage.build(paste.content, ext)) + } } ?: respond(HttpStatusCode.NotFound, "nothing found for id $uri") } } diff --git a/src/main/kotlin/moe/kageru/kodeshare/pages/PastePage.kt b/src/main/kotlin/moe/kageru/kodeshare/pages/PastePage.kt index 2fc6e27..ab2ef05 100644 --- a/src/main/kotlin/moe/kageru/kodeshare/pages/PastePage.kt +++ b/src/main/kotlin/moe/kageru/kodeshare/pages/PastePage.kt @@ -5,7 +5,7 @@ import io.ktor.http.HttpStatusCode import kotlinx.html.* object PastePage { - fun build(content: String, type: String?) = HtmlContent(HttpStatusCode.OK) { + fun build(content: String, type: String?) = HtmlContent { head { link(rel = "stylesheet", href = "/style.css", type = "text/css") link(