From abc0457a36c9fa15f624481411dd4f8c26312427 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 9 Aug 2021 19:55:08 +0600 Subject: [PATCH] files downloading improvements --- CHANGELOG.md | 4 ++ .../inmo/tgbotapi/types/files/PathedFile.kt | 11 +++- .../tgbotapi/extensions/api/DownloadFile.kt | 4 +- .../tgbotapi/extensions/api/DownloadFile.kt | 56 +++++++++++++++++++ .../inmo/tgbotapi/types/files/PathedFile.kt | 2 + 5 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 tgbotapi.extensions.api/src/jvmMain/kotlin/dev/inmo/tgbotapi/extensions/api/DownloadFile.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 9403cd66f9..81799d5b60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ * `Common`: * `Version`: * `MicroUtils`: `0.5.18` -> `0.5.19` +* `API`: + * New extensions `TelegramBot#downloadFile` for writing of incoming bytes to the file + * `PathedFile#filename` extension has been deprecated, and new property `PathedFile#fileName` has been included + directly in `PathedFile` ## 0.35.3 diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/files/PathedFile.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/files/PathedFile.kt index 06438895c8..247aa63c12 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/files/PathedFile.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/files/PathedFile.kt @@ -5,8 +5,7 @@ import dev.inmo.tgbotapi.types.FileUniqueId import dev.inmo.tgbotapi.types.fileUniqueIdField import dev.inmo.tgbotapi.types.files.abstracts.* import dev.inmo.tgbotapi.utils.* -import kotlinx.serialization.SerialName -import kotlinx.serialization.Serializable +import kotlinx.serialization.* @Serializable data class PathedFile( @@ -18,8 +17,14 @@ data class PathedFile( val filePath: String, @SerialName(fileSizeField) override val fileSize: Long? = null -): TelegramMediaFile +): TelegramMediaFile { + @Transient + val fileName: FileName by lazy(LazyThreadSafetyMode.PUBLICATION) { + filePath.filenameFromUrl + } +} +@Deprecated("Use fileName property instead", ReplaceWith("fileName")) val PathedFile.filename: FileName get() = filePath.filenameFromUrl fun TelegramAPIUrlsKeeper.resolveFileURL(file: PathedFile): String = "$fileBaseUrl/${file.filePath}" diff --git a/tgbotapi.extensions.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/DownloadFile.kt b/tgbotapi.extensions.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/DownloadFile.kt index 7f653730b6..3099d46591 100644 --- a/tgbotapi.extensions.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/DownloadFile.kt +++ b/tgbotapi.extensions.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/DownloadFile.kt @@ -16,8 +16,8 @@ suspend fun TelegramBot.downloadFile( suspend fun TelegramBot.downloadFile( pathedFile: PathedFile -): ByteArray = execute( - DownloadFile(pathedFile.filePath) +): ByteArray = downloadFile( + pathedFile.filePath ) suspend fun TelegramBot.downloadFile( diff --git a/tgbotapi.extensions.api/src/jvmMain/kotlin/dev/inmo/tgbotapi/extensions/api/DownloadFile.kt b/tgbotapi.extensions.api/src/jvmMain/kotlin/dev/inmo/tgbotapi/extensions/api/DownloadFile.kt new file mode 100644 index 0000000000..e9e6a23ffb --- /dev/null +++ b/tgbotapi.extensions.api/src/jvmMain/kotlin/dev/inmo/tgbotapi/extensions/api/DownloadFile.kt @@ -0,0 +1,56 @@ +package dev.inmo.tgbotapi.extensions.api + +import dev.inmo.micro_utils.coroutines.doOutsideOfCoroutine +import dev.inmo.tgbotapi.bot.TelegramBot +import dev.inmo.tgbotapi.extensions.api.get.getFileAdditionalInfo +import dev.inmo.tgbotapi.requests.DownloadFile +import dev.inmo.tgbotapi.requests.abstracts.FileId +import dev.inmo.tgbotapi.types.files.PathedFile +import dev.inmo.tgbotapi.types.files.abstracts.TelegramMediaFile +import dev.inmo.tgbotapi.types.message.content.abstracts.MediaContent +import java.io.File + +suspend fun TelegramBot.downloadFile( + filePath: String, + destFile: File +): File { + val bytes = downloadFile(filePath) + destFile.deleteRecursively() + + doOutsideOfCoroutine { destFile.createNewFile() } + destFile.writeBytes(bytes) + + return destFile +} + +suspend fun TelegramBot.downloadFile( + pathedFile: PathedFile, + destFile: File +) = downloadFile( + pathedFile.filePath, + destFile +) + +suspend fun TelegramBot.downloadFile( + fileId: FileId, + destFile: File +) = downloadFile( + getFileAdditionalInfo(fileId), + destFile +) + +suspend fun TelegramBot.downloadFile( + file: TelegramMediaFile, + destFile: File +): File = downloadFile( + getFileAdditionalInfo(file), + destFile +) + +suspend fun TelegramBot.downloadFile( + file: MediaContent, + destFile: File +) = downloadFile( + getFileAdditionalInfo(file.media), + destFile +) diff --git a/tgbotapi.extensions.utils/src/jvmMain/kotlin/dev/inmo/tgbotapi/types/files/PathedFile.kt b/tgbotapi.extensions.utils/src/jvmMain/kotlin/dev/inmo/tgbotapi/types/files/PathedFile.kt index 507349899d..e0cfe38006 100644 --- a/tgbotapi.extensions.utils/src/jvmMain/kotlin/dev/inmo/tgbotapi/types/files/PathedFile.kt +++ b/tgbotapi.extensions.utils/src/jvmMain/kotlin/dev/inmo/tgbotapi/types/files/PathedFile.kt @@ -9,6 +9,7 @@ fun PathedFile.asStream( telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper ): InputStream = URL(this.fullUrl(telegramAPIUrlsKeeper)).openStream() +@Deprecated("This api will be removed soon. Use `downloadFile` instead") fun PathedFile.asFile( telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper, dest: File = File.createTempFile(this.fileUniqueId, this.filename), @@ -22,6 +23,7 @@ fun PathedFile.asFile( return dest } +@Deprecated("This api will be removed soon. Use `downloadFile` instead") fun PathedFile.asBytes( telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper ): ByteArray = this.asStream(telegramAPIUrlsKeeper)