From 1089c716f3870ad1500968dc4f093d4a74d621c4 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 11 Apr 2022 21:00:56 +0600 Subject: [PATCH] deprecate StorageFileInfo --- CHANGELOG.md | 1 + .../tgbotapi/requests/abstracts/InputFile.kt | 4 +-- .../dev/inmo/tgbotapi/utils/StorageFile.kt | 30 +++++++++++++++---- .../inmo/tgbotapi/utils/StorageFileFactory.kt | 2 +- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc0eeb7b71..5e78c0d740 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * `Core`: * Fixes in `mention` creation + * Deprecate `StorageFileInfo` ## 0.38.12 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 8c71b6c6c8..4e5949aa6d 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 @@ -61,9 +61,9 @@ object InputFileSerializer : KSerializer { @Serializable(InputFileSerializer::class) data class MultipartFile ( val file: StorageFile, - val filename: String = file.storageFileInfo.fileName + val filename: String = file.fileName ) : InputFile() { - override val fileId: String = file.storageFileInfo.generateCustomName() + override val fileId: String = file.generateCustomName() } @Suppress("NOTHING_TO_INLINE", "unused") 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 2296e232dd..a0dfe6e0ab 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,8 @@ package dev.inmo.tgbotapi.utils import com.benasher44.uuid.uuid4 +import dev.inmo.micro_utils.common.MPPFile +import dev.inmo.micro_utils.common.filename import io.ktor.utils.io.* import io.ktor.utils.io.core.ByteReadPacket import io.ktor.utils.io.core.Input @@ -13,6 +15,7 @@ import kotlinx.serialization.Serializable * @param fileName This filename will be used in telegram system as name of file */ @Serializable +@Deprecated("Will be removed soon") data class StorageFileInfo( val fileName: String ) { @@ -25,18 +28,35 @@ data class StorageFileInfo( /** * Contains info about file, which potentially can be sent to telegram system. * - * @param storageFileInfo Information about this file + * @param fileName Filename * @param inputSource Lambda which able to allocate [Input] for uploading/manipulating data * * @see StorageFileInfo * @see asStorageFile */ data class StorageFile( - val storageFileInfo: StorageFileInfo, + val fileName: String, private val inputSource: () -> Input ) { val input: Input get() = inputSource() + @Deprecated("This field will be removed soon. Use fileName instead of StorageFileInfo") + val storageFileInfo: StorageFileInfo + get() = StorageFileInfo(fileName) + + /** + * This methods is required for random generation of name for keeping warranties about unique file name + */ + fun generateCustomName() = "${uuid4()}.${fileName.fileExtension}" + + @Deprecated("This constructor will be removed soon. Use constructor with fileName instead of StorageFileInfo") + constructor( + storageFileInfo: StorageFileInfo, + inputSource: () -> Input + ) : this( + storageFileInfo.fileName, + inputSource + ) } @Suppress("NOTHING_TO_INLINE") @@ -44,7 +64,7 @@ inline fun StorageFile( fileName: String, bytes: ByteArray ) = StorageFile( - StorageFileInfo(fileName) + fileName ) { ByteReadPacket(bytes) } @@ -54,8 +74,8 @@ suspend inline fun StorageFile( fileName: String, byteReadChannel: ByteReadChannel ) = StorageFile( - StorageFileInfo(fileName), - byteReadChannel.asInput().let { { it } } + fileName, + inputSource = byteReadChannel.asInput().let { { it } } ) @Suppress("NOTHING_TO_INLINE", "unused") diff --git a/tgbotapi.core/src/jvmMain/kotlin/dev/inmo/tgbotapi/utils/StorageFileFactory.kt b/tgbotapi.core/src/jvmMain/kotlin/dev/inmo/tgbotapi/utils/StorageFileFactory.kt index 9d26befaeb..2ceb51562f 100644 --- a/tgbotapi.core/src/jvmMain/kotlin/dev/inmo/tgbotapi/utils/StorageFileFactory.kt +++ b/tgbotapi.core/src/jvmMain/kotlin/dev/inmo/tgbotapi/utils/StorageFileFactory.kt @@ -7,7 +7,7 @@ import java.nio.file.Files fun StorageFile( file: File ) = StorageFile( - StorageFileInfo(file.name) + file.name ) { file.inputStream().asInput() }