Allow .extension in url
This commit is contained in:
parent
0f1bd98f49
commit
fbfec69e08
@ -47,37 +47,41 @@ object Routes {
|
|||||||
get("/about") {
|
get("/about") {
|
||||||
call.respond(HttpStatusCode.OK, AboutPage.content)
|
call.respond(HttpStatusCode.OK, AboutPage.content)
|
||||||
}
|
}
|
||||||
get<PasteRequest> { req ->
|
get<HtmlPasteRequest> { req ->
|
||||||
call.handleGet(req)
|
call.handleGet(req)
|
||||||
}
|
}
|
||||||
get<RawPasteRequest> { req ->
|
get<RawPasteRequest> { req ->
|
||||||
call.handleRaw(req)
|
call.handleRaw(req)
|
||||||
}
|
}
|
||||||
get<TypedPasteRequest> { req ->
|
|
||||||
call.handleGet(req)
|
|
||||||
}
|
|
||||||
head("/") {
|
head("/") {
|
||||||
call.respond(HttpStatusCode.OK)
|
call.respond(HttpStatusCode.OK)
|
||||||
}
|
}
|
||||||
head<RawPasteRequest> { req ->
|
head<RawPasteRequest> { req ->
|
||||||
call.handleHead(req)
|
call.handleHead(req, true)
|
||||||
}
|
}
|
||||||
head<TypedPasteRequest> { req ->
|
head<HtmlPasteRequest> { req ->
|
||||||
call.handleHead(req)
|
|
||||||
}
|
|
||||||
head<PasteRequest> { req ->
|
|
||||||
call.handleHead(req)
|
call.handleHead(req)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun ApplicationCall.handleHead(paste: AbstractPasteRequest) {
|
private suspend fun ApplicationCall.handleHead(paste: PasteRequest, raw: Boolean = false) {
|
||||||
if (PasteDao.selectByUri(paste.uri) != null) {
|
val uri = splitPath(paste.uri).first
|
||||||
respond(HttpStatusCode.OK)
|
if (PasteDao.selectByUri(uri) != null) {
|
||||||
|
respond(HttpStatusCode.OK, if (raw) ContentType.Text.Plain else ContentType.Text.Html)
|
||||||
} else {
|
} else {
|
||||||
respond(HttpStatusCode.NotFound)
|
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
|
@ExperimentalStdlibApi
|
||||||
private suspend fun ApplicationCall.handlePost() {
|
private suspend fun ApplicationCall.handlePost() {
|
||||||
receiveMultipart().forEachPart { part ->
|
receiveMultipart().forEachPart { part ->
|
||||||
@ -121,13 +125,13 @@ object Routes {
|
|||||||
return uri
|
return uri
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun ApplicationCall.handleGet(req: AbstractPasteRequest) {
|
private suspend fun ApplicationCall.handleGet(req: PasteRequest) {
|
||||||
val uri = 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(
|
respond(
|
||||||
HttpStatusCode.OK,
|
HttpStatusCode.OK,
|
||||||
PastePage.build(paste.content, req.filetype)
|
PastePage.build(paste.content, ext)
|
||||||
)
|
)
|
||||||
} ?: respond(HttpStatusCode.NotFound, "nothing found for id $uri")
|
} ?: 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
|
@KtorExperimentalLocationsAPI
|
||||||
@Location("/r/{uri}")
|
@Location("/r/{uri}")
|
||||||
data class RawPasteRequest(override val uri: String) : AbstractPasteRequest()
|
data class RawPasteRequest(override val uri: String) : PasteRequest
|
||||||
|
|
||||||
@KtorExperimentalLocationsAPI
|
@KtorExperimentalLocationsAPI
|
||||||
@Location("/{uri}")
|
@Location("/{uri}")
|
||||||
data class PasteRequest(override val uri: String) : AbstractPasteRequest()
|
data class HtmlPasteRequest(override val uri: String) : PasteRequest
|
||||||
|
|
||||||
abstract class AbstractPasteRequest {
|
interface PasteRequest {
|
||||||
abstract val uri: String
|
val uri: String
|
||||||
open val filetype: String? = null
|
|
||||||
}
|
}
|
||||||
|
@ -48,9 +48,8 @@ object AboutPage {
|
|||||||
+"If the automatic detection fails, you can override it by appending the file extension to the url."
|
+"If the automatic detection fails, you can override it by appending the file extension to the url."
|
||||||
br
|
br
|
||||||
div("framed") {
|
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
|
||||||
br
|
br
|
||||||
+"If you choose to disable Javascript entirely, everything other than the syntax highlighting will still work as intended. "
|
+"If you choose to disable Javascript entirely, everything other than the syntax highlighting will still work as intended. "
|
||||||
|
Loading…
Reference in New Issue
Block a user