From 1e6b0381eefad2cd82a15a9fb62f1ed6ebbb8f93 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 7 Nov 2020 14:44:05 +0600 Subject: [PATCH] StorageFile factories --- CHANGELOG.md | 1 + .../kotlin/dev/inmo/tgbotapi/utils/StorageFile.kt | 15 +++++++++++++++ .../requests/abstracts/InputFileFromJavaFile.kt | 2 ++ .../kotlin/dev/inmo/tgbotapi/utils/StorageFile.kt | 1 + 4 files changed, 19 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a5c441700..f7e9277351 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -79,6 +79,7 @@ `textSources` list * New dsl for creating of `TextSource` lists * Built-in `handleSafely` and `ExceptionHandler` is deprecated + * New common factories for `StorageFile` * `API`: * Extensions `TelegramBot#pinChatMessage` now support any `Chat` and `Message`s from any `Chat` * New extensions `TelegramBot#unpinAllChatMessages` diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/StorageFile.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/StorageFile.kt index 5ee622b20f..a5beeeb0ca 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/StorageFile.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/StorageFile.kt @@ -1,6 +1,7 @@ package dev.inmo.tgbotapi.utils import com.benasher44.uuid.uuid4 +import io.ktor.utils.io.core.ByteReadPacket import io.ktor.utils.io.core.Input import kotlinx.serialization.Serializable @@ -18,3 +19,17 @@ data class StorageFile( ) { fun asInput() = inputSource() } + +@Suppress("NOTHING_TO_INLINE") +inline fun StorageFile( + fileName: String, + bytes: ByteArray, + mimeType: MimeType +) = StorageFile( + StorageFileInfo(mimeType.raw, fileName) +) { + ByteReadPacket(bytes) +} + +@Suppress("NOTHING_TO_INLINE") +inline fun ByteArray.asStorageFile(fileName: String, mimeType: MimeType) = StorageFile(fileName, this, mimeType) diff --git a/tgbotapi.core/src/jvmMain/kotlin/dev/inmo/tgbotapi/requests/abstracts/InputFileFromJavaFile.kt b/tgbotapi.core/src/jvmMain/kotlin/dev/inmo/tgbotapi/requests/abstracts/InputFileFromJavaFile.kt index 34350831f3..7492cc3d98 100644 --- a/tgbotapi.core/src/jvmMain/kotlin/dev/inmo/tgbotapi/requests/abstracts/InputFileFromJavaFile.kt +++ b/tgbotapi.core/src/jvmMain/kotlin/dev/inmo/tgbotapi/requests/abstracts/InputFileFromJavaFile.kt @@ -1,7 +1,9 @@ package dev.inmo.tgbotapi.requests.abstracts +import dev.inmo.tgbotapi.utils.MimeType import dev.inmo.tgbotapi.utils.StorageFile import java.io.File +import java.io.InputStream fun File.toInputFile() = if (exists()) { MultipartFile( diff --git a/tgbotapi.core/src/jvmMain/kotlin/dev/inmo/tgbotapi/utils/StorageFile.kt b/tgbotapi.core/src/jvmMain/kotlin/dev/inmo/tgbotapi/utils/StorageFile.kt index 8d7ebe5c53..4aab4c57b6 100644 --- a/tgbotapi.core/src/jvmMain/kotlin/dev/inmo/tgbotapi/utils/StorageFile.kt +++ b/tgbotapi.core/src/jvmMain/kotlin/dev/inmo/tgbotapi/utils/StorageFile.kt @@ -2,6 +2,7 @@ package dev.inmo.tgbotapi.utils import io.ktor.utils.io.streams.asInput import java.io.File +import java.io.InputStream import java.nio.file.Files fun StorageFile(