From 21a15db031287e986719ab49157095700253bdc9 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 3 Apr 2020 14:22:34 +0600 Subject: [PATCH] StorageFile improvement --- CHANGELOG.md | 4 ++++ .../requests/abstracts/InputFile.kt | 6 +++--- .../TelegramBotAPI/utils/StorageFile.kt | 17 ++++++++++++---- .../utils/StringFileExtension.kt | 6 ++++++ .../TelegramBotAPI/utils/StorageFile.kt | 19 ------------------ .../TelegramBotAPI/utils/StorageFile.kt | 20 ++++++++----------- 6 files changed, 34 insertions(+), 38 deletions(-) create mode 100644 TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFileExtension.kt delete mode 100644 TelegramBotAPI/src/jsMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 43a61ec3df..5d4a6606b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,10 @@ * `TelegramBotAPI`: * `BotCommand` now will check and throw error in case when command or description lengths is/are incorrect + * `StorageFile` now is common for all platforms + * JavaScript realization was removed due to its redundancy + * JVM realization was replaced with `fun` factory + * `StorageFile` now able to accept any factory of `Input` ## 0.25.0 diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/InputFile.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/InputFile.kt index 03065c533b..262623227d 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/InputFile.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/abstracts/InputFile.kt @@ -33,8 +33,8 @@ internal object InputFileSerializer : KSerializer { @Serializable(InputFileSerializer::class) data class MultipartFile ( val file: StorageFile, - val mimeType: String = file.contentType, - val filename: String = file.fileName + val mimeType: String = file.storageFileInfo.contentType, + val filename: String = file.storageFileInfo.fileName ) : InputFile() { - override val fileId: String = file.generateCustomName() + override val fileId: String = file.storageFileInfo.generateCustomName() } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt index c9fcf6700f..1acd7ffdbf 100644 --- a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt @@ -1,12 +1,21 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils +import com.benasher44.uuid.uuid4 import io.ktor.utils.io.core.Input import kotlinx.serialization.Serializable +import kotlinx.serialization.Transient @Serializable -expect class StorageFile { - val contentType: String +data class StorageFileInfo( + val contentType: String, val fileName: String - fun generateCustomName(): String - fun asInput(): Input +) { + fun generateCustomName() = "${uuid4()}.${fileName.fileExtension}" +} + +data class StorageFile( + val storageFileInfo: StorageFileInfo, + private val inputSource: () -> Input +) { + fun asInput() = inputSource() } diff --git a/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFileExtension.kt b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFileExtension.kt new file mode 100644 index 0000000000..5762b04a6c --- /dev/null +++ b/TelegramBotAPI/src/commonMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StringFileExtension.kt @@ -0,0 +1,6 @@ +package com.github.insanusmokrassar.TelegramBotAPI.utils + +private val extensionRegex = Regex("[^.]*$") + +val String.fileExtension + get() = extensionRegex.find(this) ?.value ?: "" diff --git a/TelegramBotAPI/src/jsMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt b/TelegramBotAPI/src/jsMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt deleted file mode 100644 index 17bc529db9..0000000000 --- a/TelegramBotAPI/src/jsMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt +++ /dev/null @@ -1,19 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.utils - -import com.benasher44.uuid.uuid4 -import io.ktor.utils.io.core.Input -import kotlinx.serialization.Serializable -import kotlinx.serialization.Transient - -@Serializable -actual data class StorageFile( - actual val contentType: String, - actual val fileName: String, - @Transient - val inputGetter: () -> Input = throw IllegalStateException("Can't create object without input"), - @Transient - val extension: String = throw IllegalStateException("Can't create object without extension") -) { - actual fun asInput(): Input = inputGetter() - actual fun generateCustomName(): String = "${uuid4()}.$extension" -} \ No newline at end of file diff --git a/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt b/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt index 878b534f0c..0f2cbd22b1 100644 --- a/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt +++ b/TelegramBotAPI/src/jvmMain/kotlin/com/github/insanusmokrassar/TelegramBotAPI/utils/StorageFile.kt @@ -1,20 +1,16 @@ package com.github.insanusmokrassar.TelegramBotAPI.utils -import com.benasher44.uuid.uuid4 -import io.ktor.utils.io.core.Input import io.ktor.utils.io.streams.asInput -import kotlinx.serialization.Serializable -import kotlinx.serialization.Transient import java.io.File import java.nio.file.Files -@Serializable -actual class StorageFile( - @Transient - private val file: File = throw IllegalStateException("Can't create object without file") +fun StorageFile( + file: File +) = StorageFile( + StorageFileInfo( + Files.probeContentType(file.toPath()), + file.name + ) ) { - actual val contentType: String = Files.probeContentType(file.toPath()) - actual val fileName: String = file.name - actual fun generateCustomName(): String = "${uuid4()}.${file.extension}" - actual fun asInput(): Input = Files.newInputStream(file.toPath()).asInput() + file.inputStream().asInput() }