mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-07 00:20:09 +08:00
add comments
This commit is contained in:
parent
28149dfed2
commit
9854000492
@ -5,12 +5,7 @@ package net.mamoe.mirai.utils
|
|||||||
import com.soywiz.klock.DateTime
|
import com.soywiz.klock.DateTime
|
||||||
import io.ktor.client.HttpClient
|
import io.ktor.client.HttpClient
|
||||||
import io.ktor.client.request.HttpRequestBuilder
|
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 kotlinx.io.core.Input
|
||||||
import net.mamoe.mirai.contact.GroupId
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 时间戳
|
* 时间戳
|
||||||
@ -38,7 +33,7 @@ expect fun md5(byteArray: ByteArray): ByteArray
|
|||||||
/**
|
/**
|
||||||
* Hostname 解析 IP 地址
|
* Hostname 解析 IP 地址
|
||||||
*/
|
*/
|
||||||
expect fun solveIpAddress(hostname: String): String
|
expect fun solveIpAddress(hostname: String): String // TODO: 2019/10/28 是否有必要?
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Localhost 解析
|
* Localhost 解析
|
||||||
@ -46,80 +41,10 @@ expect fun solveIpAddress(hostname: String): String
|
|||||||
expect fun localIpAddress(): String
|
expect fun localIpAddress(): String
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provided by Ktor Http
|
* Ktor HttpClient. 不同平台使用不同引擎.
|
||||||
*/
|
*/
|
||||||
internal expect val httpClient: 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)
|
internal expect fun HttpRequestBuilder.configureBody(inputSize: Long, input: Input)
|
@ -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 =
|
private fun <N : Comparable<N>> N.coerceAtMostOrFail(maximumValue: N): N =
|
||||||
if (this > maximumValue) error("value is greater than its expected maximum value $maximumValue")
|
if (this > maximumValue) error("value is greater than its expected maximum value $maximumValue")
|
||||||
else this
|
else this
|
||||||
|
|
||||||
fun BytePacketBuilder.writeShortLVPacket(tag: UByte? = null, lengthOffset: ((Long) -> Long)? = null, builder: BytePacketBuilder.() -> Unit) = with(BytePacketBuilder().apply(builder).build()) {
|
fun BytePacketBuilder.writeShortLVPacket(tag: UByte? = null, lengthOffset: ((Long) -> Long)? = null, builder: BytePacketBuilder.() -> Unit) = with(BytePacketBuilder().apply(builder).build()) {
|
||||||
if (tag != null) {
|
if (tag != null) {
|
||||||
|
Loading…
Reference in New Issue
Block a user