Optimize and refactor code
authorLanius Trolling <lanius@laniustrolling.dev>
Sun, 14 Apr 2024 10:40:32 +0000 (06:40 -0400)
committerLanius Trolling <lanius@laniustrolling.dev>
Sun, 14 Apr 2024 10:40:32 +0000 (06:40 -0400)
src/jvmMain/kotlin/info/mechyrdia/data/DataFiles.kt
src/jvmMain/kotlin/info/mechyrdia/lore/ParserHtml.kt

index b1127d996e8f64b3a4f4a87f3e20633d307b5773..d5b5a02ce631f0f58e5c4c817836325e81978ddd 100644 (file)
@@ -24,17 +24,19 @@ import java.io.File
 import java.nio.ByteBuffer
 import java.nio.file.FileAlreadyExistsException
 import java.nio.file.Files
-import java.nio.file.LinkOption
-import java.nio.file.attribute.BasicFileAttributeView
 import java.nio.file.attribute.BasicFileAttributes
 import java.time.Instant
 import kotlin.String
 import kotlin.time.Duration.Companion.hours
 
+fun StoragePath.getContentType(): ContentType {
+       val extension = elements.last().substringAfter('.', "")
+       return if (extension.isEmpty()) ContentType.Text.Plain else ContentType.defaultForFileExtension(extension)
+}
+
 suspend fun ApplicationCall.respondStoredFile(path: StoragePath) {
        val content = FileStorage.instance.readFile(path) ?: return respond(HttpStatusCode.NotFound)
-       val extension = path.elements.last().substringAfter('.', "")
-       val type = if (extension.isEmpty()) ContentType.Text.Plain else ContentType.defaultForFileExtension(extension)
+       val type = path.getContentType()
        
        attributes.put(StoragePathAttributeKey, path)
        respondBytes(content, type)
index 614a0eeaaa505f34a16fb788aab563cefbfb922e..c6fbe098885656579d8ff41b4d65b7ec6ac6f52e 100644 (file)
@@ -4,7 +4,7 @@ import info.mechyrdia.JsonStorageCodec
 import io.ktor.util.*
 import kotlinx.html.*
 import kotlinx.html.org.w3c.dom.events.Event
-import kotlinx.html.stream.appendHTML
+import kotlinx.html.stream.createHTML
 import kotlinx.serialization.json.JsonPrimitive
 import kotlin.text.toCharArray
 
@@ -14,13 +14,9 @@ typealias HtmlBuilderSubject = TagConsumer<*>.() -> Any?
 context(T)
 operator fun <T : Tag> (TagConsumer<*>.() -> Any?).unaryPlus() = with(HtmlLexerTagConsumer(consumer)) { this@unaryPlus() }
 
-fun (TagConsumer<*>.() -> Any?).toFragment() = StringBuilder()
-       .appendHTML()
-       .also { builder ->
-               with(HtmlLexerTagConsumer(builder)) { this@toFragment() }
-       }
-       .finalize()
-       .toString()
+fun (TagConsumer<*>.() -> Any?).toFragment() = createHTML().also { builder ->
+       with(HtmlLexerTagConsumer(builder)) { this@toFragment() }
+}.finalize()
 
 class HtmlLexerTagConsumer private constructor(private val downstream: TagConsumer<*>) : TagConsumer<Unit> {
        override fun onTagStart(tag: Tag) {