1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-22 08:13:47 +00:00

fixes in DownloadFIleChannelRequestCallFactory

This commit is contained in:
InsanusMokrassar 2022-04-11 21:56:47 +06:00
parent 1089c716f3
commit a404a6c59c

View File

@ -1,6 +1,6 @@
package dev.inmo.tgbotapi.bot.Ktor.base package dev.inmo.tgbotapi.bot.Ktor.base
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions import dev.inmo.micro_utils.coroutines.*
import dev.inmo.tgbotapi.bot.Ktor.KtorCallFactory import dev.inmo.tgbotapi.bot.Ktor.KtorCallFactory
import dev.inmo.tgbotapi.requests.DownloadFileStream import dev.inmo.tgbotapi.requests.DownloadFileStream
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
@ -25,14 +25,17 @@ object DownloadFileChannelRequestCallFactory : KtorCallFactory {
val fullUrl = urlsKeeper.createFileLinkUrl(it.filePath) val fullUrl = urlsKeeper.createFileLinkUrl(it.filePath)
ByteReadChannelAllocator { ByteReadChannelAllocator {
val scope = CoroutineScope(coroutineContext) val scope = CoroutineScope(currentCoroutineContext() + SupervisorJob())
val outChannel = ByteChannel() val outChannel = ByteChannel()
scope.launchSafelyWithoutExceptions { scope.launch {
runCatchingSafely {
client.get<HttpStatement>(fullUrl).execute { httpResponse -> client.get<HttpStatement>(fullUrl).execute { httpResponse ->
val channel: ByteReadChannel = httpResponse.receive() val channel: ByteReadChannel = httpResponse.receive()
channel.copyAndClose(outChannel) channel.copyAndClose(outChannel)
} }
} }
scope.cancel()
}
outChannel outChannel
} as T } as T
} }