mirror of
https://github.com/InsanusMokrassar/TelegramBotApiLibraries.git
synced 2024-11-17 22:03:51 +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.MessageIdentifier
|
||||||
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaContent
|
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaContent
|
||||||
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
|
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.cancel
|
||||||
|
import io.ktor.utils.io.core.Input
|
||||||
|
|
||||||
class DefaultMessageContentCache<T>(
|
class DefaultMessageContentCache(
|
||||||
private val bot: TelegramBot,
|
private val bot: TelegramBot,
|
||||||
private val simpleMessageContentCache: MessageContentCache,
|
private val filesRefreshingChatId: ChatId,
|
||||||
private val messagesFilesCache: MessagesFilesCache,
|
private val simpleMessageContentCache: MessagesSimpleCache = InMemoryMessagesSimpleCache(),
|
||||||
private val filesRefreshingChatId: ChatId
|
private val messagesFilesCache: MessagesFilesCache = InMemoryMessagesFilesCache()
|
||||||
) : MessageContentCache {
|
) : MessageContentCache {
|
||||||
override suspend fun save(chatId: ChatId, messageId: MessageIdentifier, content: MessageContent): Boolean {
|
override suspend fun save(chatId: ChatId, messageId: MessageIdentifier, content: MessageContent): Boolean {
|
||||||
runCatching {
|
return when (content) {
|
||||||
if (content is MediaContent) {
|
is MediaContent -> {
|
||||||
val extendedInfo = bot.execute(
|
val extendedInfo = bot.execute(
|
||||||
GetFile(content.media.fileId)
|
GetFile(content.media.fileId)
|
||||||
)
|
)
|
||||||
@ -29,15 +32,33 @@ class DefaultMessageContentCache<T>(
|
|||||||
extendedInfo.filePath
|
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 {
|
}.onFailure {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return simpleMessageContentCache.save(
|
return simpleMessageContentCache.runCatching {
|
||||||
chatId, messageId, content
|
set(chatId, messageId, content)
|
||||||
)
|
}.isSuccess
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun get(chatId: ChatId, messageId: MessageIdentifier): MessageContent? {
|
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
|
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.ChatId
|
||||||
import dev.inmo.tgbotapi.types.MessageIdentifier
|
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.types.message.content.abstracts.MessageContent
|
||||||
|
import io.ktor.utils.io.core.Input
|
||||||
|
|
||||||
interface MessageContentCache {
|
interface MessageContentCache {
|
||||||
suspend fun save(chatId: ChatId, messageId: MessageIdentifier, content: MessageContent): Boolean
|
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 get(chatId: ChatId, messageId: MessageIdentifier): MessageContent?
|
||||||
suspend fun contains(chatId: ChatId, messageId: MessageIdentifier): Boolean
|
suspend fun contains(chatId: ChatId, messageId: MessageIdentifier): Boolean
|
||||||
suspend fun remove(chatId: ChatId, messageId: MessageIdentifier)
|
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.ChatId
|
||||||
import dev.inmo.tgbotapi.types.MessageIdentifier
|
import dev.inmo.tgbotapi.types.MessageIdentifier
|
||||||
import dev.inmo.tgbotapi.utils.ByteReadChannelAllocator
|
|
||||||
import dev.inmo.tgbotapi.utils.StorageFile
|
import dev.inmo.tgbotapi.utils.StorageFile
|
||||||
import io.ktor.util.toByteArray
|
import io.ktor.utils.io.core.*
|
||||||
import io.ktor.utils.io.ByteReadChannel
|
|
||||||
|
|
||||||
interface MessagesFilesCache {
|
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 get(chatId: ChatId, messageIdentifier: MessageIdentifier): StorageFile?
|
||||||
suspend fun remove(chatId: ChatId, messageIdentifier: MessageIdentifier)
|
suspend fun remove(chatId: ChatId, messageIdentifier: MessageIdentifier)
|
||||||
suspend fun contains(chatId: ChatId, messageIdentifier: MessageIdentifier): Boolean
|
suspend fun contains(chatId: ChatId, messageIdentifier: MessageIdentifier): Boolean
|
||||||
@ -26,11 +29,11 @@ class InMemoryMessagesFilesCache : MessagesFilesCache {
|
|||||||
chatId: ChatId,
|
chatId: ChatId,
|
||||||
messageIdentifier: MessageIdentifier,
|
messageIdentifier: MessageIdentifier,
|
||||||
filename: String,
|
filename: String,
|
||||||
byteReadChannelAllocator: ByteReadChannelAllocator
|
inputAllocator: suspend () -> Input
|
||||||
) {
|
) {
|
||||||
map[chatId to messageIdentifier] = StorageFile(
|
map[chatId to messageIdentifier] = StorageFile(
|
||||||
filename,
|
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.ChatId
|
||||||
import dev.inmo.tgbotapi.types.MessageIdentifier
|
import dev.inmo.tgbotapi.types.MessageIdentifier
|
||||||
import dev.inmo.tgbotapi.utils.*
|
import dev.inmo.tgbotapi.utils.*
|
||||||
|
import io.ktor.utils.io.core.Input
|
||||||
import io.ktor.utils.io.core.copyTo
|
import io.ktor.utils.io.core.copyTo
|
||||||
import io.ktor.utils.io.streams.asInput
|
import io.ktor.utils.io.streams.asInput
|
||||||
import io.ktor.utils.io.streams.asOutput
|
import io.ktor.utils.io.streams.asOutput
|
||||||
@ -42,13 +43,13 @@ class InFilesMessagesFilesCache(
|
|||||||
chatId: ChatId,
|
chatId: ChatId,
|
||||||
messageIdentifier: MessageIdentifier,
|
messageIdentifier: MessageIdentifier,
|
||||||
filename: String,
|
filename: String,
|
||||||
byteReadChannelAllocator: ByteReadChannelAllocator
|
inputAllocator: suspend () -> Input
|
||||||
) {
|
) {
|
||||||
val fullFileName = fileName(chatId, messageIdentifier, filename)
|
val fullFileName = fileName(chatId, messageIdentifier, filename)
|
||||||
val file = File(folderFile, fullFileName).apply {
|
val file = File(folderFile, fullFileName).apply {
|
||||||
delete()
|
delete()
|
||||||
}
|
}
|
||||||
byteReadChannelAllocator.invoke().asInput().use { input ->
|
inputAllocator().use { input ->
|
||||||
file.outputStream().asOutput().use { output ->
|
file.outputStream().asOutput().use { output ->
|
||||||
input.copyTo(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.*
|
||||||
import dev.inmo.micro_utils.repos.mappers.withMapper
|
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.ChatId
|
||||||
import dev.inmo.tgbotapi.types.MessageIdentifier
|
import dev.inmo.tgbotapi.types.MessageIdentifier
|
||||||
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
|
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
|
||||||
@ -16,23 +16,21 @@ import kotlin.jvm.JvmName
|
|||||||
|
|
||||||
class SimpleKeyValueMessageContentCache(
|
class SimpleKeyValueMessageContentCache(
|
||||||
private val keyValueRepo: KeyValueRepo<Pair<ChatId, MessageIdentifier>, MessageContent>
|
private val keyValueRepo: KeyValueRepo<Pair<ChatId, MessageIdentifier>, MessageContent>
|
||||||
) : MessageContentCache {
|
) : MessagesSimpleCache {
|
||||||
override suspend fun save(chatId: ChatId, messageId: MessageIdentifier, content: MessageContent): Boolean {
|
override suspend fun set(chatId: ChatId, messageIdentifier: MessageIdentifier, content: MessageContent) {
|
||||||
return keyValueRepo.runCatching {
|
keyValueRepo.set(chatId to messageIdentifier, content)
|
||||||
set(chatId to messageId, content)
|
|
||||||
}.isSuccess
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun get(chatId: ChatId, messageId: MessageIdentifier): MessageContent? {
|
override suspend fun get(chatId: ChatId, messageIdentifier: MessageIdentifier): MessageContent? {
|
||||||
return keyValueRepo.get(chatId to messageId)
|
return keyValueRepo.get(chatId to messageIdentifier)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun contains(chatId: ChatId, messageId: MessageIdentifier): Boolean {
|
override suspend fun contains(chatId: ChatId, messageIdentifier: MessageIdentifier): Boolean {
|
||||||
return keyValueRepo.contains(chatId to messageId)
|
return keyValueRepo.contains(chatId to messageIdentifier)
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun remove(chatId: ChatId, messageId: MessageIdentifier) {
|
override suspend fun remove(chatId: ChatId, messageIdentifier: MessageIdentifier) {
|
||||||
keyValueRepo.unset(chatId to messageId)
|
keyValueRepo.unset(chatId to messageIdentifier)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user