Skip to content

Commit

Permalink
try to suit imageId features
Browse files Browse the repository at this point in the history
  • Loading branch information
MrXiaoM committed May 1, 2024
1 parent 0944926 commit df78661
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,13 @@ internal object OnebotMessages {
"text" -> add(data["text"].string)
"face" -> add(Face(data["id"].string.toInt()))
"image" -> {
val image = imageFromFile((data["url"] ?: data["file"]).string)
val imageUrl = (data["url"] ?: data["file"]).string
val imageId = data["file"]?.run {
if (string.startsWith("http")) "!$string" else string
} ?: "!$imageUrl"
val fileSize = data["file_size"]?.string?.toLongOrNull() ?: 0L

val image = WrappedImage(imageUrl, imageId, ImageType.UNKNOWN, fileSize, 0, 0)
if (data["type"].string == "flash") {
add(image.flash())
} else {
Expand Down Expand Up @@ -475,14 +481,15 @@ internal object OnebotMessages {
internal fun audioFromFile(file: String): Audio = WrappedAudio(file, 0)
internal fun videoFromFile(file: String): ShortVideo = WrappedVideo(file)

@OptIn(MiraiExperimentalApi::class)
private val Image.onebotFile: String
get() = imageId
get() = (this as? WrappedImage)?.url ?: throw IllegalStateException("Image type ${this::class.qualifiedName} do not support")
private val FlashImage.onebotFile: String
get() = image.onebotFile
private val Audio.onebotFile: String
get() = (this as? WrappedAudio)?.file ?: ""
get() = (this as? WrappedAudio)?.file ?: throw IllegalStateException("Audio type ${this::class.qualifiedName} do not support")
private val ShortVideo.onebotFile: String
get() = (this as? WrappedVideo)?.file ?: ""
get() = (this as? WrappedVideo)?.file ?: throw IllegalStateException("ShortVideo type ${this::class.qualifiedName} do not support")
internal val JsonElement?.string
get() = this?.jsonPrimitive?.content ?: ""
internal val JsonElement?.int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,25 @@ import kotlinx.serialization.Serializable
import net.mamoe.mirai.Bot
import net.mamoe.mirai.contact.Contact
import net.mamoe.mirai.message.data.Image
import net.mamoe.mirai.message.data.Image.Key.calculateImageMd5ByImageId
import net.mamoe.mirai.message.data.ImageType
import net.mamoe.mirai.message.data.InternalImageProtocol
import net.mamoe.mirai.utils.MiraiExperimentalApi
import net.mamoe.mirai.utils.MiraiInternalApi
import net.mamoe.mirai.utils.toUHexString
import top.mrxiaom.overflow.internal.utils.base64Length
import top.mrxiaom.overflow.internal.utils.lengthToString
import java.util.*

@OptIn(MiraiInternalApi::class)
internal class WrappedImageProtocol : InternalImageProtocol {
private val whateverBotId: Long
get() = Bot.instances.firstOrNull()?.id ?: 10001L
internal fun friendImageId(imageId: String): String {
val md5 = calculateImageMd5ByImageId(imageId)
// /1234567890-3666252994-EFF4427CE3D27DB6B1D9A8AB72E7A29C
return "/000000000-000000000-${md5.toUHexString("")}"
}
@OptIn(MiraiExperimentalApi::class)
override fun createImage(
imageId: String,
Expand All @@ -25,7 +34,32 @@ internal class WrappedImageProtocol : InternalImageProtocol {
height: Int,
isEmoji: Boolean
): Image {
return WrappedImage(imageId, type, size, width, height)
return when {
imageId matches Image.IMAGE_ID_REGEX -> {
// TODO: image cache
// if (size == 0L && width == 0 && height == 0) {
// findExistImageByCache(imageId)?.let { return it }
// }
val url = "http://gchat.qpic.cn/gchatpic_new/$whateverBotId/0-0-$imageId/0?term=2"
WrappedImage(url, imageId, type, size, width, height)
}

imageId matches Image.IMAGE_RESOURCE_ID_REGEX_1 -> {
val url = "http://c2cpicdw.qpic.cn/offpic_new/$whateverBotId${friendImageId(imageId)}/0?term=2"
WrappedImage(url, imageId, type, size, width, height)
}

imageId matches Image.IMAGE_RESOURCE_ID_REGEX_2 -> {
val url = "http://c2cpicdw.qpic.cn/offpic_new/$whateverBotId${friendImageId(imageId)}/0?term=2"
WrappedImage(url, imageId, type, size, width, height)
}

else -> {
// Onebot
// base64:// file:/// http:// https://
return WrappedImage(imageId, "!$imageId", type, size, width, height)
}
}
}

override suspend fun isUploaded(
Expand All @@ -44,21 +78,21 @@ internal class WrappedImageProtocol : InternalImageProtocol {
@MiraiExperimentalApi
internal data class WrappedImage(
val url: String,
override val imageId: String,
override val imageType: ImageType,
override val size: Long,
override val width: Int,
override val height: Int,
): Image {
private val _stringValue: String? by lazy(LazyThreadSafetyMode.NONE) {
val fileString = if (url.startsWith("base64://") && url.length > 32) {
val s = url.replace("base64://", "")
val fileString = if (imageId.startsWith("!base64://") && url.length > 32) {
val s = url.removePrefix("base64://")
val len = base64Length(s)
val type = Base64.getDecoder().decode(s).fileType ?: "*"
"${url.substring(0, 32)}... (${if (type == "*") "" else "$type, "}about ${lengthToString(len)})"
} else url
"[overflow:image,url=$fileString]"
} else imageId.removePrefix("!")
"[overflow:image:$fileString]"
}
override val imageId: String = url

override fun contentToString(): String = if (isEmoji) {
"[动画表情]"
Expand Down

0 comments on commit df78661

Please sign in to comment.