1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-09-03 15:19:30 +00:00

improve support of local bot api servers

This commit is contained in:
2025-07-25 21:35:38 +06:00
parent 5e5dc6b36b
commit cec5a0af12
13 changed files with 110 additions and 4 deletions

View File

@@ -1,6 +1,5 @@
package dev.inmo.tgbotapi.bot.ktor.base
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
@@ -22,7 +21,17 @@ object DownloadFileChannelRequestCallFactory : KtorCallFactory {
request: Request<T>,
jsonFormatter: Json
): T? = (request as? DownloadFileStream) ?.let {
val fullUrl = urlsKeeper.createFileLinkUrl(it.filePath)
val bodyChannelAllocator: suspend () -> ByteReadChannel = resolveFile(it.filePath) ?.let {
{
byteReadChannel(it)
}
} ?: let { _ ->
val fullUrl = urlsKeeper.createFileLinkUrl(it.filePath)
return@let {
val response = client.get(fullUrl)
response.bodyAsChannel()
}
}
@Suppress("UNCHECKED_CAST")
ByteReadChannelAllocator {
@@ -30,8 +39,7 @@ object DownloadFileChannelRequestCallFactory : KtorCallFactory {
val outChannel = ByteChannel()
scope.launch {
runCatching {
val response = client.get(fullUrl)
val channel: ByteReadChannel = response.bodyAsChannel()
val channel: ByteReadChannel = bodyChannelAllocator()
channel.copyAndClose(outChannel)
}
scope.cancel()

View File

@@ -1,5 +1,6 @@
package dev.inmo.tgbotapi.bot.ktor.base
import dev.inmo.micro_utils.common.bytes
import dev.inmo.micro_utils.coroutines.safely
import dev.inmo.tgbotapi.bot.ktor.KtorCallFactory
import dev.inmo.tgbotapi.requests.DownloadFile
@@ -19,6 +20,10 @@ object DownloadFileRequestCallFactory : KtorCallFactory {
request: Request<T>,
jsonFormatter: Json,
): T? = (request as? DownloadFile)?.let {
resolveFile(it.filePath) ?.let {
return@makeCall it.bytes() as T // Always ByteArray
}
val fullUrl = urlsKeeper.createFileLinkUrl(it.filePath)
@Suppress("UNCHECKED_CAST")

View File

@@ -0,0 +1,10 @@
package dev.inmo.tgbotapi.bot.ktor.base
import dev.inmo.micro_utils.common.MPPFile
import dev.inmo.micro_utils.ktor.common.input
import io.ktor.utils.io.ByteReadChannel
internal fun byteReadChannel(file: MPPFile): ByteReadChannel {
return ByteReadChannel(file.input())
}
internal expect fun resolveFile(filename: String): MPPFile?

View File

@@ -0,0 +1,5 @@
package dev.inmo.tgbotapi.bot.ktor.base
import dev.inmo.micro_utils.common.MPPFile
internal actual fun resolveFile(filename: String): MPPFile? = null // on JS in common case there is no opportunity to take file based on its name

View File

@@ -0,0 +1,7 @@
package dev.inmo.tgbotapi.bot.ktor.base
import dev.inmo.micro_utils.common.MPPFile
internal actual fun resolveFile(filename: String): MPPFile? = runCatching {
MPPFile(filename).takeIf { it.exists() && it.isFile }
}.getOrElse { null }

View File

@@ -0,0 +1,11 @@
package dev.inmo.tgbotapi.bot.ktor.base
import dev.inmo.micro_utils.common.MPPFile
import okio.FileSystem
import okio.Path
internal actual fun resolveFile(filename: String): MPPFile? = runCatching {
with(Path) { filename.toPath() }.takeIf {
FileSystem.SYSTEM.exists(it) && FileSystem.SYSTEM.metadata(it).isRegularFile
}
}.getOrElse { null }

View File

@@ -0,0 +1,10 @@
package dev.inmo.tgbotapi.bot.ktor.base
import okio.FileSystem
import okio.Path
internal actual fun resolveFile(filename: String): dev.inmo.micro_utils.common.MPPFile? = runCatching {
with(Path) { filename.toPath() }.takeIf {
FileSystem.SYSTEM.exists(it) && FileSystem.SYSTEM.metadata(it).isRegularFile
}
}.getOrElse { null }

View File

@@ -0,0 +1,11 @@
package dev.inmo.tgbotapi.bot.ktor.base
import dev.inmo.micro_utils.common.MPPFile
import okio.FileSystem
import okio.Path
internal actual fun resolveFile(filename: String): MPPFile? = runCatching {
with(Path) { filename.toPath() }.takeIf {
FileSystem.SYSTEM.exists(it) && FileSystem.SYSTEM.metadata(it).isRegularFile
}
}.getOrElse { null }

View File

@@ -0,0 +1,11 @@
package dev.inmo.tgbotapi.bot.ktor.base
import okio.FileSystem
import okio.Path
import okio.Path.Companion.toPath
internal actual fun resolveFile(filename: String): dev.inmo.micro_utils.common.MPPFile? = runCatching {
with(Path) { filename.toPath() }.takeIf {
FileSystem.SYSTEM.exists(it) && FileSystem.SYSTEM.metadata(it).isRegularFile
}
}.getOrElse { null }

View File

@@ -0,0 +1,12 @@
package dev.inmo.tgbotapi.bot.ktor.base
import dev.inmo.micro_utils.common.MPPFile
import okio.FileSystem
import okio.Path
import okio.Path.Companion.toPath
internal actual fun resolveFile(filename: String): MPPFile? = runCatching {
with(Path) { filename.toPath() }.takeIf {
FileSystem.SYSTEM.exists(it) && FileSystem.SYSTEM.metadata(it).isRegularFile
}
}.getOrElse { null }

View File

@@ -0,0 +1,11 @@
package dev.inmo.tgbotapi.bot.ktor.base
import okio.FileSystem
import okio.Path
import okio.Path.Companion.toPath
internal actual fun resolveFile(filename: String): dev.inmo.micro_utils.common.MPPFile? = runCatching {
with(Path) { filename.toPath() }.takeIf {
FileSystem.SYSTEM.exists(it) && FileSystem.SYSTEM.metadata(it).isRegularFile
}
}.getOrElse { null }