Skip to content

Commit

Permalink
[core]修复从可确定图片类型的字符串组成的mirai code构造的图片type为UNKNOW或与期望类型不一致
Browse files Browse the repository at this point in the history
  • Loading branch information
Hamusuta0320 committed Jul 30, 2023
1 parent 8305363 commit 5867bd4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
8 changes: 2 additions & 6 deletions mirai-core-api/src/commonMain/kotlin/message/data/Image.kt
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public interface Image : Message, MessageContent, CodableMessage {
* @see Image.imageType
*/
public var type: ImageType = ImageType.UNKNOWN

get() = if(field == ImageType.UNKNOWN) ImageType.match(imageId.split(".").last()) else field
/**
* @see Image.width
*/
Expand All @@ -324,9 +324,6 @@ public interface Image : Message, MessageContent, CodableMessage {


public fun build(): Image {
// 创建之前确定类型
type = ImageType.match(imageId.split(".").last())
// isEmoji字段能确定吗
@OptIn(MiraiInternalApi::class)
return InternalImageProtocol.instance.createImage(
imageId = imageId,
Expand Down Expand Up @@ -511,8 +508,7 @@ public enum class ImageType(

//WEBP, //Unsupported by pc client
APNG("png"),
// 这里既然是未知格式,空串会不会好点,返回gif会可能造成歧义
UNKNOWN(""); // bad design, should use `null` to represent unknown, but we cannot change it anymore.
UNKNOWN("gif"); // bad design, should use `null` to represent unknown, but we cannot change it anymore.

public companion object {
private val IMAGE_TYPE_ENUM_LIST = values()
Expand Down
48 changes: 42 additions & 6 deletions mirai-core/src/commonTest/kotlin/message/ImageBuilderTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import kotlin.test.assertEquals
internal class ImageBuilderTest : AbstractTest() {
companion object {
private const val IMAGE_ID = "{01E9451B-70ED-EAE3-B37C-101F1EEBF5B5}.jpg"
private const val IMAGE_ID_PNG = "{01E9451B-70ED-EAE3-B37C-101F1EEBF5B5}.png"
private const val IMAGE_ID_BMP = "{01E9451B-70ED-EAE3-B37C-101F1EEBF5B5}.bmp"
private const val IMAGE_ID_GIF = "{01E9451B-70ED-EAE3-B37C-101F1EEBF5B5}.gif"
private const val IMAGE_ID_UNKNOW = "/01E9451B-70ED-EAE3-B37C-101F1EEBF5B5"
}

@Test
Expand All @@ -30,7 +34,7 @@ internal class ImageBuilderTest : AbstractTest() {
assertEquals(0, width)
assertEquals(0, height)
assertEquals(0, size)
assertEquals(ImageType.UNKNOWN, type)
assertEquals(ImageType.JPG, type)
assertEquals(false, isEmoji)
}

Expand All @@ -39,7 +43,7 @@ internal class ImageBuilderTest : AbstractTest() {
assertEquals(0, width)
assertEquals(0, height)
assertEquals(0, size)
assertEquals(ImageType.UNKNOWN, type)
assertEquals(ImageType.JPG, type)
assertEquals(false, isEmoji)
}

Expand All @@ -48,7 +52,7 @@ internal class ImageBuilderTest : AbstractTest() {
assertEquals(0, width)
assertEquals(0, height)
assertEquals(0, size)
assertEquals(ImageType.UNKNOWN, type)
assertEquals(ImageType.JPG, type)
assertEquals(false, isEmoji)
}

Expand All @@ -57,7 +61,7 @@ internal class ImageBuilderTest : AbstractTest() {
assertEquals(0, width)
assertEquals(0, height)
assertEquals(0, size)
assertEquals(ImageType.UNKNOWN, imageType)
assertEquals(ImageType.JPG, imageType)
assertEquals(false, isEmoji)
}
}
Expand All @@ -71,22 +75,54 @@ internal class ImageBuilderTest : AbstractTest() {
assertEquals(0, width)
assertEquals(0, height)
assertEquals(0, size)
assertEquals(ImageType.UNKNOWN, imageType)
assertEquals(ImageType.JPG, imageType)
assertEquals(false, isEmoji)
}
Image.fromId(IMAGE_ID).run {
assertEquals(IMAGE_ID, imageId)
assertEquals(0, width)
assertEquals(0, height)
assertEquals(0, size)
assertEquals(ImageType.UNKNOWN, imageType)
assertEquals(ImageType.JPG, imageType)
assertEquals(false, isEmoji)
}
Image(IMAGE_ID).run {
assertEquals(IMAGE_ID, imageId)
assertEquals(0, width)
assertEquals(0, height)
assertEquals(0, size)
assertEquals(ImageType.JPG, imageType)
assertEquals(false, isEmoji)
}
Image(IMAGE_ID_PNG).run {
assertEquals(IMAGE_ID_PNG, imageId)
assertEquals(0, width)
assertEquals(0, height)
assertEquals(0, size)
assertEquals(ImageType.PNG, imageType)
assertEquals(false, isEmoji)
}
Image(IMAGE_ID_BMP).run {
assertEquals(IMAGE_ID_BMP, imageId)
assertEquals(0, width)
assertEquals(0, height)
assertEquals(0, size)
assertEquals(ImageType.BMP, imageType)
assertEquals(false, isEmoji)
}
Image(IMAGE_ID_GIF).run {
assertEquals(IMAGE_ID_GIF, imageId)
assertEquals(0, width)
assertEquals(0, height)
assertEquals(0, size)
assertEquals(ImageType.GIF, imageType)
assertEquals(false, isEmoji)
}
Image(IMAGE_ID_UNKNOW).run {
assertEquals(IMAGE_ID_UNKNOW, imageId)
assertEquals(0, width)
assertEquals(0, height)
assertEquals(0, size)
assertEquals(ImageType.UNKNOWN, imageType)
assertEquals(false, isEmoji)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ internal class LongMessageProtocolTest : AbstractMessageProtocolTest() {
<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<msg serviceID="35" templateID="1" action="viewMultiMsg"
brief="testqGnJ1R..."
m_resid="(size=1)DBD2AB20196EEB631C95DEF40E20C709"
m_resid="(size=1)6C6FD4AEC362AA8E54058A27B422FA42"
m_fileName="160023" sourceMsgId="0" url=""
flag="3" adverSign="0" multiMsgFlag="1">
<item layout="1">
Expand All @@ -86,7 +86,7 @@ internal class LongMessageProtocolTest : AbstractMessageProtocolTest() {
</item>
<source name="聊天记录" icon="" action="" appid="-1"/>
</msg>
""".trimIndent(), "(size=1)DBD2AB20196EEB631C95DEF40E20C709"
""".trimIndent(), "(size=1)6C6FD4AEC362AA8E54058A27B422FA42"
) + IgnoreLengthCheck + ForceAsLongMessage, context.currentMessageChain
)
}
Expand Down

0 comments on commit 5867bd4

Please sign in to comment.