Fix voice upload, fix #782

This commit is contained in:
Him188 2020-12-28 16:29:21 +08:00
parent d8228c523e
commit 9347d6baf6
2 changed files with 18 additions and 25 deletions

View File

@ -14,25 +14,10 @@
package net.mamoe.mirai.utils 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.charsets.*
import io.ktor.utils.io.jvm.javaio.*
import kotlinx.io.core.* import kotlinx.io.core.*
import java.io.InputStream
import kotlin.text.Charsets 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") @Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
public inline fun <R> ByteReadPacket.useBytes( public inline fun <R> ByteReadPacket.useBytes(

View File

@ -38,6 +38,20 @@ import kotlin.time.ExperimentalTime
import kotlin.time.measureTime 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") @Suppress("SpellCheckingInspection")
internal suspend fun HttpClient.postImage( internal suspend fun HttpClient.postImage(
htcmd: String, htcmd: String,
@ -65,14 +79,7 @@ internal suspend fun HttpClient.postImage(
userAgent("QQClient") userAgent("QQClient")
} }
body = object : OutgoingContent.WriteChannelContent() { body = imageInput.consumeAsWriteChannelContent(ContentType.Image.Any)
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) }
}
}
} == HttpStatusCode.OK } == HttpStatusCode.OK
@ -98,7 +105,7 @@ internal object HighwayHelper {
kind: String, kind: String,
commandId: Int commandId: Int
) = servers.retryWithServers( ) = servers.retryWithServers(
(input.size * 1000 / 1024 / 10).coerceAtLeast(5000).toLong(), (input.size * 1000 / 1024 / 10).coerceAtLeast(5000),
onFail = { onFail = {
throw IllegalStateException("cannot upload $kind, failed on all servers.", it) throw IllegalStateException("cannot upload $kind, failed on all servers.", it)
} }
@ -212,7 +219,8 @@ internal object HighwayHelper {
parameter("bmd5", resource.md5.toUHexString("")) parameter("bmd5", resource.md5.toUHexString(""))
parameter("mType", "pttDu") parameter("mType", "pttDu")
parameter("voice_encodec", resource.voiceCodec) parameter("voice_encodec", resource.voiceCodec)
body = resource.inputStream().consumeAsWriteChannelContent() body = resource.inputStream().withUse { readBytes() }
// body = resource.inputStream().consumeAsWriteChannelContent(null)
} }
} }
} }