mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 08:13:47 +00:00
improvements in files downloading api
This commit is contained in:
parent
c8ad68ea69
commit
40d94cca70
@ -4,6 +4,10 @@
|
||||
|
||||
* `API`:
|
||||
* New extensions `TelegramBot#downloadFile` for writing of incoming bytes to the file
|
||||
* New extensions `TelegramBot#downloadFileStream` and `TelegramBot#downloadFileStreamAllocator` for getting of input
|
||||
streams instead of whole bytes arrays
|
||||
* Old extensions `TelegramBot#downloadFile` has been replaced to the new package. Migration: replace in your project
|
||||
`import dev.inmo.tgbotapi.extensions.api.downloadFile` with `import dev.inmo.tgbotapi.extensions.api.files.downloadFile`
|
||||
* `PathedFile#filename` extension has been deprecated, and new property `PathedFile#fileName` has been included
|
||||
directly in `PathedFile`
|
||||
|
||||
|
@ -1,39 +1,33 @@
|
||||
package dev.inmo.tgbotapi.extensions.api
|
||||
|
||||
import dev.inmo.tgbotapi.bot.TelegramBot
|
||||
import dev.inmo.tgbotapi.extensions.api.get.getFileAdditionalInfo
|
||||
import dev.inmo.tgbotapi.requests.DownloadFile
|
||||
import dev.inmo.tgbotapi.extensions.api.files.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
|
||||
|
||||
@Deprecated("Replaced", ReplaceWith("downloadFile", "dev.inmo.tgbotapi.extensions.api.files.downloadFile"))
|
||||
suspend fun TelegramBot.downloadFile(
|
||||
filePath: String
|
||||
): ByteArray = execute(
|
||||
DownloadFile(filePath)
|
||||
)
|
||||
): ByteArray = downloadFile(filePath)
|
||||
|
||||
@Deprecated("Replaced", ReplaceWith("downloadFile", "dev.inmo.tgbotapi.extensions.api.files.downloadFile"))
|
||||
suspend fun TelegramBot.downloadFile(
|
||||
pathedFile: PathedFile
|
||||
): ByteArray = downloadFile(
|
||||
pathedFile.filePath
|
||||
)
|
||||
): ByteArray = downloadFile(pathedFile)
|
||||
|
||||
@Deprecated("Replaced", ReplaceWith("downloadFile", "dev.inmo.tgbotapi.extensions.api.files.downloadFile"))
|
||||
suspend fun TelegramBot.downloadFile(
|
||||
fileId: FileId
|
||||
): ByteArray = downloadFile(
|
||||
getFileAdditionalInfo(fileId)
|
||||
)
|
||||
): ByteArray = downloadFile(fileId)
|
||||
|
||||
@Deprecated("Replaced", ReplaceWith("downloadFile", "dev.inmo.tgbotapi.extensions.api.files.downloadFile"))
|
||||
suspend fun TelegramBot.downloadFile(
|
||||
file: TelegramMediaFile
|
||||
): ByteArray = downloadFile(
|
||||
getFileAdditionalInfo(file)
|
||||
)
|
||||
): ByteArray = downloadFile(file)
|
||||
|
||||
@Deprecated("Replaced", ReplaceWith("downloadFile", "dev.inmo.tgbotapi.extensions.api.files.downloadFile"))
|
||||
suspend fun TelegramBot.downloadFile(
|
||||
file: MediaContent
|
||||
): ByteArray = downloadFile(
|
||||
getFileAdditionalInfo(file.media)
|
||||
)
|
||||
): ByteArray = downloadFile(file)
|
||||
|
@ -0,0 +1,39 @@
|
||||
package dev.inmo.tgbotapi.extensions.api.files
|
||||
|
||||
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
|
||||
|
||||
suspend fun TelegramBot.downloadFile(
|
||||
filePath: String
|
||||
): ByteArray = execute(
|
||||
DownloadFile(filePath)
|
||||
)
|
||||
|
||||
suspend fun TelegramBot.downloadFile(
|
||||
pathedFile: PathedFile
|
||||
): ByteArray = downloadFile(
|
||||
pathedFile.filePath
|
||||
)
|
||||
|
||||
suspend fun TelegramBot.downloadFile(
|
||||
fileId: FileId
|
||||
): ByteArray = downloadFile(
|
||||
getFileAdditionalInfo(fileId)
|
||||
)
|
||||
|
||||
suspend fun TelegramBot.downloadFile(
|
||||
file: TelegramMediaFile
|
||||
): ByteArray = downloadFile(
|
||||
getFileAdditionalInfo(file)
|
||||
)
|
||||
|
||||
suspend fun TelegramBot.downloadFile(
|
||||
file: MediaContent
|
||||
): ByteArray = downloadFile(
|
||||
getFileAdditionalInfo(file.media)
|
||||
)
|
@ -0,0 +1,29 @@
|
||||
package dev.inmo.tgbotapi.extensions.api.files
|
||||
|
||||
import dev.inmo.tgbotapi.bot.TelegramBot
|
||||
import dev.inmo.tgbotapi.extensions.api.get.getFileAdditionalInfo
|
||||
import dev.inmo.tgbotapi.requests.DownloadFileStream
|
||||
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
|
||||
|
||||
suspend fun TelegramBot.downloadFileStream(
|
||||
filePath: String
|
||||
) = downloadFileStreamAllocator(filePath).invoke()
|
||||
|
||||
suspend fun TelegramBot.downloadFileStream(
|
||||
pathedFile: PathedFile
|
||||
) = downloadFileStream(pathedFile.filePath)
|
||||
|
||||
suspend fun TelegramBot.downloadFileStream(
|
||||
fileId: FileId
|
||||
) = downloadFileStream(getFileAdditionalInfo(fileId))
|
||||
|
||||
suspend fun TelegramBot.downloadFileStream(
|
||||
file: TelegramMediaFile
|
||||
) = downloadFileStream(getFileAdditionalInfo(file))
|
||||
|
||||
suspend fun TelegramBot.downloadFileStream(
|
||||
file: MediaContent
|
||||
) = downloadFileStream(getFileAdditionalInfo(file.media))
|
@ -0,0 +1,29 @@
|
||||
package dev.inmo.tgbotapi.extensions.api.files
|
||||
|
||||
import dev.inmo.tgbotapi.bot.TelegramBot
|
||||
import dev.inmo.tgbotapi.extensions.api.get.getFileAdditionalInfo
|
||||
import dev.inmo.tgbotapi.requests.DownloadFileStream
|
||||
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
|
||||
|
||||
suspend fun TelegramBot.downloadFileStreamAllocator(
|
||||
filePath: String
|
||||
) = execute(DownloadFileStream(filePath))
|
||||
|
||||
suspend fun TelegramBot.downloadFileStreamAllocator(
|
||||
pathedFile: PathedFile
|
||||
) = downloadFileStreamAllocator(pathedFile.filePath)
|
||||
|
||||
suspend fun TelegramBot.downloadFileStreamAllocator(
|
||||
fileId: FileId
|
||||
) = downloadFileStreamAllocator(getFileAdditionalInfo(fileId))
|
||||
|
||||
suspend fun TelegramBot.downloadFileStreamAllocator(
|
||||
file: TelegramMediaFile
|
||||
) = downloadFileStreamAllocator(getFileAdditionalInfo(file))
|
||||
|
||||
suspend fun TelegramBot.downloadFileStreamAllocator(
|
||||
file: MediaContent
|
||||
) = downloadFileStreamAllocator(getFileAdditionalInfo(file.media))
|
@ -1,24 +1,29 @@
|
||||
package dev.inmo.tgbotapi.extensions.api
|
||||
package dev.inmo.tgbotapi.extensions.api.files
|
||||
|
||||
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 io.ktor.util.cio.writeChannel
|
||||
import io.ktor.utils.io.copyTo
|
||||
import kotlinx.coroutines.job
|
||||
import java.io.File
|
||||
import kotlin.coroutines.coroutineContext
|
||||
|
||||
suspend fun TelegramBot.downloadFile(
|
||||
filePath: String,
|
||||
destFile: File
|
||||
): File {
|
||||
val bytes = downloadFile(filePath)
|
||||
destFile.deleteRecursively()
|
||||
val readChannel = downloadFileStream(filePath)
|
||||
|
||||
destFile.deleteRecursively()
|
||||
destFile.parentFile.mkdirs()
|
||||
doOutsideOfCoroutine { destFile.createNewFile() }
|
||||
destFile.writeBytes(bytes)
|
||||
|
||||
readChannel.copyTo(destFile.writeChannel(coroutineContext.job))
|
||||
|
||||
return destFile
|
||||
}
|
Loading…
Reference in New Issue
Block a user