diff --git a/src/main/kotlin/moe/kageru/kodeshare/Routes.kt b/src/main/kotlin/moe/kageru/kodeshare/Routes.kt index 072366f..98fff7d 100644 --- a/src/main/kotlin/moe/kageru/kodeshare/Routes.kt +++ b/src/main/kotlin/moe/kageru/kodeshare/Routes.kt @@ -47,37 +47,41 @@ object Routes { get("/about") { call.respond(HttpStatusCode.OK, AboutPage.content) } - get { req -> + get { req -> call.handleGet(req) } get { req -> call.handleRaw(req) } - get { req -> - call.handleGet(req) - } head("/") { call.respond(HttpStatusCode.OK) } head { req -> - call.handleHead(req) + call.handleHead(req, true) } - head { req -> - call.handleHead(req) - } - head { req -> + head { 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 { + 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 } diff --git a/src/main/kotlin/moe/kageru/kodeshare/pages/AboutPage.kt b/src/main/kotlin/moe/kageru/kodeshare/pages/AboutPage.kt index 1c17c72..699abe6 100644 --- a/src/main/kotlin/moe/kageru/kodeshare/pages/AboutPage.kt +++ b/src/main/kotlin/moe/kageru/kodeshare/pages/AboutPage.kt @@ -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 “/.”, 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. "