mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2025-09-04 07:39:39 +00:00
improve support of local bot api servers
This commit is contained in:
@@ -2,6 +2,11 @@
|
|||||||
|
|
||||||
## 27.1.0
|
## 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
|
## 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)**
|
**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
|
package dev.inmo.tgbotapi.bot.ktor.base
|
||||||
|
|
||||||
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
|
||||||
@@ -22,7 +21,17 @@ object DownloadFileChannelRequestCallFactory : KtorCallFactory {
|
|||||||
request: Request<T>,
|
request: Request<T>,
|
||||||
jsonFormatter: Json
|
jsonFormatter: Json
|
||||||
): T? = (request as? DownloadFileStream) ?.let {
|
): 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")
|
@Suppress("UNCHECKED_CAST")
|
||||||
ByteReadChannelAllocator {
|
ByteReadChannelAllocator {
|
||||||
@@ -30,8 +39,7 @@ object DownloadFileChannelRequestCallFactory : KtorCallFactory {
|
|||||||
val outChannel = ByteChannel()
|
val outChannel = ByteChannel()
|
||||||
scope.launch {
|
scope.launch {
|
||||||
runCatching {
|
runCatching {
|
||||||
val response = client.get(fullUrl)
|
val channel: ByteReadChannel = bodyChannelAllocator()
|
||||||
val channel: ByteReadChannel = response.bodyAsChannel()
|
|
||||||
channel.copyAndClose(outChannel)
|
channel.copyAndClose(outChannel)
|
||||||
}
|
}
|
||||||
scope.cancel()
|
scope.cancel()
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
package dev.inmo.tgbotapi.bot.ktor.base
|
package dev.inmo.tgbotapi.bot.ktor.base
|
||||||
|
|
||||||
|
import dev.inmo.micro_utils.common.bytes
|
||||||
import dev.inmo.micro_utils.coroutines.safely
|
import dev.inmo.micro_utils.coroutines.safely
|
||||||
import dev.inmo.tgbotapi.bot.ktor.KtorCallFactory
|
import dev.inmo.tgbotapi.bot.ktor.KtorCallFactory
|
||||||
import dev.inmo.tgbotapi.requests.DownloadFile
|
import dev.inmo.tgbotapi.requests.DownloadFile
|
||||||
@@ -19,6 +20,10 @@ object DownloadFileRequestCallFactory : KtorCallFactory {
|
|||||||
request: Request<T>,
|
request: Request<T>,
|
||||||
jsonFormatter: Json,
|
jsonFormatter: Json,
|
||||||
): T? = (request as? DownloadFile)?.let {
|
): T? = (request as? DownloadFile)?.let {
|
||||||
|
resolveFile(it.filePath) ?.let {
|
||||||
|
return@makeCall it.bytes() as T // Always ByteArray
|
||||||
|
}
|
||||||
|
|
||||||
val fullUrl = urlsKeeper.createFileLinkUrl(it.filePath)
|
val fullUrl = urlsKeeper.createFileLinkUrl(it.filePath)
|
||||||
|
|
||||||
@Suppress("UNCHECKED_CAST")
|
@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