mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2025-09-02 22:59:48 +00:00
improve support of local bot api servers
This commit is contained in:
@@ -2,6 +2,11 @@
|
||||
|
||||
## 27.1.0
|
||||
|
||||
* `Core`:
|
||||
* Improve support of local bot api servers files. Next call factories will try to resolve file locally before real call:
|
||||
* [DownloadFileRequestCallFactory.kt](tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/DownloadFileRequestCallFactory.kt) (for [DownloadFile.kt](tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/DownloadFile.kt) as well)
|
||||
* [DownloadFileChannelRequestCallFactory.kt](tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/bot/ktor/base/DownloadFileChannelRequestCallFactory.kt) (for [DownloadFileStream.kt](tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/DownloadFileStream.kt) as well)
|
||||
|
||||
## 27.0.0
|
||||
|
||||
**THIS UPDATE MAY CONTAIN BREAKING CHANGES. IN CASE OF ANY MIGRATION PROBLEMS FEEL FREE TO ASK IN [OUR CHAT](https://t.me/ktgbotapi_chat)**
|
||||
|
@@ -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()
|
||||
|
@@ -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")
|
||||
|
@@ -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?
|
@@ -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
|
@@ -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 }
|
@@ -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 }
|
@@ -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 }
|
@@ -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 }
|
@@ -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 }
|
@@ -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 }
|
@@ -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 }
|
Reference in New Issue
Block a user