1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-29 21:48:43 +00:00
This commit is contained in:
InsanusMokrassar 2021-08-09 23:24:12 +06:00
parent b475976ff1
commit c66d64adbc
3 changed files with 12 additions and 14 deletions

View File

@ -3,7 +3,7 @@ package dev.inmo.tgbotapi.bot.Ktor.base
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
import dev.inmo.tgbotapi.utils.InputStreamAllocator import dev.inmo.tgbotapi.utils.ByteReadChannelAllocator
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import io.ktor.client.call.receive import io.ktor.client.call.receive
@ -23,7 +23,7 @@ object DownloadFileChannelRequestCallFactory : KtorCallFactory {
): T? = (request as? DownloadFileStream) ?.let { ): T? = (request as? DownloadFileStream) ?.let {
val fullUrl = urlsKeeper.createFileLinkUrl(it.filePath) val fullUrl = urlsKeeper.createFileLinkUrl(it.filePath)
return InputStreamAllocator { return ByteReadChannelAllocator {
val scope = CoroutineScope(coroutineContext) val scope = CoroutineScope(coroutineContext)
val outChannel = ByteChannel() val outChannel = ByteChannel()
scope.launch { scope.launch {

View File

@ -1,15 +1,15 @@
package dev.inmo.tgbotapi.requests package dev.inmo.tgbotapi.requests
import dev.inmo.tgbotapi.requests.abstracts.Request import dev.inmo.tgbotapi.requests.abstracts.Request
import dev.inmo.tgbotapi.utils.InputStreamAllocator import dev.inmo.tgbotapi.utils.ByteReadChannelAllocator
import dev.inmo.tgbotapi.utils.InputStreamAllocatorSerializer import dev.inmo.tgbotapi.utils.ByteReadChannelAllocatorSerializer
import kotlinx.serialization.DeserializationStrategy import kotlinx.serialization.DeserializationStrategy
class DownloadFileStream( class DownloadFileStream(
val filePath: String val filePath: String
) : Request<InputStreamAllocator> { ) : Request<ByteReadChannelAllocator> {
override fun method(): String = filePath override fun method(): String = filePath
override val resultDeserializer: DeserializationStrategy<InputStreamAllocator> override val resultDeserializer: DeserializationStrategy<ByteReadChannelAllocator>
get() = InputStreamAllocatorSerializer get() = ByteReadChannelAllocatorSerializer
} }

View File

@ -1,22 +1,20 @@
package dev.inmo.tgbotapi.utils package dev.inmo.tgbotapi.utils
import dev.inmo.micro_utils.common.ByteArrayAllocatorSerializer import dev.inmo.micro_utils.common.ByteArrayAllocatorSerializer
import io.ktor.util.toByteArray
import io.ktor.utils.io.ByteReadChannel import io.ktor.utils.io.ByteReadChannel
import kotlinx.coroutines.*
import kotlinx.serialization.KSerializer import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder import kotlinx.serialization.encoding.Encoder
fun interface InputStreamAllocator { fun interface ByteReadChannelAllocator {
suspend operator fun invoke(): ByteReadChannel suspend operator fun invoke(): ByteReadChannel
} }
object InputStreamAllocatorSerializer : KSerializer<InputStreamAllocator> { object ByteReadChannelAllocatorSerializer : KSerializer<ByteReadChannelAllocator> {
override val descriptor: SerialDescriptor = ByteArrayAllocatorSerializer.descriptor override val descriptor: SerialDescriptor = ByteArrayAllocatorSerializer.descriptor
override fun serialize(encoder: Encoder, value: InputStreamAllocator) { override fun serialize(encoder: Encoder, value: ByteReadChannelAllocator) {
TODO("Not yet implemented") TODO("Not yet implemented")
// ByteArrayAllocatorSerializer.serialize( // ByteArrayAllocatorSerializer.serialize(
// encoder // encoder
@ -24,8 +22,8 @@ object InputStreamAllocatorSerializer : KSerializer<InputStreamAllocator> {
// } // }
} }
override fun deserialize(decoder: Decoder): InputStreamAllocator { override fun deserialize(decoder: Decoder): ByteReadChannelAllocator {
val byteArrayAllocator = ByteArrayAllocatorSerializer.deserialize(decoder) val byteArrayAllocator = ByteArrayAllocatorSerializer.deserialize(decoder)
return InputStreamAllocator { ByteReadChannel(byteArrayAllocator()) } return ByteReadChannelAllocator { ByteReadChannel(byteArrayAllocator()) }
} }
} }