add mime type working

This commit is contained in:
InsanusMokrassar 2020-08-17 22:50:13 +06:00
parent ae51e728d4
commit a681f28308
7 changed files with 75 additions and 5 deletions

View File

@ -1,7 +1,6 @@
package com.insanusmokrassar.postssystem.core.content package com.insanusmokrassar.postssystem.core.content
import com.insanusmokrassar.postssystem.utils.common.ByteArrayAllocator import com.insanusmokrassar.postssystem.utils.common.*
import com.insanusmokrassar.postssystem.utils.common.ByteArrayAllocatorSerializer
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
typealias ContentId = String typealias ContentId = String
@ -12,6 +11,10 @@ typealias ContentId = String
@Serializable @Serializable
sealed class Content sealed class Content
/**
* It is content, which was added by some external source to use inside of some plugins. For example, you can use
* location as content inside your target system and add this type of content specially for this system only
*/
@Serializable @Serializable
data class SpecialContent( data class SpecialContent(
val internalId: ContentId val internalId: ContentId
@ -24,7 +27,7 @@ data class TextContent(
@Serializable @Serializable
data class BinaryContent( data class BinaryContent(
val mimeType: String, val mimeType: MimeType,
val originalFileName: String, val originalFileName: String,
@Serializable(ByteArrayAllocatorSerializer::class) @Serializable(ByteArrayAllocatorSerializer::class)
val dataAllocator: ByteArrayAllocator val dataAllocator: ByteArrayAllocator

View File

@ -2,6 +2,7 @@ package com.insanusmokrassar.postssystem.core.exposed.content
import com.insanusmokrassar.postssystem.core.content.BinaryContent import com.insanusmokrassar.postssystem.core.content.BinaryContent
import com.insanusmokrassar.postssystem.core.content.ContentId import com.insanusmokrassar.postssystem.core.content.ContentId
import com.insanusmokrassar.postssystem.utils.common.mimeType
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.statements.api.ExposedBlob import org.jetbrains.exposed.sql.statements.api.ExposedBlob
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
@ -31,7 +32,7 @@ private class BinaryContentHolderRepoTable(
}.limit(1).firstOrNull() ?.let { }.limit(1).firstOrNull() ?.let {
val bytes = it[dataColumn].bytes val bytes = it[dataColumn].bytes
BinaryContent( BinaryContent(
it[mimeColumn], mimeType(it[mimeColumn]),
it[originalFileNameColumn] it[originalFileNameColumn]
) { ) {
bytes bytes
@ -54,7 +55,7 @@ private class BinaryContentHolderRepoTable(
insert { insert {
it[idColumn] = id it[idColumn] = id
it[originalFileNameColumn] = content.originalFileName it[originalFileNameColumn] = content.originalFileName
it[mimeColumn] = content.mimeType it[mimeColumn] = content.mimeType.raw
it[dataColumn] = ExposedBlob(content.dataAllocator()) it[dataColumn] = ExposedBlob(content.dataAllocator())
} }
} }

View File

@ -12,6 +12,8 @@ uuidVersion=0.1.1
exposed_version=0.26.2 exposed_version=0.26.2
test_sqlite_version=3.32.3.2 test_sqlite_version=3.32.3.2
javax_activation_version=1.1.1
gradle_bintray_plugin_version=1.8.4 gradle_bintray_plugin_version=1.8.4
core_version=0.4.2 core_version=0.4.2

View File

@ -55,6 +55,8 @@ kotlin {
implementation kotlin('stdlib-jdk8') implementation kotlin('stdlib-jdk8')
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version" api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version"
api "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$kotlin_serialisation_runtime_version" api "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$kotlin_serialisation_runtime_version"
api "javax.activation:activation:$javax_activation_version"
} }
} }
jvmTest { jvmTest {

View File

@ -0,0 +1,30 @@
package com.insanusmokrassar.postssystem.utils.common
import kotlinx.serialization.*
private val mimesCache = mutableMapOf<String, MimeType>()
@Serializable(MimeTypeSerializer::class)
expect class MimeType {
val raw: String
}
fun mimeType(raw: String) = mimesCache.getOrPut(raw) {
parseMimeType(raw)
}
internal expect fun parseMimeType(raw: String): MimeType
@Serializer(MimeType::class)
internal object MimeTypeSerializer : KSerializer<MimeType> {
override val descriptor: SerialDescriptor = PrimitiveDescriptor("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)
}
}

View File

@ -0,0 +1,19 @@
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,13 @@
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)