1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 16:23:48 +00:00

files downloading improvements

This commit is contained in:
InsanusMokrassar 2021-08-09 19:55:08 +06:00
parent 8dbdbdee13
commit abc0457a36
5 changed files with 72 additions and 5 deletions

View File

@ -7,6 +7,10 @@
* `Common`: * `Common`:
* `Version`: * `Version`:
* `MicroUtils`: `0.5.18` -> `0.5.19` * `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 ## 0.35.3

View File

@ -5,8 +5,7 @@ import dev.inmo.tgbotapi.types.FileUniqueId
import dev.inmo.tgbotapi.types.fileUniqueIdField import dev.inmo.tgbotapi.types.fileUniqueIdField
import dev.inmo.tgbotapi.types.files.abstracts.* import dev.inmo.tgbotapi.types.files.abstracts.*
import dev.inmo.tgbotapi.utils.* import dev.inmo.tgbotapi.utils.*
import kotlinx.serialization.SerialName import kotlinx.serialization.*
import kotlinx.serialization.Serializable
@Serializable @Serializable
data class PathedFile( data class PathedFile(
@ -18,8 +17,14 @@ data class PathedFile(
val filePath: String, val filePath: String,
@SerialName(fileSizeField) @SerialName(fileSizeField)
override val fileSize: Long? = null 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 val PathedFile.filename: FileName
get() = filePath.filenameFromUrl get() = filePath.filenameFromUrl
fun TelegramAPIUrlsKeeper.resolveFileURL(file: PathedFile): String = "$fileBaseUrl/${file.filePath}" fun TelegramAPIUrlsKeeper.resolveFileURL(file: PathedFile): String = "$fileBaseUrl/${file.filePath}"

View File

@ -16,8 +16,8 @@ suspend fun TelegramBot.downloadFile(
suspend fun TelegramBot.downloadFile( suspend fun TelegramBot.downloadFile(
pathedFile: PathedFile pathedFile: PathedFile
): ByteArray = execute( ): ByteArray = downloadFile(
DownloadFile(pathedFile.filePath) pathedFile.filePath
) )
suspend fun TelegramBot.downloadFile( suspend fun TelegramBot.downloadFile(

View File

@ -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
)

View File

@ -9,6 +9,7 @@ fun PathedFile.asStream(
telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper
): InputStream = URL(this.fullUrl(telegramAPIUrlsKeeper)).openStream() ): InputStream = URL(this.fullUrl(telegramAPIUrlsKeeper)).openStream()
@Deprecated("This api will be removed soon. Use `downloadFile` instead")
fun PathedFile.asFile( fun PathedFile.asFile(
telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper, telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper,
dest: File = File.createTempFile(this.fileUniqueId, this.filename), dest: File = File.createTempFile(this.fileUniqueId, this.filename),
@ -22,6 +23,7 @@ fun PathedFile.asFile(
return dest return dest
} }
@Deprecated("This api will be removed soon. Use `downloadFile` instead")
fun PathedFile.asBytes( fun PathedFile.asBytes(
telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper telegramAPIUrlsKeeper: TelegramAPIUrlsKeeper
): ByteArray = this.asStream(telegramAPIUrlsKeeper) ): ByteArray = this.asStream(telegramAPIUrlsKeeper)