diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a921a4196..4896601051 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ * `Core`: * Several new extensions `ByteReadChannel#asStorageFile` and `ByteReadChannelAllocator#asStorageFile` + * Several new extensions `ByteArray#asMultipartFile`, `ByteReadChannel#asMultipartFile` and + `ByteReadChannelAllocator#asMultipartFile` * `API`: * New extensions `TelegramBot#downloadFile` for writing of incoming bytes to the file * New extensions `TelegramBot#downloadFileStream` and `TelegramBot#downloadFileStreamAllocator` for getting of input diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/abstracts/InputFile.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/abstracts/InputFile.kt index d6111400e6..986510d47b 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/abstracts/InputFile.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/abstracts/InputFile.kt @@ -1,7 +1,7 @@ package dev.inmo.tgbotapi.requests.abstracts -import dev.inmo.tgbotapi.utils.RiskFeature -import dev.inmo.tgbotapi.utils.StorageFile +import dev.inmo.tgbotapi.utils.* +import io.ktor.utils.io.ByteReadChannel import kotlinx.serialization.KSerializer import kotlinx.serialization.Serializable import kotlinx.serialization.descriptors.* @@ -42,6 +42,8 @@ object InputFileSerializer : KSerializer { // TODO:: add checks for files size /** * Contains info about file for sending + * + * @see asMultipartFile */ @Serializable(InputFileSerializer::class) data class MultipartFile ( @@ -51,3 +53,22 @@ data class MultipartFile ( ) : InputFile() { override val fileId: String = file.storageFileInfo.generateCustomName() } + + +@Suppress("NOTHING_TO_INLINE", "unused") +inline fun ByteArray.asMultipartFile( + fileName: String, + mimeType: MimeType +) = MultipartFile(asStorageFile(fileName, mimeType)) + +@Suppress("NOTHING_TO_INLINE", "unused") +suspend inline fun ByteReadChannel.asMultipartFile( + fileName: String, + mimeType: MimeType +) = MultipartFile(asStorageFile(fileName, mimeType)) + +@Suppress("NOTHING_TO_INLINE", "unused") +suspend inline fun ByteReadChannelAllocator.asMultipartFile( + fileName: String, + mimeType: MimeType +) = this.invoke().asMultipartFile(fileName, mimeType)