mirror of
https://github.com/InsanusMokrassar/TelegramBotApiLibraries.git
synced 2024-12-22 16:47:16 +00:00
simple fixes
This commit is contained in:
parent
7e37a43904
commit
cde0a11c1b
@ -10,17 +10,20 @@ import dev.inmo.tgbotapi.types.InputMedia.*
|
||||
import dev.inmo.tgbotapi.types.MessageIdentifier
|
||||
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaContent
|
||||
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
|
||||
import dev.inmo.tgbotapi.utils.StorageFile
|
||||
import dev.inmo.tgbotapi.utils.asInput
|
||||
import io.ktor.utils.io.cancel
|
||||
import io.ktor.utils.io.core.Input
|
||||
|
||||
class DefaultMessageContentCache<T>(
|
||||
class DefaultMessageContentCache(
|
||||
private val bot: TelegramBot,
|
||||
private val simpleMessageContentCache: MessageContentCache,
|
||||
private val messagesFilesCache: MessagesFilesCache,
|
||||
private val filesRefreshingChatId: ChatId
|
||||
private val filesRefreshingChatId: ChatId,
|
||||
private val simpleMessageContentCache: MessagesSimpleCache = InMemoryMessagesSimpleCache(),
|
||||
private val messagesFilesCache: MessagesFilesCache = InMemoryMessagesFilesCache()
|
||||
) : MessageContentCache {
|
||||
override suspend fun save(chatId: ChatId, messageId: MessageIdentifier, content: MessageContent): Boolean {
|
||||
runCatching {
|
||||
if (content is MediaContent) {
|
||||
return when (content) {
|
||||
is MediaContent -> {
|
||||
val extendedInfo = bot.execute(
|
||||
GetFile(content.media.fileId)
|
||||
)
|
||||
@ -29,15 +32,33 @@ class DefaultMessageContentCache<T>(
|
||||
extendedInfo.filePath
|
||||
)
|
||||
)
|
||||
messagesFilesCache.set(chatId, messageId, extendedInfo.fileName, allocator)
|
||||
|
||||
save(chatId, messageId, content, extendedInfo.fileName) {
|
||||
allocator.invoke().asInput()
|
||||
}
|
||||
}
|
||||
else -> simpleMessageContentCache.runCatching {
|
||||
set(chatId, messageId, content)
|
||||
}.isSuccess
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun save(
|
||||
chatId: ChatId,
|
||||
messageId: MessageIdentifier,
|
||||
content: MediaContent,
|
||||
filename: String,
|
||||
inputAllocator: suspend () -> Input
|
||||
): Boolean {
|
||||
runCatching {
|
||||
messagesFilesCache.set(chatId, messageId, filename, inputAllocator)
|
||||
}.onFailure {
|
||||
return false
|
||||
}
|
||||
|
||||
return simpleMessageContentCache.save(
|
||||
chatId, messageId, content
|
||||
)
|
||||
return simpleMessageContentCache.runCatching {
|
||||
set(chatId, messageId, content)
|
||||
}.isSuccess
|
||||
}
|
||||
|
||||
override suspend fun get(chatId: ChatId, messageId: MessageIdentifier): MessageContent? {
|
||||
@ -101,7 +122,7 @@ class DefaultMessageContentCache<T>(
|
||||
}
|
||||
)
|
||||
|
||||
simpleMessageContentCache.save(chatId, messageId, newContent.content)
|
||||
simpleMessageContentCache.set(chatId, messageId, newContent.content)
|
||||
return newContent.content
|
||||
}
|
||||
}
|
||||
|
@ -2,10 +2,19 @@ package dev.inmo.tgbotapi.libraries.cache.media.common
|
||||
|
||||
import dev.inmo.tgbotapi.types.ChatId
|
||||
import dev.inmo.tgbotapi.types.MessageIdentifier
|
||||
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaContent
|
||||
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
|
||||
import io.ktor.utils.io.core.Input
|
||||
|
||||
interface MessageContentCache {
|
||||
suspend fun save(chatId: ChatId, messageId: MessageIdentifier, content: MessageContent): Boolean
|
||||
suspend fun save(
|
||||
chatId: ChatId,
|
||||
messageId: MessageIdentifier,
|
||||
content: MediaContent,
|
||||
filename: String,
|
||||
inputAllocator: suspend () -> Input
|
||||
): Boolean
|
||||
suspend fun get(chatId: ChatId, messageId: MessageIdentifier): MessageContent?
|
||||
suspend fun contains(chatId: ChatId, messageId: MessageIdentifier): Boolean
|
||||
suspend fun remove(chatId: ChatId, messageId: MessageIdentifier)
|
||||
|
@ -2,13 +2,16 @@ package dev.inmo.tgbotapi.libraries.cache.media.common
|
||||
|
||||
import dev.inmo.tgbotapi.types.ChatId
|
||||
import dev.inmo.tgbotapi.types.MessageIdentifier
|
||||
import dev.inmo.tgbotapi.utils.ByteReadChannelAllocator
|
||||
import dev.inmo.tgbotapi.utils.StorageFile
|
||||
import io.ktor.util.toByteArray
|
||||
import io.ktor.utils.io.ByteReadChannel
|
||||
import io.ktor.utils.io.core.*
|
||||
|
||||
interface MessagesFilesCache {
|
||||
suspend fun set(chatId: ChatId, messageIdentifier: MessageIdentifier, filename: String, byteReadChannelAllocator: ByteReadChannelAllocator)
|
||||
suspend fun set(
|
||||
chatId: ChatId,
|
||||
messageIdentifier: MessageIdentifier,
|
||||
filename: String,
|
||||
inputAllocator: suspend () -> Input
|
||||
)
|
||||
suspend fun get(chatId: ChatId, messageIdentifier: MessageIdentifier): StorageFile?
|
||||
suspend fun remove(chatId: ChatId, messageIdentifier: MessageIdentifier)
|
||||
suspend fun contains(chatId: ChatId, messageIdentifier: MessageIdentifier): Boolean
|
||||
@ -26,11 +29,11 @@ class InMemoryMessagesFilesCache : MessagesFilesCache {
|
||||
chatId: ChatId,
|
||||
messageIdentifier: MessageIdentifier,
|
||||
filename: String,
|
||||
byteReadChannelAllocator: ByteReadChannelAllocator
|
||||
inputAllocator: suspend () -> Input
|
||||
) {
|
||||
map[chatId to messageIdentifier] = StorageFile(
|
||||
filename,
|
||||
byteReadChannelAllocator.invoke()
|
||||
inputAllocator().readBytes()
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,48 @@
|
||||
package dev.inmo.tgbotapi.libraries.cache.media.common
|
||||
|
||||
import dev.inmo.tgbotapi.types.ChatId
|
||||
import dev.inmo.tgbotapi.types.MessageIdentifier
|
||||
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
|
||||
import dev.inmo.tgbotapi.utils.StorageFile
|
||||
import io.ktor.utils.io.core.*
|
||||
|
||||
interface MessagesSimpleCache {
|
||||
suspend fun set(
|
||||
chatId: ChatId,
|
||||
messageIdentifier: MessageIdentifier,
|
||||
content: MessageContent
|
||||
)
|
||||
suspend fun get(chatId: ChatId, messageIdentifier: MessageIdentifier): MessageContent?
|
||||
suspend fun remove(chatId: ChatId, messageIdentifier: MessageIdentifier)
|
||||
suspend fun contains(chatId: ChatId, messageIdentifier: MessageIdentifier): Boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* It is not recommended to use in production realization of [MessagesFilesCache] which has been created for fast
|
||||
* start of application creation with usage of [MessageContentCache] with aim to replace this realization by some
|
||||
* disks-oriented one
|
||||
*/
|
||||
class InMemoryMessagesSimpleCache : MessagesSimpleCache {
|
||||
private val map = mutableMapOf<Pair<ChatId, MessageIdentifier>, MessageContent>()
|
||||
|
||||
override suspend fun set(
|
||||
chatId: ChatId,
|
||||
messageIdentifier: MessageIdentifier,
|
||||
content: MessageContent
|
||||
) {
|
||||
map[chatId to messageIdentifier] = content
|
||||
}
|
||||
|
||||
override suspend fun get(chatId: ChatId, messageIdentifier: MessageIdentifier): MessageContent? {
|
||||
return map[chatId to messageIdentifier]
|
||||
}
|
||||
|
||||
override suspend fun remove(chatId: ChatId, messageIdentifier: MessageIdentifier) {
|
||||
map.remove(chatId to messageIdentifier)
|
||||
}
|
||||
|
||||
override suspend fun contains(chatId: ChatId, messageIdentifier: MessageIdentifier): Boolean {
|
||||
return map.contains(chatId to messageIdentifier)
|
||||
}
|
||||
|
||||
}
|
@ -3,6 +3,7 @@ package dev.inmo.tgbotapi.libraries.cache.media.common
|
||||
import dev.inmo.tgbotapi.types.ChatId
|
||||
import dev.inmo.tgbotapi.types.MessageIdentifier
|
||||
import dev.inmo.tgbotapi.utils.*
|
||||
import io.ktor.utils.io.core.Input
|
||||
import io.ktor.utils.io.core.copyTo
|
||||
import io.ktor.utils.io.streams.asInput
|
||||
import io.ktor.utils.io.streams.asOutput
|
||||
@ -42,13 +43,13 @@ class InFilesMessagesFilesCache(
|
||||
chatId: ChatId,
|
||||
messageIdentifier: MessageIdentifier,
|
||||
filename: String,
|
||||
byteReadChannelAllocator: ByteReadChannelAllocator
|
||||
inputAllocator: suspend () -> Input
|
||||
) {
|
||||
val fullFileName = fileName(chatId, messageIdentifier, filename)
|
||||
val file = File(folderFile, fullFileName).apply {
|
||||
delete()
|
||||
}
|
||||
byteReadChannelAllocator.invoke().asInput().use { input ->
|
||||
inputAllocator().use { input ->
|
||||
file.outputStream().asOutput().use { output ->
|
||||
input.copyTo(output)
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package dev.inmo.tgbotapi.libraries.cache.media.micro_utils
|
||||
|
||||
import dev.inmo.micro_utils.repos.*
|
||||
import dev.inmo.micro_utils.repos.mappers.withMapper
|
||||
import dev.inmo.tgbotapi.libraries.cache.media.common.MessageContentCache
|
||||
import dev.inmo.tgbotapi.libraries.cache.media.common.MessagesSimpleCache
|
||||
import dev.inmo.tgbotapi.types.ChatId
|
||||
import dev.inmo.tgbotapi.types.MessageIdentifier
|
||||
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
|
||||
@ -16,23 +16,21 @@ import kotlin.jvm.JvmName
|
||||
|
||||
class SimpleKeyValueMessageContentCache(
|
||||
private val keyValueRepo: KeyValueRepo<Pair<ChatId, MessageIdentifier>, MessageContent>
|
||||
) : MessageContentCache {
|
||||
override suspend fun save(chatId: ChatId, messageId: MessageIdentifier, content: MessageContent): Boolean {
|
||||
return keyValueRepo.runCatching {
|
||||
set(chatId to messageId, content)
|
||||
}.isSuccess
|
||||
) : MessagesSimpleCache {
|
||||
override suspend fun set(chatId: ChatId, messageIdentifier: MessageIdentifier, content: MessageContent) {
|
||||
keyValueRepo.set(chatId to messageIdentifier, content)
|
||||
}
|
||||
|
||||
override suspend fun get(chatId: ChatId, messageId: MessageIdentifier): MessageContent? {
|
||||
return keyValueRepo.get(chatId to messageId)
|
||||
override suspend fun get(chatId: ChatId, messageIdentifier: MessageIdentifier): MessageContent? {
|
||||
return keyValueRepo.get(chatId to messageIdentifier)
|
||||
}
|
||||
|
||||
override suspend fun contains(chatId: ChatId, messageId: MessageIdentifier): Boolean {
|
||||
return keyValueRepo.contains(chatId to messageId)
|
||||
override suspend fun contains(chatId: ChatId, messageIdentifier: MessageIdentifier): Boolean {
|
||||
return keyValueRepo.contains(chatId to messageIdentifier)
|
||||
}
|
||||
|
||||
override suspend fun remove(chatId: ChatId, messageId: MessageIdentifier) {
|
||||
keyValueRepo.unset(chatId to messageId)
|
||||
override suspend fun remove(chatId: ChatId, messageIdentifier: MessageIdentifier) {
|
||||
keyValueRepo.unset(chatId to messageIdentifier)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user