add comments

This commit is contained in:
Him188 2019-10-28 22:02:42 +08:00
parent 28149dfed2
commit 9854000492
2 changed files with 6 additions and 80 deletions

View File

@ -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 <reified T> 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)

View File

@ -26,9 +26,10 @@ fun BytePacketBuilder.writeShortLVByteArray(byteArray: ByteArray) {
}
// will box, but it doesn't matter
private fun <N : Comparable<N>> 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) {