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`:
|
* `API`:
|
||||||
* New extensions `TelegramBot#downloadFile` for writing of incoming bytes to the file
|
* 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
|
* `PathedFile#filename` extension has been deprecated, and new property `PathedFile#fileName` has been included
|
||||||
directly in `PathedFile`
|
directly in `PathedFile`
|
||||||
|
|
||||||
|
@ -1,39 +1,33 @@
|
|||||||
package dev.inmo.tgbotapi.extensions.api
|
package dev.inmo.tgbotapi.extensions.api
|
||||||
|
|
||||||
import dev.inmo.tgbotapi.bot.TelegramBot
|
import dev.inmo.tgbotapi.bot.TelegramBot
|
||||||
import dev.inmo.tgbotapi.extensions.api.get.getFileAdditionalInfo
|
import dev.inmo.tgbotapi.extensions.api.files.downloadFile
|
||||||
import dev.inmo.tgbotapi.requests.DownloadFile
|
|
||||||
import dev.inmo.tgbotapi.requests.abstracts.FileId
|
import dev.inmo.tgbotapi.requests.abstracts.FileId
|
||||||
import dev.inmo.tgbotapi.types.files.PathedFile
|
import dev.inmo.tgbotapi.types.files.PathedFile
|
||||||
import dev.inmo.tgbotapi.types.files.abstracts.TelegramMediaFile
|
import dev.inmo.tgbotapi.types.files.abstracts.TelegramMediaFile
|
||||||
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaContent
|
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaContent
|
||||||
|
|
||||||
|
@Deprecated("Replaced", ReplaceWith("downloadFile", "dev.inmo.tgbotapi.extensions.api.files.downloadFile"))
|
||||||
suspend fun TelegramBot.downloadFile(
|
suspend fun TelegramBot.downloadFile(
|
||||||
filePath: String
|
filePath: String
|
||||||
): ByteArray = execute(
|
): ByteArray = downloadFile(filePath)
|
||||||
DownloadFile(filePath)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
@Deprecated("Replaced", ReplaceWith("downloadFile", "dev.inmo.tgbotapi.extensions.api.files.downloadFile"))
|
||||||
suspend fun TelegramBot.downloadFile(
|
suspend fun TelegramBot.downloadFile(
|
||||||
pathedFile: PathedFile
|
pathedFile: PathedFile
|
||||||
): ByteArray = downloadFile(
|
): ByteArray = downloadFile(pathedFile)
|
||||||
pathedFile.filePath
|
|
||||||
)
|
|
||||||
|
|
||||||
|
@Deprecated("Replaced", ReplaceWith("downloadFile", "dev.inmo.tgbotapi.extensions.api.files.downloadFile"))
|
||||||
suspend fun TelegramBot.downloadFile(
|
suspend fun TelegramBot.downloadFile(
|
||||||
fileId: FileId
|
fileId: FileId
|
||||||
): ByteArray = downloadFile(
|
): ByteArray = downloadFile(fileId)
|
||||||
getFileAdditionalInfo(fileId)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
@Deprecated("Replaced", ReplaceWith("downloadFile", "dev.inmo.tgbotapi.extensions.api.files.downloadFile"))
|
||||||
suspend fun TelegramBot.downloadFile(
|
suspend fun TelegramBot.downloadFile(
|
||||||
file: TelegramMediaFile
|
file: TelegramMediaFile
|
||||||
): ByteArray = downloadFile(
|
): ByteArray = downloadFile(file)
|
||||||
getFileAdditionalInfo(file)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
@Deprecated("Replaced", ReplaceWith("downloadFile", "dev.inmo.tgbotapi.extensions.api.files.downloadFile"))
|
||||||
suspend fun TelegramBot.downloadFile(
|
suspend fun TelegramBot.downloadFile(
|
||||||
file: MediaContent
|
file: MediaContent
|
||||||
): ByteArray = downloadFile(
|
): ByteArray = downloadFile(file)
|
||||||
getFileAdditionalInfo(file.media)
|
|
||||||
)
|
|
||||||
|
@ -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.micro_utils.coroutines.doOutsideOfCoroutine
|
||||||
import dev.inmo.tgbotapi.bot.TelegramBot
|
import dev.inmo.tgbotapi.bot.TelegramBot
|
||||||
import dev.inmo.tgbotapi.extensions.api.get.getFileAdditionalInfo
|
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.requests.abstracts.FileId
|
||||||
import dev.inmo.tgbotapi.types.files.PathedFile
|
import dev.inmo.tgbotapi.types.files.PathedFile
|
||||||
import dev.inmo.tgbotapi.types.files.abstracts.TelegramMediaFile
|
import dev.inmo.tgbotapi.types.files.abstracts.TelegramMediaFile
|
||||||
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaContent
|
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 java.io.File
|
||||||
|
import kotlin.coroutines.coroutineContext
|
||||||
|
|
||||||
suspend fun TelegramBot.downloadFile(
|
suspend fun TelegramBot.downloadFile(
|
||||||
filePath: String,
|
filePath: String,
|
||||||
destFile: File
|
destFile: File
|
||||||
): File {
|
): File {
|
||||||
val bytes = downloadFile(filePath)
|
val readChannel = downloadFileStream(filePath)
|
||||||
destFile.deleteRecursively()
|
|
||||||
|
|
||||||
|
destFile.deleteRecursively()
|
||||||
|
destFile.parentFile.mkdirs()
|
||||||
doOutsideOfCoroutine { destFile.createNewFile() }
|
doOutsideOfCoroutine { destFile.createNewFile() }
|
||||||
destFile.writeBytes(bytes)
|
|
||||||
|
readChannel.copyTo(destFile.writeChannel(coroutineContext.job))
|
||||||
|
|
||||||
return destFile
|
return destFile
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user