From a681f283080eaa4a6aec15e90f587bf4e6cf7c35 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 17 Aug 2020 22:50:13 +0600 Subject: [PATCH] add mime type working --- .../postssystem/core/content/Content.kt | 9 ++++-- .../content/BinaryContentHolderRepo.kt | 5 ++-- gradle.properties | 2 ++ utils/common/build.gradle | 2 ++ .../postssystem/utils/common/MimeType.kt | 30 +++++++++++++++++++ .../postssystem/utils/common/MimeType.kt | 19 ++++++++++++ .../postssystem/utils/common/MimeType.kt | 13 ++++++++ 7 files changed, 75 insertions(+), 5 deletions(-) create mode 100644 utils/common/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/common/MimeType.kt create mode 100644 utils/common/src/jsMain/kotlin/com/insanusmokrassar/postssystem/utils/common/MimeType.kt create mode 100644 utils/common/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/utils/common/MimeType.kt diff --git a/core/api/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/content/Content.kt b/core/api/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/content/Content.kt index c6a85c55..92a8bee7 100644 --- a/core/api/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/content/Content.kt +++ b/core/api/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/content/Content.kt @@ -1,7 +1,6 @@ package com.insanusmokrassar.postssystem.core.content -import com.insanusmokrassar.postssystem.utils.common.ByteArrayAllocator -import com.insanusmokrassar.postssystem.utils.common.ByteArrayAllocatorSerializer +import com.insanusmokrassar.postssystem.utils.common.* import kotlinx.serialization.Serializable typealias ContentId = String @@ -12,6 +11,10 @@ typealias ContentId = String @Serializable 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 data class SpecialContent( val internalId: ContentId @@ -24,7 +27,7 @@ data class TextContent( @Serializable data class BinaryContent( - val mimeType: String, + val mimeType: MimeType, val originalFileName: String, @Serializable(ByteArrayAllocatorSerializer::class) val dataAllocator: ByteArrayAllocator diff --git a/core/exposed/src/main/kotlin/com/insanusmokrassar/postssystem/core/exposed/content/BinaryContentHolderRepo.kt b/core/exposed/src/main/kotlin/com/insanusmokrassar/postssystem/core/exposed/content/BinaryContentHolderRepo.kt index 8c74e05d..b2435ab0 100644 --- a/core/exposed/src/main/kotlin/com/insanusmokrassar/postssystem/core/exposed/content/BinaryContentHolderRepo.kt +++ b/core/exposed/src/main/kotlin/com/insanusmokrassar/postssystem/core/exposed/content/BinaryContentHolderRepo.kt @@ -2,6 +2,7 @@ package com.insanusmokrassar.postssystem.core.exposed.content import com.insanusmokrassar.postssystem.core.content.BinaryContent import com.insanusmokrassar.postssystem.core.content.ContentId +import com.insanusmokrassar.postssystem.utils.common.mimeType import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.statements.api.ExposedBlob import org.jetbrains.exposed.sql.transactions.transaction @@ -31,7 +32,7 @@ private class BinaryContentHolderRepoTable( }.limit(1).firstOrNull() ?.let { val bytes = it[dataColumn].bytes BinaryContent( - it[mimeColumn], + mimeType(it[mimeColumn]), it[originalFileNameColumn] ) { bytes @@ -54,7 +55,7 @@ private class BinaryContentHolderRepoTable( insert { it[idColumn] = id it[originalFileNameColumn] = content.originalFileName - it[mimeColumn] = content.mimeType + it[mimeColumn] = content.mimeType.raw it[dataColumn] = ExposedBlob(content.dataAllocator()) } } diff --git a/gradle.properties b/gradle.properties index c96d3493..a247c68e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -12,6 +12,8 @@ uuidVersion=0.1.1 exposed_version=0.26.2 test_sqlite_version=3.32.3.2 +javax_activation_version=1.1.1 + gradle_bintray_plugin_version=1.8.4 core_version=0.4.2 diff --git a/utils/common/build.gradle b/utils/common/build.gradle index 6f8071c1..153dcd0b 100644 --- a/utils/common/build.gradle +++ b/utils/common/build.gradle @@ -55,6 +55,8 @@ kotlin { implementation kotlin('stdlib-jdk8') api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version" api "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$kotlin_serialisation_runtime_version" + + api "javax.activation:activation:$javax_activation_version" } } jvmTest { diff --git a/utils/common/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/common/MimeType.kt b/utils/common/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/common/MimeType.kt new file mode 100644 index 00000000..930c6e66 --- /dev/null +++ b/utils/common/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/common/MimeType.kt @@ -0,0 +1,30 @@ +package com.insanusmokrassar.postssystem.utils.common + +import kotlinx.serialization.* + +private val mimesCache = mutableMapOf() + +@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 { + 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) + } +} diff --git a/utils/common/src/jsMain/kotlin/com/insanusmokrassar/postssystem/utils/common/MimeType.kt b/utils/common/src/jsMain/kotlin/com/insanusmokrassar/postssystem/utils/common/MimeType.kt new file mode 100644 index 00000000..ff7ff02b --- /dev/null +++ b/utils/common/src/jsMain/kotlin/com/insanusmokrassar/postssystem/utils/common/MimeType.kt @@ -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() + +@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) diff --git a/utils/common/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/utils/common/MimeType.kt b/utils/common/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/utils/common/MimeType.kt new file mode 100644 index 00000000..d4a7b9fb --- /dev/null +++ b/utils/common/src/jvmMain/kotlin/com/insanusmokrassar/postssystem/utils/common/MimeType.kt @@ -0,0 +1,13 @@ +package com.insanusmokrassar.postssystem.utils.common + +import kotlinx.serialization.* + +private val mimesCache = mutableMapOf() + +@Serializable(MimeTypeSerializer::class) +actual class MimeType( + actual val raw: String +) : javax.activation.MimeType(raw) + +actual fun parseMimeType(raw: String): MimeType = MimeType(raw) +