Rename BufferedImage as ExternalImage

This commit is contained in:
Him188 2019-10-24 15:03:42 +08:00
parent 0334d33d7e
commit 3ab540d5f2
4 changed files with 31 additions and 34 deletions

View File

@ -2,8 +2,6 @@
package net.mamoe.mirai.network.protocol.tim.packet
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.io.core.*
import net.mamoe.mirai.contact.QQ
import net.mamoe.mirai.message.ImageId
@ -17,7 +15,7 @@ import net.mamoe.mirai.utils.io.*
/**
* 上传图片
*/
suspend fun QQ.uploadImage(image: BufferedImage): ImageId = with(bot.network.session) {
suspend fun QQ.uploadImage(image: ExternalImage): ImageId = with(bot.network.session) {
//SubmitImageFilenamePacket(account, account, "sdiovaoidsa.png", sessionKey).sendAndExpect<ServerSubmitImageFilenameResponsePacket>().join()
DebugLogger.logPurple("正在上传好友图片, md5=${image.md5.toUHexString()}")
return FriendImageIdRequestPacket(this.qqAccount, sessionKey, this.qqAccount, image).sendAndExpect<FriendImageIdRequestPacket.Response, ImageId> {
@ -103,10 +101,10 @@ class SubmitImageFilenamePacket(
@PacketId(0x03_52u)
@PacketVersion(date = "2019.10.20", timVersion = "2.3.2.21173")
class FriendImageIdRequestPacket(
private val botNumber: UInt,
private val sessionKey: ByteArray,
private val target: UInt,
private val image: BufferedImage
private val botNumber: UInt,
private val sessionKey: ByteArray,
private val target: UInt,
private val image: ExternalImage
) : OutgoingPacket() {
//00 00 00 07 00 00 00 4B 08 01 12 03 98 01 01 08 01 12 47 08 A2 FF 8C F0 03 10 89 FC A6 8C 0B 18 00 22 10 2B 23 D7 05 CA D1 F2 CF 37 10 FE 58 26 92 FC C4 28 FD 08 32 1A 7B 00 47 00 47 00 42 00 7E 00 49 00 31 00 5A 00 4D 00 43 00 28 00 25 00 49 00 38 01 48 00 70 42 78 42
@ -221,7 +219,6 @@ class FriendImageIdRequestPacket(
writeUVarInt(image.inputSize.toUInt())
GlobalScope.launch { }
writeUByte(0x32u)
//长度应为1A
writeUVarintLVPacket {

View File

@ -12,20 +12,20 @@ import net.mamoe.mirai.utils.io.toUHexString
suspend fun Group.uploadImage(
image: BufferedImage
image: ExternalImage
) = with(bot.network.session) {
GroupImageIdRequestPacket(bot.qqAccount, groupId, image, sessionKey)
.sendAndExpect<GroupImageIdRequestPacket.Response, Unit> {
if (it.uKey != null) {
httpPostGroupImage(
botAccount = bot.qqAccount,
groupNumber = groupId,
imageInput = image.input,
inputSize = image.inputSize,
uKeyHex = it.uKey!!.toUHexString("")
)
}
}.await()
GroupImageIdRequestPacket(bot.qqAccount, internalId, image, sessionKey)
.sendAndExpect<GroupImageIdRequestPacket.Response, Unit> {
if (it.uKey != null) {
httpPostGroupImage(
botAccount = bot.qqAccount,
groupNumber = internalId,
imageInput = image.input,
inputSize = image.inputSize,
uKeyHex = it.uKey!!.toUHexString("")
)
}
}.await()
}
/**
@ -34,10 +34,10 @@ suspend fun Group.uploadImage(
@PacketId(0x0388u)
@PacketVersion(date = "2019.10.20", timVersion = "2.3.2.21173")
class GroupImageIdRequestPacket(
private val bot: UInt,
private val groupId: UInt,
private val image: BufferedImage,
private val sessionKey: ByteArray
private val bot: UInt,
private val groupId: UInt,
private val image: ExternalImage,
private val sessionKey: ByteArray
) : OutgoingPacket() {
override fun encode(builder: BytePacketBuilder) = with(builder) {

View File

@ -6,15 +6,15 @@ import kotlinx.io.core.ByteReadPacket
import kotlinx.io.core.Input
import net.mamoe.mirai.message.ImageId
fun BufferedImage(
fun ExternalImage(
width: Int,
height: Int,
md5: ByteArray,
format: String,
data: ByteReadPacket
) = BufferedImage(width, height, md5, format, data, data.remaining)
) = ExternalImage(width, height, md5, format, data, data.remaining)
class BufferedImage(
class ExternalImage(
val width: Int,
val height: Int,
val md5: ByteArray,
@ -28,7 +28,7 @@ class BufferedImage(
*/
val groupImageId: ImageId by lazy { ImageId("{${md5[0..3]}-${md5[4..5]}-${md5[6..7]}-${md5[8..9]}-${md5[10..15]}}.$format") }
override fun toString(): String = "[BufferedImage(${width}x${height} $format)]"
override fun toString(): String = "[ExternalImage(${width}x${height} $format)]"
}
private operator fun ByteArray.get(range: IntRange): String = buildString {

View File

@ -13,7 +13,7 @@ import java.security.MessageDigest
import javax.imageio.ImageIO
import java.awt.image.BufferedImage as JavaBufferedImage
fun JavaBufferedImage.toMiraiImage(formatName: String = "gif"): BufferedImage {
fun JavaBufferedImage.toMiraiImage(formatName: String = "gif"): ExternalImage {
val digest = MessageDigest.getInstance("md5")
digest.reset()
@ -28,10 +28,10 @@ fun JavaBufferedImage.toMiraiImage(formatName: String = "gif"): BufferedImage {
})
}
return BufferedImage(width, height, digest.digest(), formatName, buffer)
return ExternalImage(width, height, digest.digest(), formatName, buffer)
}
fun BufferedImage.toJavaImage(): JavaBufferedImage = ImageIO.read(object : InputStream() {
fun ExternalImage.toJavaImage(): JavaBufferedImage = ImageIO.read(object : InputStream() {
override fun read(): Int = with(this@toJavaImage.input) {
if (!endOfInput)
readByte().toInt()
@ -39,7 +39,7 @@ fun BufferedImage.toJavaImage(): JavaBufferedImage = ImageIO.read(object : Input
}
})
fun File.toMiraiImage(): BufferedImage {
fun File.toMiraiImage(): ExternalImage {
val image = ImageIO.getImageReaders(this.inputStream()).asSequence().first()
val digest = MessageDigest.getInstance("md5")
@ -53,7 +53,7 @@ fun File.toMiraiImage(): BufferedImage {
})
val dimension = image.defaultReadParam.sourceRenderSize
return BufferedImage(
return ExternalImage(
width = dimension.width,
height = dimension.height,
md5 = digest.digest(),