Refactor GET handling
This commit is contained in:
parent
fbfec69e08
commit
f16fea1f49
@ -48,10 +48,10 @@ object Routes {
|
|||||||
call.respond(HttpStatusCode.OK, AboutPage.content)
|
call.respond(HttpStatusCode.OK, AboutPage.content)
|
||||||
}
|
}
|
||||||
get<HtmlPasteRequest> { req ->
|
get<HtmlPasteRequest> { req ->
|
||||||
call.handleGet(req)
|
call.handleGet(req, raw = false)
|
||||||
}
|
}
|
||||||
get<RawPasteRequest> { req ->
|
get<RawPasteRequest> { req ->
|
||||||
call.handleRaw(req)
|
call.handleGet(req, raw = true)
|
||||||
}
|
}
|
||||||
head("/") {
|
head("/") {
|
||||||
call.respond(HttpStatusCode.OK)
|
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?> {
|
private fun splitPath(uri: String): Pair<String, String?> {
|
||||||
return if (uri.contains('.')) {
|
return if (uri.contains('.')) {
|
||||||
val (name, ext) = uri.split(".", limit = 2)
|
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
|
@ExperimentalStdlibApi
|
||||||
private suspend fun ApplicationCall.handlePost() {
|
private suspend fun ApplicationCall.handlePost() {
|
||||||
receiveMultipart().forEachPart { part ->
|
receiveMultipart().forEachPart { part ->
|
||||||
when (part) {
|
when (part) {
|
||||||
is PartData.FileItem -> {
|
is PartData.FileItem -> {
|
||||||
val content = part.streamProvider().use { it.readBytes().decodeToString() }
|
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")
|
Log.info("Saving new file paste with uri $uri")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
is PartData.FormItem -> {
|
is PartData.FormItem -> {
|
||||||
val content = part.value
|
val content = part.value
|
||||||
respondToUpload(content)?.let { uri ->
|
processUpload(content)?.let { uri ->
|
||||||
Log.info("Saving new text paste with uri $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 {
|
content.ifBlank {
|
||||||
Log.info("Rejecting blank paste")
|
Log.info("Rejecting blank paste")
|
||||||
respond(HttpStatusCode.BadRequest, "Empty pastes are not allowed")
|
respond(HttpStatusCode.BadRequest, "Empty pastes are not allowed")
|
||||||
@ -125,22 +125,15 @@ object Routes {
|
|||||||
return uri
|
return uri
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun ApplicationCall.handleGet(req: PasteRequest) {
|
private suspend fun ApplicationCall.handleGet(req: PasteRequest, raw: Boolean) {
|
||||||
val (uri, ext) = splitPath(req.uri)
|
val (uri, ext) = splitPath(req.uri)
|
||||||
Log.info("Retrieving paste $uri")
|
Log.info("Retrieving paste $uri")
|
||||||
PasteDao.selectByUri(uri)?.data?.let { paste ->
|
PasteDao.selectByUri(uri)?.data?.let { paste ->
|
||||||
respond(
|
if (raw) {
|
||||||
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)
|
respondText(paste.content, ContentType.Text.Plain)
|
||||||
|
} else {
|
||||||
|
respond(HttpStatusCode.OK, PastePage.build(paste.content, ext))
|
||||||
|
}
|
||||||
} ?: respond(HttpStatusCode.NotFound, "nothing found for id $uri")
|
} ?: respond(HttpStatusCode.NotFound, "nothing found for id $uri")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@ import io.ktor.http.HttpStatusCode
|
|||||||
import kotlinx.html.*
|
import kotlinx.html.*
|
||||||
|
|
||||||
object PastePage {
|
object PastePage {
|
||||||
fun build(content: String, type: String?) = HtmlContent(HttpStatusCode.OK) {
|
fun build(content: String, type: String?) = HtmlContent {
|
||||||
head {
|
head {
|
||||||
link(rel = "stylesheet", href = "/style.css", type = "text/css")
|
link(rel = "stylesheet", href = "/style.css", type = "text/css")
|
||||||
link(
|
link(
|
||||||
|
Loading…
Reference in New Issue
Block a user