1
0
mirror of https://github.com/mamoe/mirai.git synced 2025-04-25 04:50:26 +08:00

Small fixes

This commit is contained in:
Him188 2020-02-07 16:57:00 +08:00
parent 98669ca121
commit 4b162c546c
8 changed files with 12 additions and 62 deletions
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid
mirai-core/src
androidMain/kotlin/net/mamoe/mirai/utils
commonMain/kotlin/net.mamoe.mirai/utils
jvmMain/kotlin/net/mamoe/mirai/utils

View File

@ -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.utils.toIpV4AddressString
import net.mamoe.mirai.utils.*
import net.mamoe.mirai.utils.io.toUHexString
import kotlin.coroutines.CoroutineContext
import kotlin.properties.Delegates
@ -59,12 +60,12 @@ internal class QQImpl(bot: QQAndroidBot, override val coroutineContext: Coroutin
fileId = 0,
fileMd5 = image.md5,
fileSize = image.inputSize.toInt(),
fileName = image.filename,
fileName = image.md5.toUHexString("") + ".jpg",
imgOriginal = 1,
imgWidth = image.width,
imgHeight = image.height,
imgType = image.imageType,
buType = 1
buType = 0
)
).sendAndExpect<LongConn.OffPicUp.Response>()
@ -81,8 +82,8 @@ internal class QQImpl(bot: QQAndroidBot, override val coroutineContext: Coroutin
HighwayHelper.uploadImage(
client = bot.client,
uin = bot.uin,
serverIp = response.serverIp[2].toIpV4AddressString(),
serverPort = response.serverPort[2],
serverIp = response.serverIp[0].toIpV4AddressString(),
serverPort = response.serverPort[0],
imageInput = image.input,
inputSize = image.inputSize.toInt(),
md5 = image.md5,

View File

@ -69,7 +69,7 @@ object Highway {
sequenceId: Int,
appId: Int = 537062845,
dataFlag: Int = 4096,
commandId: Int = 2,
commandId: Int,
localId: Int = 2052,
uKey: ByteArray,

View File

@ -24,6 +24,8 @@ internal object HighwayHelper {
md5: ByteArray,
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()
socket.connect(serverIp, serverPort)
socket.use {

View File

@ -3,8 +3,6 @@ package net.mamoe.mirai.utils
import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO
import io.ktor.util.KtorExperimentalAPI
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.io.pool.useInstance
import net.mamoe.mirai.utils.io.ByteArrayPool
import java.io.ByteArrayOutputStream
@ -13,16 +11,9 @@ import java.io.EOFException
import java.io.InputStream
import java.net.InetAddress
import java.security.MessageDigest
import java.util.concurrent.Executors
import java.util.zip.CRC32
import java.util.zip.Inflater
/**
* 设备名
*/
actual val deviceName: String get() = InetAddress.getLocalHost().hostName
/**
* Ktor HttpClient. 不同平台使用不同引擎.
*/
@ -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 {
this.checkOffsetAndLength(offset, length)
if (length == 0) return ByteArray(0)
@ -104,6 +85,3 @@ actual fun ByteArray.unzip(offset: Int, length: Int): ByteArray {
}
}
actual fun newCoroutineDispatcher(threadCount: Int): CoroutineDispatcher {
return Executors.newFixedThreadPool(threadCount).asCoroutineDispatcher()
}

View File

@ -29,7 +29,7 @@ class ExternalImage(
val filename: String
) {
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 {

View File

@ -14,16 +14,6 @@ inline val currentTimeMillis: Long get() = GMTDate().timestamp
inline val currentTimeSeconds: Long get() = currentTimeMillis / 1000
/**
* 设备名
*/
expect val deviceName: String
/**
* CRC32 算法
*/
expect fun crc32(key: ByteArray): Int
/**
* zip 压缩
@ -39,11 +29,6 @@ expect fun md5(byteArray: ByteArray): ByteArray
inline fun md5(str: String): ByteArray = md5(str.toByteArray())
/**
* hostname 解析 ipv4
*/
expect fun solveIpAddress(hostname: String): String
/**
* Localhost 解析
*/
@ -54,8 +39,6 @@ expect fun localIpAddress(): String
*/
expect val Http: HttpClient
expect fun newCoroutineDispatcher(threadCount: Int): CoroutineDispatcher
internal fun ByteArray.checkOffsetAndLength(offset: Int, length: Int) {
require(offset >= 0) { "offset shouldn't be negative: $offset" }
require(length >= 0) { "length shouldn't be negative: $length" }

View File

@ -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() }
@ -102,8 +102,8 @@ suspend inline fun URL.suspendToExternalImage(): ExternalImage = withContext(IO)
@Throws(IOException::class)
fun InputStream.toExternalImage(): ExternalImage {
val file = createTempFile().apply { deleteOnExit() }
file.outputStream().asOutput().use {
this.asInput().copyTo(it)
file.outputStream().use {
this.copyTo(it)
}
this.close()
return file.toExternalImage()

View File

@ -4,21 +4,13 @@ package net.mamoe.mirai.utils
import io.ktor.client.HttpClient
import io.ktor.client.engine.cio.CIO
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.asCoroutineDispatcher
import kotlinx.io.pool.useInstance
import net.mamoe.mirai.utils.io.ByteArrayPool
import java.io.*
import java.net.InetAddress
import java.security.MessageDigest
import java.util.concurrent.Executors
import java.util.zip.CRC32
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)
fun InputStream.md5(): ByteArray = this.use {
@ -51,8 +43,6 @@ fun DataInput.md5(): ByteArray {
return digest.digest()
}
actual fun solveIpAddress(hostname: String): String = InetAddress.getByName(hostname).hostAddress
actual fun localIpAddress(): String = InetAddress.getLocalHost().hostAddress
actual val Http: HttpClient get() = HttpClient(CIO)
@ -75,7 +65,3 @@ actual fun ByteArray.unzip(offset: Int, length: Int): ByteArray {
return output.toByteArray()
}
}
actual fun newCoroutineDispatcher(threadCount: Int): CoroutineDispatcher {
return Executors.newFixedThreadPool(threadCount).asCoroutineDispatcher()
}