From a404a6c59cc31a3d8c02e46a0d0000fca61a1cbc Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 11 Apr 2022 21:56:47 +0600 Subject: [PATCH] fixes in DownloadFIleChannelRequestCallFactory --- .../base/DownloadFileChannelRequestCallFactory.kt | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/Ktor/base/DownloadFileChannelRequestCallFactory.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/Ktor/base/DownloadFileChannelRequestCallFactory.kt index 3532ec5217..46cf0c1aec 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/Ktor/base/DownloadFileChannelRequestCallFactory.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/Ktor/base/DownloadFileChannelRequestCallFactory.kt @@ -1,6 +1,6 @@ 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.requests.DownloadFileStream import dev.inmo.tgbotapi.requests.abstracts.Request @@ -25,13 +25,16 @@ object DownloadFileChannelRequestCallFactory : KtorCallFactory { val fullUrl = urlsKeeper.createFileLinkUrl(it.filePath) ByteReadChannelAllocator { - val scope = CoroutineScope(coroutineContext) + val scope = CoroutineScope(currentCoroutineContext() + SupervisorJob()) val outChannel = ByteChannel() - scope.launchSafelyWithoutExceptions { - client.get(fullUrl).execute { httpResponse -> - val channel: ByteReadChannel = httpResponse.receive() - channel.copyAndClose(outChannel) + scope.launch { + runCatchingSafely { + client.get(fullUrl).execute { httpResponse -> + val channel: ByteReadChannel = httpResponse.receive() + channel.copyAndClose(outChannel) + } } + scope.cancel() } outChannel } as T