diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/Image.kt b/mirai-core-api/src/commonMain/kotlin/message/data/Image.kt index 75163ff7cd..c84286ba90 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/Image.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/Image.kt @@ -306,7 +306,6 @@ public interface Image : Message, MessageContent, CodableMessage { * @see Image.imageType */ public var type: ImageType = ImageType.UNKNOWN - /** * @see Image.width */ @@ -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, diff --git a/mirai-core/src/commonTest/kotlin/message/ImageBuilderTest.kt b/mirai-core/src/commonTest/kotlin/message/ImageBuilderTest.kt index 0769439069..1efef4a2a3 100644 --- a/mirai-core/src/commonTest/kotlin/message/ImageBuilderTest.kt +++ b/mirai-core/src/commonTest/kotlin/message/ImageBuilderTest.kt @@ -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 @@ -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) } @@ -71,7 +119,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) } Image.fromId(IMAGE_ID).run { @@ -79,7 +127,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) } Image(IMAGE_ID).run { @@ -87,7 +135,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) } Image(IMAGE_ID) { diff --git a/mirai-core/src/commonTest/kotlin/message/protocol/impl/LongMessageProtocolTest.kt b/mirai-core/src/commonTest/kotlin/message/protocol/impl/LongMessageProtocolTest.kt index 5010e119c4..5feb3baf33 100644 --- a/mirai-core/src/commonTest/kotlin/message/protocol/impl/LongMessageProtocolTest.kt +++ b/mirai-core/src/commonTest/kotlin/message/protocol/impl/LongMessageProtocolTest.kt @@ -76,7 +76,7 @@ internal class LongMessageProtocolTest : AbstractMessageProtocolTest() { @@ -86,7 +86,7 @@ internal class LongMessageProtocolTest : AbstractMessageProtocolTest() { - """.trimIndent(), "(size=1)DBD2AB20196EEB631C95DEF40E20C709" + """.trimIndent(), "(size=1)6C6FD4AEC362AA8E54058A27B422FA42" ) + IgnoreLengthCheck + ForceAsLongMessage, context.currentMessageChain ) }