add mimetype is image check for binary content and upgrade mime types

This commit is contained in:
InsanusMokrassar 2020-08-17 23:25:09 +06:00
parent a681f28308
commit 3ab7451b2e
7 changed files with 50 additions and 38 deletions

View File

@ -33,6 +33,9 @@ data class BinaryContent(
val dataAllocator: ByteArrayAllocator
) : 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
* [com.insanusmokrassar.postssystem.core.post.RegisteredPost]s by using

View File

@ -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 }
}

View File

@ -2,18 +2,15 @@ package com.insanusmokrassar.postssystem.utils.common
import kotlinx.serialization.*
private val mimesCache = mutableMapOf<String, MimeType>()
@Serializable(MimeTypeSerializer::class)
expect class MimeType {
val raw: String
private val mimesCache = mutableMapOf<String, MimeType>().also {
knownMimeTypes.forEach { mimeType -> it[mimeType.raw] = mimeType }
}
fun mimeType(raw: String) = mimesCache.getOrPut(raw) {
parseMimeType(raw)
}
internal expect fun parseMimeType(raw: String): MimeType
internal fun parseMimeType(raw: String): MimeType = findBuiltinMimeType(raw) ?: CustomMimeType(raw)
@Serializer(MimeType::class)
internal object MimeTypeSerializer : KSerializer<MimeType> {

View File

@ -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)

View File

@ -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]

View File

@ -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)

View File

@ -0,0 +1,5 @@
package com.insanusmokrassar.postssystem.utils.common
val MimeType.asActivationMimeType: javax.activation.MimeType
get() = javax.activation.MimeType(raw)