Allow .extension in url

This commit is contained in:
kageru 2019-09-29 17:03:44 +02:00
parent 0f1bd98f49
commit fbfec69e08
2 changed files with 24 additions and 27 deletions

View File

@ -47,37 +47,41 @@ object Routes {
get("/about") {
call.respond(HttpStatusCode.OK, AboutPage.content)
}
get<PasteRequest> { req ->
get<HtmlPasteRequest> { req ->
call.handleGet(req)
}
get<RawPasteRequest> { req ->
call.handleRaw(req)
}
get<TypedPasteRequest> { req ->
call.handleGet(req)
}
head("/") {
call.respond(HttpStatusCode.OK)
}
head<RawPasteRequest> { req ->
call.handleHead(req)
call.handleHead(req, true)
}
head<TypedPasteRequest> { req ->
call.handleHead(req)
}
head<PasteRequest> { req ->
head<HtmlPasteRequest> { req ->
call.handleHead(req)
}
}
private suspend fun ApplicationCall.handleHead(paste: AbstractPasteRequest) {
if (PasteDao.selectByUri(paste.uri) != null) {
respond(HttpStatusCode.OK)
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)
Pair(name, ext)
} else {
Pair(uri, null)
}
}
@ExperimentalStdlibApi
private suspend fun ApplicationCall.handlePost() {
receiveMultipart().forEachPart { part ->
@ -121,13 +125,13 @@ object Routes {
return uri
}
private suspend fun ApplicationCall.handleGet(req: AbstractPasteRequest) {
val uri = req.uri
private suspend fun ApplicationCall.handleGet(req: PasteRequest) {
val (uri, ext) = splitPath(req.uri)
Log.info("Retrieving paste $uri")
PasteDao.selectByUri(uri)?.data?.let { paste ->
respond(
HttpStatusCode.OK,
PastePage.build(paste.content, req.filetype)
PastePage.build(paste.content, ext)
)
} ?: respond(HttpStatusCode.NotFound, "nothing found for id $uri")
}
@ -141,20 +145,14 @@ object Routes {
}
}
@KtorExperimentalLocationsAPI
// tfw we can’t do {id}.{filetype} here because reasons:tm:
@Location("/{uri}/{filetype}")
data class TypedPasteRequest(override val uri: String, override val filetype: String) : AbstractPasteRequest()
@KtorExperimentalLocationsAPI
@Location("/r/{uri}")
data class RawPasteRequest(override val uri: String) : AbstractPasteRequest()
data class RawPasteRequest(override val uri: String) : PasteRequest
@KtorExperimentalLocationsAPI
@Location("/{uri}")
data class PasteRequest(override val uri: String) : AbstractPasteRequest()
data class HtmlPasteRequest(override val uri: String) : PasteRequest
abstract class AbstractPasteRequest {
abstract val uri: String
open val filetype: String? = null
interface PasteRequest {
val uri: String
}

View File

@ -48,9 +48,8 @@ object AboutPage {
+"If the automatic detection fails, you can override it by appending the file extension to the url."
br
div("framed") {
+"$domain/rx78/kt"
+"$domain/rx78.kt"
}
+"I wanted to use “/<id>.<ext>”, but it looks like ktor locations don’t work like that. Or I’m just bad at this."
br
br
+"If you choose to disable Javascript entirely, everything other than the syntax highlighting will still work as intended. "