1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-12-22 16:47:13 +00:00

add MimeType

This commit is contained in:
InsanusMokrassar 2020-05-10 14:37:55 +06:00
parent 9747c8bff1
commit 9b16d5d82b
12 changed files with 92 additions and 10 deletions

View File

@ -55,6 +55,9 @@
* Versions:
* Coroutines: `1.3.5` -> `1.3.6`
* Klock: `1.10.5` -> `1.11.1`
* `TelegramBotAPI`:
* Expected class `MimeType` was added
* Field `MimeTyped#mimeType` now typed by `MimeType` instead of `String`
### 0.27.1

View File

@ -1,5 +1,7 @@
package com.github.insanusmokrassar.TelegramBotAPI.CommonAbstracts
import com.github.insanusmokrassar.TelegramBotAPI.utils.MimeType
interface MimeTyped {
val mimeType: String? // TODO::replace by something like enum or interface
val mimeType: MimeType?
}

View File

@ -8,6 +8,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.ParseMode
import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.parseModeField
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup
import com.github.insanusmokrassar.TelegramBotAPI.types.files.abstracts.mimeTypeField
import com.github.insanusmokrassar.TelegramBotAPI.utils.MimeType
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@ -20,7 +21,7 @@ data class InlineQueryResultDocumentImpl(
@SerialName(titleField)
override val title: String,
@SerialName(mimeTypeField)
override val mimeType: String,
override val mimeType: MimeType,
@SerialName(thumbUrlField)
override val thumbUrl: String? = null,
@SerialName(thumbWidthField)

View File

@ -8,6 +8,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.ParseMode
import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.parseModeField
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup
import com.github.insanusmokrassar.TelegramBotAPI.types.files.abstracts.mimeTypeField
import com.github.insanusmokrassar.TelegramBotAPI.utils.MimeType
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@ -20,7 +21,7 @@ data class InlineQueryResultVideoImpl(
@SerialName(thumbUrlField)
override val thumbUrl: String,
@SerialName(mimeTypeField)
override val mimeType: String,
override val mimeType: MimeType,
@SerialName(titleField)
override val title: String,
@SerialName(videoWidthField)

View File

@ -4,6 +4,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.FileId
import com.github.insanusmokrassar.TelegramBotAPI.types.FileUniqueId
import com.github.insanusmokrassar.TelegramBotAPI.types.fileUniqueIdField
import com.github.insanusmokrassar.TelegramBotAPI.types.files.abstracts.*
import com.github.insanusmokrassar.TelegramBotAPI.utils.MimeType
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@ -20,7 +21,7 @@ data class AnimationFile(
@SerialName(fileNameField)
override val fileName: String? = null,
@SerialName(mimeTypeField)
override val mimeType: String? = null,
override val mimeType: MimeType? = null,
@SerialName(fileSizeField)
override val fileSize: Long? = null
) : TelegramMediaFile, MimedMediaFile, ThumbedMediaFile, PlayableMediaFile, CustomNamedMediaFile, SizedMediaFile

View File

@ -5,6 +5,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.FileId
import com.github.insanusmokrassar.TelegramBotAPI.types.FileUniqueId
import com.github.insanusmokrassar.TelegramBotAPI.types.fileUniqueIdField
import com.github.insanusmokrassar.TelegramBotAPI.types.files.abstracts.*
import com.github.insanusmokrassar.TelegramBotAPI.utils.MimeType
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@ -18,7 +19,7 @@ data class AudioFile(
override val performer: String? = null,
override val title: String? = null,
@SerialName(mimeTypeField)
override val mimeType: String? = null,
override val mimeType: MimeType? = null,
@SerialName(fileSizeField)
override val fileSize: Long? = null,
override val thumb: PhotoSize? = null

View File

@ -4,6 +4,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.FileId
import com.github.insanusmokrassar.TelegramBotAPI.types.FileUniqueId
import com.github.insanusmokrassar.TelegramBotAPI.types.fileUniqueIdField
import com.github.insanusmokrassar.TelegramBotAPI.types.files.abstracts.*
import com.github.insanusmokrassar.TelegramBotAPI.utils.MimeType
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@ -17,7 +18,7 @@ data class DocumentFile(
override val fileSize: Long? = null,
override val thumb: PhotoSize? = null,
@SerialName(mimeTypeField)
override val mimeType: String? = null,
override val mimeType: MimeType? = null,
@SerialName(fileNameField)
override val fileName: String? = null
) : TelegramMediaFile, MimedMediaFile, ThumbedMediaFile, CustomNamedMediaFile

View File

@ -4,6 +4,7 @@ import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.FileId
import com.github.insanusmokrassar.TelegramBotAPI.types.FileUniqueId
import com.github.insanusmokrassar.TelegramBotAPI.types.fileUniqueIdField
import com.github.insanusmokrassar.TelegramBotAPI.types.files.abstracts.*
import com.github.insanusmokrassar.TelegramBotAPI.utils.MimeType
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@ -18,7 +19,7 @@ data class VideoFile(
override val duration: Long? = null,
override val thumb: PhotoSize? = null,
@SerialName(mimeTypeField)
override val mimeType: String? = null,
override val mimeType: MimeType? = null,
@SerialName(fileSizeField)
override val fileSize: Long? = null
) : TelegramMediaFile, MimedMediaFile, ThumbedMediaFile, PlayableMediaFile, SizedMediaFile

View File

@ -1,9 +1,9 @@
package com.github.insanusmokrassar.TelegramBotAPI.types.files
import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.FileId
import com.github.insanusmokrassar.TelegramBotAPI.types.FileUniqueId
import com.github.insanusmokrassar.TelegramBotAPI.types.fileUniqueIdField
import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.files.abstracts.*
import com.github.insanusmokrassar.TelegramBotAPI.utils.MimeType
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@ -13,9 +13,10 @@ data class VoiceFile(
override val fileId: FileId,
@SerialName(fileUniqueIdField)
override val fileUniqueId: FileUniqueId,
@SerialName(durationField)
override val duration: Long? = null,
@SerialName(mimeTypeField)
override val mimeType: String? = null,
override val mimeType: MimeType? = null,
@SerialName(fileSizeField)
override val fileSize: Long? = null
) : TelegramMediaFile, MimedMediaFile, PlayableMediaFile

View File

@ -0,0 +1,11 @@
package com.github.insanusmokrassar.TelegramBotAPI.utils
import kotlinx.serialization.*
@Serializable(MimeTypeSerializer::class)
expect class MimeType {
val raw: String
}
@Serializer(MimeType::class)
internal expect object MimeTypeSerializer : KSerializer<MimeType>

View File

@ -0,0 +1,33 @@
package com.github.insanusmokrassar.TelegramBotAPI.utils
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
}
@Serializer(MimeType::class)
internal actual object MimeTypeSerializer : KSerializer<MimeType> {
override val descriptor: SerialDescriptor = PrimitiveDescriptor("mimeType", PrimitiveKind.STRING)
override fun deserialize(decoder: Decoder): MimeType {
val mimeType = decoder.decodeString()
return mimesCache.getOrPut(mimeType) {
MimeType(mimeType)
}
}
override fun serialize(encoder: Encoder, value: MimeType) {
encoder.encodeString(value.raw)
}
}

View File

@ -0,0 +1,26 @@
package com.github.insanusmokrassar.TelegramBotAPI.utils
import kotlinx.serialization.*
private val mimesCache = mutableMapOf<String, MimeType>()
@Serializable(MimeTypeSerializer::class)
actual class MimeType(
actual val raw: String
) : javax.activation.MimeType(raw)
@Serializer(MimeType::class)
internal actual object MimeTypeSerializer : KSerializer<MimeType> {
override val descriptor: SerialDescriptor = PrimitiveDescriptor("mimeType", PrimitiveKind.STRING)
override fun deserialize(decoder: Decoder): MimeType {
val mimeType = decoder.decodeString()
return mimesCache.getOrPut(mimeType) {
MimeType(mimeType)
}
}
override fun serialize(encoder: Encoder, value: MimeType) {
encoder.encodeString(value.raw)
}
}