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
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 <R> ByteReadPacket.useBytes(

View File

@ -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)
}
}
}