Refactor GET handling

This commit is contained in:
kageru 2019-09-29 17:10:02 +02:00
parent fbfec69e08
commit f16fea1f49
2 changed files with 21 additions and 28 deletions

View File

@ -48,10 +48,10 @@ object Routes {
call.respond(HttpStatusCode.OK, AboutPage.content)
}
get<HtmlPasteRequest> { req ->
call.handleGet(req)
call.handleGet(req, raw = false)
}
get<RawPasteRequest> { 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<String, String?> {
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")
}
}

View File

@ -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(