commonMain.com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.InputFile.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of TelegramBotAPI-jvm Show documentation
Show all versions of TelegramBotAPI-jvm Show documentation
Library for Object-Oriented and type-safe work with Telegram Bot API
package com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts
import com.github.insanusmokrassar.TelegramBotAPI.utils.StorageFile
import kotlinx.serialization.*
@Serializable(InputFileSerializer::class)
sealed class InputFile {
abstract val fileId: String
}
// TODO:: add checks for file url/file id regex
/**
* Contains file id or file url
*/
@Serializable(InputFileSerializer::class)
data class FileId(
override val fileId: String
) : InputFile()
fun String.toInputFile() = FileId(this)
@Serializer(InputFile::class)
internal object InputFileSerializer : KSerializer {
override val descriptor: SerialDescriptor = PrimitiveDescriptor(FileId::class.toString(), PrimitiveKind.STRING)
override fun serialize(encoder: Encoder, value: InputFile) = encoder.encodeString(value.fileId)
override fun deserialize(decoder: Decoder): FileId = FileId(decoder.decodeString())
}
// TODO:: add checks for files size
/**
* Contains info about file for sending
*/
@Serializable(InputFileSerializer::class)
data class MultipartFile (
val file: StorageFile,
val mimeType: String = file.contentType,
val filename: String = file.fileName
) : InputFile() {
override val fileId: String = file.generateCustomName()
}