Skip to content

Commit

Permalink
revert suit imageId feature
Browse files Browse the repository at this point in the history
  • Loading branch information
MrXiaoM committed May 9, 2024
1 parent 3bd5c2e commit f37b538
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import top.mrxiaom.overflow.internal.listener.addGuildListeners
import top.mrxiaom.overflow.internal.message.OnebotMessages
import top.mrxiaom.overflow.internal.message.data.OfflineMessageSourceImpl
import top.mrxiaom.overflow.internal.message.data.WrappedFileMessage
import top.mrxiaom.overflow.internal.message.data.WrappedImage
import top.mrxiaom.overflow.internal.plugin.OverflowCoreAsPlugin
import top.mrxiaom.overflow.internal.utils.wrapAsOtherClientInfo
import java.io.File
Expand Down Expand Up @@ -280,7 +279,7 @@ class Overflow : IMirai, CoroutineScope, LowLevelApiAccessor, OverflowAPI {
}
}
}
override fun imageFromFile(file: String): Image = Image.fromId(file)
override fun imageFromFile(file: String): Image = OnebotMessages.imageFromFile(file)
override fun audioFromFile(file: String): Audio = OnebotMessages.audioFromFile(file)
override fun videoFromFile(file: String): ShortVideo = OnebotMessages.videoFromFile(file)
override fun serializeMessage(bot: RemoteBot?, message: Message): String = OnebotMessages.serializeToOneBotJson(bot, message)
Expand Down Expand Up @@ -411,8 +410,7 @@ class Overflow : IMirai, CoroutineScope, LowLevelApiAccessor, OverflowAPI {

override suspend fun queryImageUrl(bot: Bot, image: Image): String {
// Onebot 没有 imageId 概念,Overflow 使用 imageId 字段来存储 Onebot 中的 file 链接
if (image !is WrappedImage) throw IllegalStateException("Image type ${image::class.qualifiedName} do not support")
return image.url
return image.imageId
}

@LowLevelApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ internal class FriendWrapper(
}

override suspend fun uploadImage(resource: ExternalResource): Image {
return Overflow.imageFromFile(FileService.instance!!.upload(resource))
return OnebotMessages.imageFromFile(FileService.instance!!.upload(resource))
}

override suspend fun uploadShortVideo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ internal class GroupWrapper(
}

override suspend fun uploadImage(resource: ExternalResource): Image {
return Overflow.imageFromFile(FileService.instance!!.upload(resource))
return OnebotMessages.imageFromFile(FileService.instance!!.upload(resource))
}

override suspend fun uploadShortVideo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ internal class MemberWrapper(
}

override suspend fun uploadImage(resource: ExternalResource): Image {
return Overflow.imageFromFile(FileService.instance!!.upload(resource))
return OnebotMessages.imageFromFile(FileService.instance!!.upload(resource))
}

override suspend fun uploadShortVideo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ internal class StrangerWrapper(
}

override suspend fun uploadImage(resource: ExternalResource): Image {
return Overflow.imageFromFile(FileService.instance!!.upload(resource))
return OnebotMessages.imageFromFile(FileService.instance!!.upload(resource))
}

override suspend fun uploadShortVideo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,7 @@ internal object OnebotMessages {
"text" -> add(data["text"].string)
"face" -> add(Face(data["id"].string.toInt()))
"image" -> {
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)
val image = imageFromFile((data["url"] ?: data["file"]).string)
if (data["type"].string == "flash") {
add(image.flash())
} else {
Expand Down Expand Up @@ -478,18 +472,18 @@ internal object OnebotMessages {
else -> "text"
}
}
internal fun imageFromFile(file: String): Image = Image.fromId(file)
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() = (this as? WrappedImage)?.url ?: throw IllegalStateException("Image type ${this::class.qualifiedName} do not support")
get() = imageId
private val FlashImage.onebotFile: String
get() = image.onebotFile
private val Audio.onebotFile: String
get() = (this as? WrappedAudio)?.file ?: throw IllegalStateException("Audio type ${this::class.qualifiedName} do not support")
get() = (this as? WrappedAudio)?.file ?: ""
private val ShortVideo.onebotFile: String
get() = (this as? WrappedVideo)?.file ?: throw IllegalStateException("ShortVideo type ${this::class.qualifiedName} do not support")
get() = (this as? WrappedVideo)?.file ?: ""
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,25 +6,16 @@ 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 @@ -34,32 +25,7 @@ internal class WrappedImageProtocol : InternalImageProtocol {
height: Int,
isEmoji: Boolean
): Image {
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)
}
}
return WrappedImage(imageId, type, size, width, height)
}

override suspend fun isUploaded(
Expand All @@ -78,21 +44,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 (imageId.startsWith("!base64://") && url.length > 32) {
val s = url.removePrefix("base64://")
val fileString = if (url.startsWith("base64://") && url.length > 32) {
val s = url.replace("base64://", "")
val len = base64Length(s)
val type = Base64.getDecoder().decode(s).fileType ?: "*"
"${url.substring(0, 32)}... (${if (type == "*") "" else "$type, "}about ${lengthToString(len)})"
} else imageId.removePrefix("!")
"[overflow:image:$fileString]"
} else url
"[overflow:image,url=$fileString]"
}
override val imageId: String = url

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

0 comments on commit f37b538

Please sign in to comment.