Commit 2bf0a963 authored by Him188's avatar Him188

Update libraries and reduce redundant code

parent d5e6c2f8
...@@ -9,11 +9,11 @@ kotlinVersion=1.3.61 ...@@ -9,11 +9,11 @@ kotlinVersion=1.3.61
# kotlin libraries # kotlin libraries
serializationVersion=0.13.0 serializationVersion=0.13.0
coroutinesVersion=1.3.2 coroutinesVersion=1.3.2
atomicFuVersion=0.13.0 atomicFuVersion=0.14.1
kotlinXIoVersion=0.1.15 kotlinXIoVersion=0.1.16
coroutinesIoVersion=0.24.0 coroutinesIoVersion=0.1.16
# utility # utility
ktorVersion=1.2.4 ktorVersion=1.2.6
klockVersion=1.7.0 klockVersion=1.7.0
# gradle plugin # gradle plugin
protobufJavaVersion=3.10.0 protobufJavaVersion=3.10.0
\ No newline at end of file
...@@ -2,12 +2,7 @@ package net.mamoe.mirai.utils ...@@ -2,12 +2,7 @@ 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.client.request.HttpRequestBuilder
import io.ktor.http.ContentType
import io.ktor.http.content.OutgoingContent
import io.ktor.util.KtorExperimentalAPI import io.ktor.util.KtorExperimentalAPI
import kotlinx.coroutines.io.ByteWriteChannel
import kotlinx.io.core.Input
import java.io.DataInput import java.io.DataInput
import java.io.EOFException import java.io.EOFException
import java.io.InputStream import java.io.InputStream
...@@ -33,24 +28,6 @@ internal actual val Http: HttpClient ...@@ -33,24 +28,6 @@ internal actual val Http: HttpClient
*/ */
actual fun localIpAddress(): String = InetAddress.getLocalHost().hostAddress actual fun localIpAddress(): String = InetAddress.getLocalHost().hostAddress
internal actual fun HttpRequestBuilder.configureBody(
inputSize: Long,
input: Input
) {
body = object : OutgoingContent.WriteChannelContent() {
override val contentType: ContentType = ContentType.Image.PNG
override val contentLength: Long = inputSize
override suspend fun writeTo(channel: ByteWriteChannel) {//不知道为什么这个 channel 在 common 找不到...
val buffer = byteArrayOf(1)
repeat(contentLength.toInt()) {
input.readFully(buffer, 0, 1)
channel.writeFully(buffer, 0, 1)
}
}
}
}
/** /**
* MD5 算法 * MD5 算法
* *
......
...@@ -14,7 +14,7 @@ import kotlin.reflect.KClass ...@@ -14,7 +14,7 @@ import kotlin.reflect.KClass
/** /**
* 订阅者的状态 * 订阅者的状态
*/ */ // Not using enum for Android
inline class ListeningStatus(inline val listening: Boolean) { inline class ListeningStatus(inline val listening: Boolean) {
companion object { companion object {
@JvmStatic @JvmStatic
......
...@@ -86,6 +86,7 @@ abstract class BotSessionBase( ...@@ -86,6 +86,7 @@ abstract class BotSessionBase(
field = value field = value
_gtk = getGTK(value) _gtk = getGTK(value)
} }
@Suppress("PropertyName")
internal lateinit var _cookies: String internal lateinit var _cookies: String
private var _gtk: Int = 0 private var _gtk: Int = 0
......
...@@ -75,8 +75,8 @@ class ImageDownloadInfo( ...@@ -75,8 +75,8 @@ class ImageDownloadInfo(
@SerialId(13) private val _original: String? = null, @SerialId(13) private val _original: String? = null,
@SerialId(14) private val _compressed: String? = null @SerialId(14) private val _compressed: String? = null
) : GroupImageResponse, ImageLink { ) : GroupImageResponse, ImageLink {
private val port: List<Byte> get() = _port!! private inline val port: List<Byte> get() = _port!!
private val host: String get() = "http://" + _host!! private inline val host: String get() = "http://" + _host!!
val thumbnail: String get() = host + ":" + port.first() + _thumbnail!! val thumbnail: String get() = host + ":" + port.first() + _thumbnail!!
override val original: String get() = host + ":" + port.first() + _original!! override val original: String get() = host + ":" + port.first() + _original!!
......
...@@ -4,12 +4,14 @@ package net.mamoe.mirai.network.protocol.tim.packet.action ...@@ -4,12 +4,14 @@ package net.mamoe.mirai.network.protocol.tim.packet.action
import io.ktor.client.HttpClient import io.ktor.client.HttpClient
import io.ktor.client.request.post import io.ktor.client.request.post
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode import io.ktor.http.HttpStatusCode
import io.ktor.http.URLProtocol import io.ktor.http.URLProtocol
import io.ktor.http.content.OutgoingContent
import io.ktor.http.userAgent import io.ktor.http.userAgent
import kotlinx.coroutines.io.ByteWriteChannel
import kotlinx.io.core.Input import kotlinx.io.core.Input
import net.mamoe.mirai.contact.GroupId import net.mamoe.mirai.contact.GroupId
import net.mamoe.mirai.utils.configureBody
@Suppress("SpellCheckingInspection") @Suppress("SpellCheckingInspection")
...@@ -41,7 +43,18 @@ internal suspend inline fun HttpClient.postImage( ...@@ -41,7 +43,18 @@ internal suspend inline fun HttpClient.postImage(
userAgent("QQClient") userAgent("QQClient")
} }
configureBody(inputSize, imageInput) body = object : OutgoingContent.WriteChannelContent() {
override val contentType: ContentType = ContentType.Image.PNG
override val contentLength: Long = inputSize
override suspend fun writeTo(channel: ByteWriteChannel) {
val buffer = byteArrayOf(1)
repeat(contentLength.toInt()) {
imageInput.readFully(buffer, 0, 1)
channel.writeFully(buffer, 0, 1)
}
}
}
} == HttpStatusCode.OK } == HttpStatusCode.OK
} finally { } finally {
imageInput.close() imageInput.close()
......
...@@ -4,8 +4,6 @@ package net.mamoe.mirai.utils ...@@ -4,8 +4,6 @@ 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 kotlinx.io.core.Input
/** /**
* 时间戳 * 时间戳
...@@ -45,7 +43,3 @@ expect fun localIpAddress(): String ...@@ -45,7 +43,3 @@ expect fun localIpAddress(): String
*/ */
@PublishedApi @PublishedApi
internal expect val Http: HttpClient internal expect val Http: HttpClient
// FIXME: 2019/10/28 这个方法不是很好的实现
internal expect fun HttpRequestBuilder.configureBody(inputSize: Long, input: Input)
...@@ -4,13 +4,6 @@ package net.mamoe.mirai.utils ...@@ -4,13 +4,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.client.request.HttpRequestBuilder
import io.ktor.client.request.get
import io.ktor.http.ContentType
import io.ktor.http.content.OutgoingContent
import kotlinx.coroutines.io.ByteWriteChannel
import kotlinx.io.core.ByteReadPacket
import kotlinx.io.core.Input
import java.io.DataInput import java.io.DataInput
import java.io.EOFException import java.io.EOFException
import java.io.InputStream import java.io.InputStream
...@@ -57,28 +50,4 @@ actual fun solveIpAddress(hostname: String): String = InetAddress.getByName(host ...@@ -57,28 +50,4 @@ actual fun solveIpAddress(hostname: String): String = InetAddress.getByName(host
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)
\ No newline at end of file
suspend fun HttpClient.getURL(url: String): ByteReadPacket {
return this.get(url)
}
internal actual fun HttpRequestBuilder.configureBody(
inputSize: Long,
input: Input
) {
//body = ByteArrayContent(input.readBytes(), ContentType.Image.PNG)
body = object : OutgoingContent.WriteChannelContent() {
override val contentType: ContentType = ContentType.Image.PNG
override val contentLength: Long = inputSize
override suspend fun writeTo(channel: ByteWriteChannel) {//不知道为什么这个 channel 在 common 找不到...
val buffer = byteArrayOf(1)
repeat(contentLength.toInt()) {
input.readFully(buffer, 0, 1)
channel.writeFully(buffer, 0, 1)
}
}
}
}
\ 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