mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-12-23 00:57:13 +00:00
files downloading improvements
This commit is contained in:
parent
8dbdbdee13
commit
abc0457a36
@ -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
|
||||
|
||||
|
@ -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}"
|
||||
|
@ -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(
|
||||
|
@ -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
|
||||
)
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user