MicroUtils/mime_types/src/commonMain/kotlin/dev/inmo/micro_utils/mime_types/MimeTypeTools.kt

32 lines
975 B
Kotlin
Raw Normal View History

2020-09-26 15:57:55 +00:00
package dev.inmo.micro_utils.mime_types
import kotlinx.serialization.*
2020-09-26 15:57:55 +00:00
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
private val mimesCache = mutableMapOf<String, MimeType>().also {
knownMimeTypes.forEach { mimeType -> it[mimeType.raw] = mimeType }
}
fun mimeType(raw: String) = mimesCache.getOrPut(raw) {
parseMimeType(raw)
}
internal fun parseMimeType(raw: String): MimeType = CustomMimeType(raw)
@Suppress("OPT_IN_USAGE")
2020-09-26 15:57:55 +00:00
@Serializer(MimeType::class)
object MimeTypeSerializer : KSerializer<MimeType> {
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("mimeType", PrimitiveKind.STRING)
override fun deserialize(decoder: Decoder): MimeType {
val mimeType = decoder.decodeString()
return mimeType(mimeType)
}
override fun serialize(encoder: Encoder, value: MimeType) {
encoder.encodeString(value.raw)
}
}