Commit 4b162c54 authored by Him188's avatar Him188

Small fixes

parent 98669ca1
...@@ -17,6 +17,7 @@ import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.image.LongConn ...@@ -17,6 +17,7 @@ import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.image.LongConn
import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.receive.MessageSvc import net.mamoe.mirai.qqandroid.network.protocol.packet.chat.receive.MessageSvc
import net.mamoe.mirai.qqandroid.utils.toIpV4AddressString import net.mamoe.mirai.qqandroid.utils.toIpV4AddressString
import net.mamoe.mirai.utils.* import net.mamoe.mirai.utils.*
import net.mamoe.mirai.utils.io.toUHexString
import kotlin.coroutines.CoroutineContext import kotlin.coroutines.CoroutineContext
import kotlin.properties.Delegates import kotlin.properties.Delegates
...@@ -59,12 +60,12 @@ internal class QQImpl(bot: QQAndroidBot, override val coroutineContext: Coroutin ...@@ -59,12 +60,12 @@ internal class QQImpl(bot: QQAndroidBot, override val coroutineContext: Coroutin
fileId = 0, fileId = 0,
fileMd5 = image.md5, fileMd5 = image.md5,
fileSize = image.inputSize.toInt(), fileSize = image.inputSize.toInt(),
fileName = image.filename, fileName = image.md5.toUHexString("") + ".jpg",
imgOriginal = 1, imgOriginal = 1,
imgWidth = image.width, imgWidth = image.width,
imgHeight = image.height, imgHeight = image.height,
imgType = image.imageType, imgType = image.imageType,
buType = 1 buType = 0
) )
).sendAndExpect<LongConn.OffPicUp.Response>() ).sendAndExpect<LongConn.OffPicUp.Response>()
...@@ -81,8 +82,8 @@ internal class QQImpl(bot: QQAndroidBot, override val coroutineContext: Coroutin ...@@ -81,8 +82,8 @@ internal class QQImpl(bot: QQAndroidBot, override val coroutineContext: Coroutin
HighwayHelper.uploadImage( HighwayHelper.uploadImage(
client = bot.client, client = bot.client,
uin = bot.uin, uin = bot.uin,
serverIp = response.serverIp[2].toIpV4AddressString(), serverIp = response.serverIp[0].toIpV4AddressString(),
serverPort = response.serverPort[2], serverPort = response.serverPort[0],
imageInput = image.input, imageInput = image.input,
inputSize = image.inputSize.toInt(), inputSize = image.inputSize.toInt(),
md5 = image.md5, md5 = image.md5,
......
...@@ -69,7 +69,7 @@ object Highway { ...@@ -69,7 +69,7 @@ object Highway {
sequenceId: Int, sequenceId: Int,
appId: Int = 537062845, appId: Int = 537062845,
dataFlag: Int = 4096, dataFlag: Int = 4096,
commandId: Int = 2, commandId: Int,
localId: Int = 2052, localId: Int = 2052,
uKey: ByteArray, uKey: ByteArray,
......
...@@ -24,6 +24,8 @@ internal object HighwayHelper { ...@@ -24,6 +24,8 @@ internal object HighwayHelper {
md5: ByteArray, md5: ByteArray,
commandId: Int // group=2, friend=1 commandId: Int // group=2, friend=1
) { ) {
require(md5.size == 16) { "bad md5. Required size=16, got ${md5.size}" }
require(uKey.size == 128) { "bad uKey. Required size=128, got ${uKey.size}" }
val socket = PlatformSocket() val socket = PlatformSocket()
socket.connect(serverIp, serverPort) socket.connect(serverIp, serverPort)
socket.use { socket.use {
......
...@@ -3,8 +3,6 @@ package net.mamoe.mirai.utils ...@@ -3,8 +3,6 @@ package net.mamoe.mirai.utils
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO import io.ktor.client.engine.cio.CIO
import io.ktor.util.KtorExperimentalAPI import io.ktor.util.KtorExperimentalAPI
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.io.pool.useInstance import kotlinx.io.pool.useInstance
import net.mamoe.mirai.utils.io.ByteArrayPool import net.mamoe.mirai.utils.io.ByteArrayPool
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
...@@ -13,16 +11,9 @@ import java.io.EOFException ...@@ -13,16 +11,9 @@ import java.io.EOFException
import java.io.InputStream import java.io.InputStream
import java.net.InetAddress import java.net.InetAddress
import java.security.MessageDigest import java.security.MessageDigest
import java.util.concurrent.Executors
import java.util.zip.CRC32
import java.util.zip.Inflater import java.util.zip.Inflater
/**
* 设备名
*/
actual val deviceName: String get() = InetAddress.getLocalHost().hostName
/** /**
* Ktor HttpClient. 不同平台使用不同引擎. * Ktor HttpClient. 不同平台使用不同引擎.
*/ */
...@@ -75,16 +66,6 @@ private inline fun InputStream.readInSequence(block: (Int) -> Unit) { ...@@ -75,16 +66,6 @@ private inline fun InputStream.readInSequence(block: (Int) -> Unit) {
} }
} }
/**
* CRC32 算法
*/
actual fun crc32(key: ByteArray): Int = CRC32().apply { update(key) }.value.toInt()
/**
* hostname 解析 ipv4
*/
actual fun solveIpAddress(hostname: String): String = InetAddress.getByName(hostname).hostAddress
actual fun ByteArray.unzip(offset: Int, length: Int): ByteArray { actual fun ByteArray.unzip(offset: Int, length: Int): ByteArray {
this.checkOffsetAndLength(offset, length) this.checkOffsetAndLength(offset, length)
if (length == 0) return ByteArray(0) if (length == 0) return ByteArray(0)
...@@ -104,6 +85,3 @@ actual fun ByteArray.unzip(offset: Int, length: Int): ByteArray { ...@@ -104,6 +85,3 @@ actual fun ByteArray.unzip(offset: Int, length: Int): ByteArray {
} }
} }
actual fun newCoroutineDispatcher(threadCount: Int): CoroutineDispatcher {
return Executors.newFixedThreadPool(threadCount).asCoroutineDispatcher()
}
\ No newline at end of file
...@@ -29,7 +29,7 @@ class ExternalImage( ...@@ -29,7 +29,7 @@ class ExternalImage(
val filename: String val filename: String
) { ) {
init { init {
check(inputSize in Int.MIN_VALUE.toLong()..Int.MAX_VALUE.toLong()) { "file is too big" } check(inputSize in 0L..Int.MAX_VALUE.toLong()) { "file is too big" }
} }
companion object { companion object {
......
...@@ -14,16 +14,6 @@ inline val currentTimeMillis: Long get() = GMTDate().timestamp ...@@ -14,16 +14,6 @@ inline val currentTimeMillis: Long get() = GMTDate().timestamp
inline val currentTimeSeconds: Long get() = currentTimeMillis / 1000 inline val currentTimeSeconds: Long get() = currentTimeMillis / 1000
/**
* 设备名
*/
expect val deviceName: String
/**
* CRC32 算法
*/
expect fun crc32(key: ByteArray): Int
/** /**
* 解 zip 压缩 * 解 zip 压缩
...@@ -39,11 +29,6 @@ expect fun md5(byteArray: ByteArray): ByteArray ...@@ -39,11 +29,6 @@ expect fun md5(byteArray: ByteArray): ByteArray
inline fun md5(str: String): ByteArray = md5(str.toByteArray()) inline fun md5(str: String): ByteArray = md5(str.toByteArray())
/**
* hostname 解析 ipv4
*/
expect fun solveIpAddress(hostname: String): String
/** /**
* Localhost 解析 * Localhost 解析
*/ */
...@@ -54,8 +39,6 @@ expect fun localIpAddress(): String ...@@ -54,8 +39,6 @@ expect fun localIpAddress(): String
*/ */
expect val Http: HttpClient expect val Http: HttpClient
expect fun newCoroutineDispatcher(threadCount: Int): CoroutineDispatcher
internal fun ByteArray.checkOffsetAndLength(offset: Int, length: Int) { internal fun ByteArray.checkOffsetAndLength(offset: Int, length: Int) {
require(offset >= 0) { "offset shouldn't be negative: $offset" } require(offset >= 0) { "offset shouldn't be negative: $offset" }
require(length >= 0) { "length shouldn't be negative: $length" } require(length >= 0) { "length shouldn't be negative: $length" }
......
...@@ -44,7 +44,7 @@ fun BufferedImage.toExternalImage(formatName: String = "gif"): ExternalImage { ...@@ -44,7 +44,7 @@ fun BufferedImage.toExternalImage(formatName: String = "gif"): ExternalImage {
}) })
} }
return ExternalImage(width, height, digest.digest(), formatName, buffer, getRandomString(10) + "." + formatName) return ExternalImage(width, height, digest.digest(), formatName, buffer, getRandomString(16) + "." + formatName)
} }
suspend inline fun BufferedImage.suspendToExternalImage(): ExternalImage = withContext(IO) { toExternalImage() } suspend inline fun BufferedImage.suspendToExternalImage(): ExternalImage = withContext(IO) { toExternalImage() }
...@@ -102,8 +102,8 @@ suspend inline fun URL.suspendToExternalImage(): ExternalImage = withContext(IO) ...@@ -102,8 +102,8 @@ suspend inline fun URL.suspendToExternalImage(): ExternalImage = withContext(IO)
@Throws(IOException::class) @Throws(IOException::class)
fun InputStream.toExternalImage(): ExternalImage { fun InputStream.toExternalImage(): ExternalImage {
val file = createTempFile().apply { deleteOnExit() } val file = createTempFile().apply { deleteOnExit() }
file.outputStream().asOutput().use { file.outputStream().use {
this.asInput().copyTo(it) this.copyTo(it)
} }
this.close() this.close()
return file.toExternalImage() return file.toExternalImage()
......
...@@ -4,21 +4,13 @@ package net.mamoe.mirai.utils ...@@ -4,21 +4,13 @@ package net.mamoe.mirai.utils
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO import io.ktor.client.engine.cio.CIO
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.io.pool.useInstance import kotlinx.io.pool.useInstance
import net.mamoe.mirai.utils.io.ByteArrayPool import net.mamoe.mirai.utils.io.ByteArrayPool
import java.io.* import java.io.*
import java.net.InetAddress import java.net.InetAddress
import java.security.MessageDigest import java.security.MessageDigest
import java.util.concurrent.Executors
import java.util.zip.CRC32
import java.util.zip.Inflater import java.util.zip.Inflater
actual val deviceName: String = InetAddress.getLocalHost().hostName
actual fun crc32(key: ByteArray): Int = CRC32().let { it.update(key); it.value.toInt() }
actual fun md5(byteArray: ByteArray): ByteArray = MessageDigest.getInstance("MD5").digest(byteArray) actual fun md5(byteArray: ByteArray): ByteArray = MessageDigest.getInstance("MD5").digest(byteArray)
fun InputStream.md5(): ByteArray = this.use { fun InputStream.md5(): ByteArray = this.use {
...@@ -51,8 +43,6 @@ fun DataInput.md5(): ByteArray { ...@@ -51,8 +43,6 @@ fun DataInput.md5(): ByteArray {
return digest.digest() return digest.digest()
} }
actual fun solveIpAddress(hostname: String): String = InetAddress.getByName(hostname).hostAddress
actual fun localIpAddress(): String = InetAddress.getLocalHost().hostAddress actual fun localIpAddress(): String = InetAddress.getLocalHost().hostAddress
actual val Http: HttpClient get() = HttpClient(CIO) actual val Http: HttpClient get() = HttpClient(CIO)
...@@ -75,7 +65,3 @@ actual fun ByteArray.unzip(offset: Int, length: Int): ByteArray { ...@@ -75,7 +65,3 @@ actual fun ByteArray.unzip(offset: Int, length: Int): ByteArray {
return output.toByteArray() return output.toByteArray()
} }
} }
actual fun newCoroutineDispatcher(threadCount: Int): CoroutineDispatcher {
return Executors.newFixedThreadPool(threadCount).asCoroutineDispatcher()
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment