Skip to content

Commit

Permalink
chore(overflow): migration to overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
KurenaiRyu committed Aug 6, 2024
1 parent fa1f81e commit aff1da1
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 382 deletions.
3 changes: 2 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ repositories {
mavenLocal {
content {
includeGroup("com.github.Nyayurn")
includeGroup("top.mrxiaom")
}
}
maven {
Expand Down Expand Up @@ -119,7 +120,7 @@ dependencies {
implementation(platform("net.mamoe:mirai-bom:${Versions.MIRAI}"))
implementation("net.mamoe:mirai-core")
implementation("net.mamoe:mirai-core-utils")
implementation("top.mrxiaom:overflow-core:2.16.0+")
implementation("top.mrxiaom:overflow-core:2.16+")

//tdlib
implementation(platform("it.tdlight:tdlight-java-bom:${Versions.TD_LIGHT}"))
Expand Down
1 change: 0 additions & 1 deletion src/main/kotlin/kurenai/imsyncbot/ConfigProperties.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ data class BotProperties(
)

@Serializable
@Deprecated("No longer needed")
data class QQProperties(
val host: String = "localhost",
val port: Int = 9000,
Expand Down
395 changes: 59 additions & 336 deletions src/main/kotlin/kurenai/imsyncbot/bot/qq/MessageContext.kt

Large diffs are not rendered by default.

44 changes: 25 additions & 19 deletions src/main/kotlin/kurenai/imsyncbot/bot/qq/QQBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import net.mamoe.mirai.contact.nameCardOrNick
import net.mamoe.mirai.event.Event
import net.mamoe.mirai.event.events.*
import net.mamoe.mirai.message.data.At
import net.mamoe.mirai.message.data.MessageChain.Companion.serializeToJsonString
import net.mamoe.mirai.message.data.PlainText
import net.mamoe.mirai.message.data.ids
import net.mamoe.mirai.utils.ConcurrentHashMap
Expand Down Expand Up @@ -70,7 +69,8 @@ class QQBot(
private val groupMessageLockMap = ConcurrentHashMap<Long, Semaphore>()

private suspend fun buildBot(): Bot {
val url = "ws://${qqProperties.host}:${qqProperties.port}"
val url = "ws://${qqProperties.host}:${qqProperties.port}/"
log.info("Connecting to $url")
return BotBuilder.positive(url)
.token(qqProperties.token)
.overrideLogger(log)
Expand Down Expand Up @@ -183,23 +183,24 @@ class QQBot(
try {
when (event) {
is MessageEvent -> {
val json = event.message.serializeToJsonString()
val json = event.message.toString()
when (event) {
is FriendMessageEvent -> {
MessageService.save(
QQMessage().apply {

messageId = event.message.ids[0]
botId = event.bot.id
objId = event.subject.id
sender = event.sender.id
target = event.source.targetId
type = QQMessageType.FRIEND
this.json = json
handled = false
msgTime = event.source.getLocalDateTime()
}
)
CoroutineScope(Dispatchers.IO).launch {
MessageService.save(
QQMessage().apply {
messageId = event.message.ids[0]
botId = event.bot.id
objId = event.subject.id
sender = event.sender.id
target = event.source.targetId
type = QQMessageType.FRIEND
this.json = json
handled = false
msgTime = event.source.getLocalDateTime()
}
)
}

// bot.qqMessageHandler.onFriendMessage(
// PrivateMessageContext(
Expand All @@ -223,7 +224,9 @@ class QQBot(
this.handled = false
this.msgTime = event.source.getLocalDateTime()
}
MessageService.save(message)
CoroutineScope(Dispatchers.IO).launch {
MessageService.save(message)
}

bot.qqMessageHandler.onGroupMessage(
GroupMessageContext(
Expand All @@ -248,7 +251,10 @@ class QQBot(
this.handled = false
this.msgTime = event.source.getLocalDateTime()
}
MessageService.save(message)

CoroutineScope(Dispatchers.IO).launch {
MessageService.save(message)
}

bot.qqMessageHandler.onGroupMessage(
GroupMessageContext(
Expand Down
45 changes: 24 additions & 21 deletions src/main/kotlin/kurenai/imsyncbot/service/FileService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package kurenai.imsyncbot.service

import it.tdlight.jni.TdApi
import it.tdlight.jni.TdApi.InputFileLocal
import it.tdlight.jni.TdApi.InputFileRemote
import kotlinx.coroutines.flow.channelFlow
import kurenai.imsyncbot.domain.FileCache
import kurenai.imsyncbot.fileCacheRepository
Expand All @@ -14,7 +13,6 @@ import net.mamoe.mirai.message.data.Image
import net.mamoe.mirai.message.data.Image.Key.queryUrl
import net.mamoe.mirai.utils.MiraiInternalApi
import kotlin.io.path.pathString
import kotlin.jvm.optionals.getOrNull

/**
* @author Kurenai
Expand All @@ -25,30 +23,35 @@ object FileService {

@OptIn(MiraiInternalApi::class)
suspend fun download(image: Image) = withIO {
fileCacheRepository.findById(image.md5.toHex()).getOrNull()?.let {
InputFileRemote(it.fileId)
} ?: run {
InputFileLocal(
BotUtil.downloadImg(
"${image.imageId.substring(1..36).replace("-", "")}.${image.imageType.formatName}",
image.queryUrl()
).pathString
)
}
// fileCacheRepository.findById(image.md5.toHex()).getOrNull()?.let {
// InputFileRemote(it.fileId)
// } ?: run {
// InputFileLocal(
// BotUtil.downloadImg(
// "${image.imageId.substring(1..36).replace("-", "")}.${image.imageType.formatName}",
// image.queryUrl()
// ).pathString
// )
// }
InputFileLocal(BotUtil.downloadImg(image.queryUrl()).pathString)
}

@OptIn(MiraiInternalApi::class)
suspend fun download(images: Iterable<Image>) = channelFlow {
val imgMap = images.associateBy { it.md5.toHex() }.toMutableMap()
val caches = withIO { fileCacheRepository.findAllById(imgMap.keys) }
caches.forEach {
send(InputFileRemote(it.fileId))
imgMap.remove(it.id)
}
// val imgMap = images.associateBy { it.md5.toHex() }.toMutableMap()
// val caches = withIO { fileCacheRepository.findAllById(imgMap.keys) }
// caches.forEach {
// send(InputFileRemote(it.fileId))
// imgMap.remove(it.id)
// }
//
// imgMap.entries.takeIf { it.isNotEmpty() }?.forEach { (_, img) ->
// val filename = "${img.imageId.substring(1..36).replace("-", "")}.${img.imageType.formatName}"
// send(InputFileLocal(BotUtil.downloadImg(filename, img.queryUrl()).pathString))
// }

imgMap.entries.takeIf { it.isNotEmpty() }?.forEach { (_, img) ->
val filename = "${img.imageId.substring(1..36).replace("-", "")}.${img.imageType.formatName}"
send(InputFileLocal(BotUtil.downloadImg(filename, img.queryUrl()).pathString))
images.forEach {
send(InputFileLocal(BotUtil.downloadImg(it.queryUrl()).pathString))
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/main/kotlin/kurenai/imsyncbot/utils/BotUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.future.await
import kotlinx.coroutines.withContext
import kurenai.imsyncbot.exception.BotException
import kurenai.imsyncbot.snowFlake
import java.nio.file.Files
import java.nio.file.Path
import kotlin.io.path.createDirectories
import kotlin.io.path.exists
Expand Down Expand Up @@ -80,6 +82,20 @@ object BotUtil {
return download(image, url, onlyCache, overwrite)
}

suspend fun downloadImg(
url: String,
ext: String? = null,
onlyCache: Boolean = false,
): Path {
val image = Path.of(getImagePath(snowFlake.nextAlpha()))
val tmpPath = download(image, url, onlyCache, false)
val path = Path.of(getImagePath(tmpPath.crc32c() + if (ext?.isNotBlank() == true) ".$ext" else ""))
withContext(Dispatchers.IO) {
Files.move(tmpPath, path)
}
return path
}

private suspend fun download(path: Path, url: String, onlyCache: Boolean, overwrite: Boolean): Path {
if (!onlyCache) {
HttpUtil.download(path, url, overwrite = overwrite)
Expand Down
39 changes: 35 additions & 4 deletions src/main/kotlin/kurenai/imsyncbot/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator
import com.fasterxml.jackson.module.kotlin.KotlinFeature
import com.fasterxml.jackson.module.kotlin.kotlinModule
import io.ktor.client.*
import io.ktor.util.*
import it.tdlight.jni.TdApi
import kotlinx.coroutines.*
import kotlinx.coroutines.sync.Semaphore
import kotlinx.coroutines.sync.withPermit
import kotlinx.serialization.json.Json
import okio.ByteString.Companion.decodeHex
import okhttp3.internal.toHexString
import org.reflections.Reflections
import org.slf4j.Logger
import org.slf4j.LoggerFactory
Expand All @@ -27,7 +25,10 @@ import java.text.StringCharacterIterator
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneId
import java.util.HexFormat
import java.util.*
import java.util.zip.CRC32
import java.util.zip.CRC32C
import java.util.zip.Checksum
import kotlin.coroutines.CoroutineContext
import kotlin.coroutines.EmptyCoroutineContext
import kotlin.io.path.inputStream
Expand Down Expand Up @@ -140,6 +141,36 @@ fun Long.toLocalDateTime(): LocalDateTime {
return LocalDateTime.ofInstant(Instant.ofEpochMilli(this), ZoneId.systemDefault())
}

fun ByteArray.crc32c(): String {
val checksum = CRC32C()
checksum.update(this)
return checksum.value.toHexString()
}

fun ByteArray.crc32(): String {
val checksum = CRC32()
checksum.update(this)
return checksum.value.toHexString()
}

fun Path.crc32c(): String {
return checksum(this, CRC32C())
}

fun Path.crc32(): String {
return checksum(this, CRC32())
}

private fun checksum(path: Path, checksum: Checksum): String {
path.inputStream().use { input ->
val buff = ByteArray(DEFAULT_BUFFER_SIZE)
while (input.read(buff) != -1) {
checksum.update(buff)
}
}
return checksum.value.toHexString()
}

fun ByteArray.md5(): String {
val md = MessageDigest.getInstance("MD5")
return md.digest(this).toHex()
Expand Down

0 comments on commit aff1da1

Please sign in to comment.