diff --git a/mirai-core-utils/src/commonMain/kotlin/IO.kt b/mirai-core-utils/src/commonMain/kotlin/IO.kt index 358628a8a..2969e81d0 100644 --- a/mirai-core-utils/src/commonMain/kotlin/IO.kt +++ b/mirai-core-utils/src/commonMain/kotlin/IO.kt @@ -14,25 +14,10 @@ package net.mamoe.mirai.utils -import io.ktor.http.content.* -import io.ktor.utils.io.* import io.ktor.utils.io.charsets.* -import io.ktor.utils.io.jvm.javaio.* import kotlinx.io.core.* -import java.io.InputStream import kotlin.text.Charsets -/** - * 在发送完成后将会 [InputStream.close] - */ -public fun InputStream.consumeAsWriteChannelContent(): OutgoingContent.WriteChannelContent { - return object : OutgoingContent.WriteChannelContent() { - override suspend fun writeTo(channel: ByteWriteChannel) { - runBIO { this@consumeAsWriteChannelContent.withUse { copyTo(channel) } } - } - } -} - @Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE") public inline fun ByteReadPacket.useBytes( diff --git a/mirai-core/src/commonMain/kotlin/network/highway/HighwayHelper.kt b/mirai-core/src/commonMain/kotlin/network/highway/HighwayHelper.kt index 7b2cb0ed8..d65185db8 100644 --- a/mirai-core/src/commonMain/kotlin/network/highway/HighwayHelper.kt +++ b/mirai-core/src/commonMain/kotlin/network/highway/HighwayHelper.kt @@ -38,6 +38,20 @@ import kotlin.time.ExperimentalTime import kotlin.time.measureTime +/** + * 在发送完成后将会 [InputStream.close] + */ +internal fun ExternalResource.consumeAsWriteChannelContent(contentType: ContentType?): OutgoingContent.WriteChannelContent { + return object : OutgoingContent.WriteChannelContent() { + override val contentType: ContentType? = contentType + override val contentLength: Long = size + + override suspend fun writeTo(channel: ByteWriteChannel) { + inputStream().withUse { copyTo(channel) } + } + } +} + @Suppress("SpellCheckingInspection") internal suspend fun HttpClient.postImage( htcmd: String, @@ -65,14 +79,7 @@ internal suspend fun HttpClient.postImage( userAgent("QQClient") } - body = object : OutgoingContent.WriteChannelContent() { - override val contentType: ContentType = ContentType.Image.Any - override val contentLength: Long = imageInput.size.toLong() - - override suspend fun writeTo(channel: ByteWriteChannel) { - imageInput.inputStream().withUse { copyTo(channel) } - } - } + body = imageInput.consumeAsWriteChannelContent(ContentType.Image.Any) } == HttpStatusCode.OK @@ -98,7 +105,7 @@ internal object HighwayHelper { kind: String, commandId: Int ) = servers.retryWithServers( - (input.size * 1000 / 1024 / 10).coerceAtLeast(5000).toLong(), + (input.size * 1000 / 1024 / 10).coerceAtLeast(5000), onFail = { throw IllegalStateException("cannot upload $kind, failed on all servers.", it) } @@ -212,7 +219,8 @@ internal object HighwayHelper { parameter("bmd5", resource.md5.toUHexString("")) parameter("mType", "pttDu") parameter("voice_encodec", resource.voiceCodec) - body = resource.inputStream().consumeAsWriteChannelContent() + body = resource.inputStream().withUse { readBytes() } + // body = resource.inputStream().consumeAsWriteChannelContent(null) } } }