Skip to content

Commit

Permalink
[core] 修复从可确定图片类型的字符串组成的 mirai code 构造的图片 type 为 UNKNOW 或与期望类型不一致 (#2761
Browse files Browse the repository at this point in the history
)

* [core]修复从可确定图片类型的字符串组成的mirai code构造的图片type为UNKNOW或与期望类型不一致

* [core]修复从可确定图片类型的字符串组成的mirai code构造的图片type为UNKNOW或与期望类型不一致

* [core]修复从可确定图片类型的字符串组成的mirai code构造的图片type为UNKNOW或与期望类型不一致

* Update mirai-core-api/src/commonMain/kotlin/message/data/Image.kt

---------

Co-authored-by: Him188 <[email protected]>
  • Loading branch information
Hamusuta0320 and Him188 authored Aug 12, 2023
1 parent 0aad162 commit e61b058
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
4 changes: 3 additions & 1 deletion 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,6 @@ public interface Image : Message, MessageContent, CodableMessage {
* @see Image.imageType
*/
public var type: ImageType = ImageType.UNKNOWN

/**
* @see Image.width
*/
Expand All @@ -324,6 +323,9 @@ public interface Image : Message, MessageContent, CodableMessage {


public fun build(): Image {
if (type == ImageType.UNKNOWN) {
type = ImageType.match(imageId.split(".").last())
}
@OptIn(MiraiInternalApi::class)
return InternalImageProtocol.instance.createImage(
imageId = imageId,
Expand Down
54 changes: 51 additions & 3 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 Down Expand Up @@ -57,6 +61,50 @@ internal class ImageBuilderTest : AbstractTest() {
assertEquals(0, width)
assertEquals(0, height)
assertEquals(0, size)
assertEquals(ImageType.JPG, imageType)
assertEquals(false, isEmoji)
}
}

@Test
fun imageType() {
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 All @@ -71,23 +119,23 @@ 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.UNKNOWN, imageType)
assertEquals(ImageType.JPG, imageType)
assertEquals(false, isEmoji)
}
Image(IMAGE_ID) {
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 e61b058

Please sign in to comment.