add mimetype is image check for binary content and upgrade mime types
This commit is contained in:
parent
a681f28308
commit
3ab7451b2e
@ -33,6 +33,9 @@ data class BinaryContent(
|
|||||||
val dataAllocator: ByteArrayAllocator
|
val dataAllocator: ByteArrayAllocator
|
||||||
) : Content()
|
) : Content()
|
||||||
|
|
||||||
|
val BinaryContent.isImage: Boolean
|
||||||
|
get() = mimeType is KnownMimeTypes.Image
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Content which is already registered in database. Using its [id] you can retrieve all known
|
* Content which is already registered in database. Using its [id] you can retrieve all known
|
||||||
* [com.insanusmokrassar.postssystem.core.post.RegisteredPost]s by using
|
* [com.insanusmokrassar.postssystem.core.post.RegisteredPost]s by using
|
||||||
|
@ -0,0 +1,32 @@
|
|||||||
|
package com.insanusmokrassar.postssystem.utils.common
|
||||||
|
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
@Serializable(MimeTypeSerializer::class)
|
||||||
|
interface MimeType {
|
||||||
|
val raw: String
|
||||||
|
}
|
||||||
|
data class CustomMimeType(override val raw: String) : MimeType
|
||||||
|
|
||||||
|
private const val imageMainType = "image"
|
||||||
|
|
||||||
|
sealed class KnownMimeTypes(override val raw: String) : MimeType {
|
||||||
|
object Any : MimeType, KnownMimeTypes("*/*")
|
||||||
|
sealed class Image(raw: String) : MimeType, KnownMimeTypes(raw) {
|
||||||
|
object Any : Image("$imageMainType/*")
|
||||||
|
object Gif : Image("$imageMainType/gif")
|
||||||
|
object Jpeg : Image("$imageMainType/jpeg")
|
||||||
|
object PNG : Image("$imageMainType/png")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
internal val knownMimeTypes: Set<MimeType> = setOf(
|
||||||
|
KnownMimeTypes.Any,
|
||||||
|
KnownMimeTypes.Image.Any,
|
||||||
|
KnownMimeTypes.Image.Gif,
|
||||||
|
KnownMimeTypes.Image.Jpeg,
|
||||||
|
KnownMimeTypes.Image.PNG
|
||||||
|
)
|
||||||
|
fun findBuiltinMimeType(from: String): MimeType? {
|
||||||
|
return knownMimeTypes.firstOrNull { it.raw == from }
|
||||||
|
}
|
@ -2,18 +2,15 @@ package com.insanusmokrassar.postssystem.utils.common
|
|||||||
|
|
||||||
import kotlinx.serialization.*
|
import kotlinx.serialization.*
|
||||||
|
|
||||||
private val mimesCache = mutableMapOf<String, MimeType>()
|
private val mimesCache = mutableMapOf<String, MimeType>().also {
|
||||||
|
knownMimeTypes.forEach { mimeType -> it[mimeType.raw] = mimeType }
|
||||||
@Serializable(MimeTypeSerializer::class)
|
|
||||||
expect class MimeType {
|
|
||||||
val raw: String
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun mimeType(raw: String) = mimesCache.getOrPut(raw) {
|
fun mimeType(raw: String) = mimesCache.getOrPut(raw) {
|
||||||
parseMimeType(raw)
|
parseMimeType(raw)
|
||||||
}
|
}
|
||||||
|
|
||||||
internal expect fun parseMimeType(raw: String): MimeType
|
internal fun parseMimeType(raw: String): MimeType = findBuiltinMimeType(raw) ?: CustomMimeType(raw)
|
||||||
|
|
||||||
@Serializer(MimeType::class)
|
@Serializer(MimeType::class)
|
||||||
internal object MimeTypeSerializer : KSerializer<MimeType> {
|
internal object MimeTypeSerializer : KSerializer<MimeType> {
|
@ -1,19 +0,0 @@
|
|||||||
package com.insanusmokrassar.postssystem.utils.common
|
|
||||||
|
|
||||||
import kotlinx.serialization.*
|
|
||||||
import org.w3c.dom.get
|
|
||||||
import kotlin.browser.window
|
|
||||||
|
|
||||||
private val mimesCache = mutableMapOf<String, MimeType>()
|
|
||||||
|
|
||||||
@Serializable(MimeTypeSerializer::class)
|
|
||||||
actual class MimeType(
|
|
||||||
actual val raw: String
|
|
||||||
) {
|
|
||||||
@Transient
|
|
||||||
val jsMimeType = window.navigator.mimeTypes[raw]
|
|
||||||
|
|
||||||
override fun toString(): String = raw
|
|
||||||
}
|
|
||||||
|
|
||||||
actual fun parseMimeType(raw: String) = MimeType(raw)
|
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.insanusmokrassar.postssystem.utils.common
|
||||||
|
|
||||||
|
import org.w3c.dom.get
|
||||||
|
import kotlin.browser.window
|
||||||
|
|
||||||
|
val MimeType.jsMimeType
|
||||||
|
get() = window.navigator.mimeTypes[raw]
|
@ -1,13 +0,0 @@
|
|||||||
package com.insanusmokrassar.postssystem.utils.common
|
|
||||||
|
|
||||||
import kotlinx.serialization.*
|
|
||||||
|
|
||||||
private val mimesCache = mutableMapOf<String, MimeType>()
|
|
||||||
|
|
||||||
@Serializable(MimeTypeSerializer::class)
|
|
||||||
actual class MimeType(
|
|
||||||
actual val raw: String
|
|
||||||
) : javax.activation.MimeType(raw)
|
|
||||||
|
|
||||||
actual fun parseMimeType(raw: String): MimeType = MimeType(raw)
|
|
||||||
|
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.insanusmokrassar.postssystem.utils.common
|
||||||
|
|
||||||
|
val MimeType.asActivationMimeType: javax.activation.MimeType
|
||||||
|
get() = javax.activation.MimeType(raw)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user