diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/PlatformUtils.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/PlatformUtils.kt index c7bea5bbe..5af98c39a 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/PlatformUtils.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/PlatformUtils.kt @@ -5,12 +5,7 @@ package net.mamoe.mirai.utils import com.soywiz.klock.DateTime import io.ktor.client.HttpClient import io.ktor.client.request.HttpRequestBuilder -import io.ktor.client.request.post -import io.ktor.http.HttpStatusCode -import io.ktor.http.URLProtocol -import io.ktor.http.userAgent import kotlinx.io.core.Input -import net.mamoe.mirai.contact.GroupId /** * 时间戳 @@ -38,7 +33,7 @@ expect fun md5(byteArray: ByteArray): ByteArray /** * Hostname 解析 IP 地址 */ -expect fun solveIpAddress(hostname: String): String +expect fun solveIpAddress(hostname: String): String // TODO: 2019/10/28 是否有必要? /** * Localhost 解析 @@ -46,80 +41,10 @@ expect fun solveIpAddress(hostname: String): String expect fun localIpAddress(): String /** - * Provided by Ktor Http + * Ktor HttpClient. 不同平台使用不同引擎. */ internal expect val httpClient: HttpClient -/** - * 上传好友图片 - */ -@Suppress("DuplicatedCode") -suspend fun httpPostFriendImage( - botAccount: UInt, - uKeyHex: String, - imageInput: Input, - inputSize: Long -): Boolean = (httpClient.postImage( - htcmd = "0x6ff0070", - uin = botAccount, - groupcode = null, - imageInput = imageInput, - inputSize = inputSize, - uKeyHex = uKeyHex -) as HttpStatusCode).value == 200 - -/** - * 上传群图片 - */ -@Suppress("DuplicatedCode") -suspend fun httpPostGroupImage( - botAccount: UInt, - groupId: GroupId, - uKeyHex: String, - imageInput: Input, - inputSize: Long -): Boolean = (httpClient.postImage( - htcmd = "0x6ff0071", - uin = botAccount, - groupcode = groupId, - imageInput = imageInput, - inputSize = inputSize, - uKeyHex = uKeyHex -) as HttpStatusCode).value == 200 - -@Suppress("SpellCheckingInspection") -private suspend inline fun HttpClient.postImage( - htcmd: String, - uin: UInt, - groupcode: GroupId?, - imageInput: Input, - inputSize: Long, - uKeyHex: String -): T = try { - post { - url { - protocol = URLProtocol.HTTP - host = "htdata2.qq.com" - path("cgi-bin/httpconn") - - parameters["htcmd"] = htcmd - parameters["uin"] = uin.toLong().toString() - - if (groupcode != null) parameters["groupcode"] = groupcode.value.toLong().toString() - - parameters["term"] = "pc" - parameters["ver"] = "5603" - parameters["filesize"] = inputSize.toString() - parameters["range"] = 0.toString() - parameters["ukey"] = uKeyHex - - userAgent("QQClient") - } - - configureBody(inputSize, imageInput) - } -} finally { - imageInput.close() -} +// FIXME: 2019/10/28 这个方法不是很好的实现 internal expect fun HttpRequestBuilder.configureBody(inputSize: Long, input: Input) \ No newline at end of file diff --git a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/OutputUtils.kt b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/OutputUtils.kt index 86477669b..bf7089f1c 100644 --- a/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/OutputUtils.kt +++ b/mirai-core/src/commonMain/kotlin/net.mamoe.mirai/utils/io/OutputUtils.kt @@ -26,9 +26,10 @@ fun BytePacketBuilder.writeShortLVByteArray(byteArray: ByteArray) { } +// will box, but it doesn't matter private fun > N.coerceAtMostOrFail(maximumValue: N): N = - if (this > maximumValue) error("value is greater than its expected maximum value $maximumValue") - else this + if (this > maximumValue) error("value is greater than its expected maximum value $maximumValue") + else this fun BytePacketBuilder.writeShortLVPacket(tag: UByte? = null, lengthOffset: ((Long) -> Long)? = null, builder: BytePacketBuilder.() -> Unit) = with(BytePacketBuilder().apply(builder).build()) { if (tag != null) {