mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-01 07:36:48 +08:00
Fix voice upload
This commit is contained in:
parent
dfad8572ee
commit
31a8932070
@ -18,6 +18,7 @@ import net.mamoe.mirai.event.events.*
|
||||
import net.mamoe.mirai.message.MessageReceipt
|
||||
import net.mamoe.mirai.message.data.*
|
||||
import net.mamoe.mirai.utils.ExternalResource
|
||||
import net.mamoe.mirai.utils.ExternalResource.Companion.uploadAsVoice
|
||||
import net.mamoe.mirai.utils.MiraiExperimentalApi
|
||||
import net.mamoe.mirai.utils.OverFileSizeMaxException
|
||||
|
||||
@ -154,9 +155,11 @@ public interface Group : Contact, CoroutineScope {
|
||||
/**
|
||||
* 上传一个语音消息以备发送.
|
||||
*
|
||||
* - 请手动关闭输入流
|
||||
* - **请手动关闭 [resource]**
|
||||
* - 请使用 amr 或 silk 格式
|
||||
*
|
||||
* @see ExternalResource.uploadAsVoice
|
||||
*
|
||||
* @throws EventCancelledException 当发送消息事件被取消
|
||||
* @throws OverFileSizeMaxException 当语音文件过大而被服务器拒绝上传时. (最大大小约为 1 MB)
|
||||
*/
|
||||
|
@ -231,7 +231,7 @@ public interface ExternalResource : Closeable {
|
||||
@JvmBlockingBridge
|
||||
@JvmStatic
|
||||
@MiraiExperimentalApi
|
||||
public suspend inline fun ExternalResource.uploadAsVoice(group: Group): Voice {
|
||||
public suspend fun ExternalResource.uploadAsVoice(group: Group): Voice {
|
||||
return group.uploadVoice(this)
|
||||
}
|
||||
}
|
||||
|
@ -14,10 +14,25 @@
|
||||
|
||||
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(
|
||||
|
@ -267,12 +267,6 @@ internal class GroupImpl(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传一个语音消息以备发送.
|
||||
* 请注意,这是一个实验性api且随时会被删除
|
||||
* @throws EventCancelledException 当发送消息事件被取消
|
||||
* @throws OverFileSizeMaxException 当语音文件过大而被服务器拒绝上传时. (最大大小约为 1 MB)
|
||||
*/
|
||||
override suspend fun uploadVoice(resource: ExternalResource): Voice {
|
||||
if (resource.size > 1048576) {
|
||||
throw OverFileSizeMaxException()
|
||||
|
@ -212,7 +212,7 @@ internal object HighwayHelper {
|
||||
parameter("bmd5", resource.md5.toUHexString(""))
|
||||
parameter("mType", "pttDu")
|
||||
parameter("voice_encodec", resource.voiceCodec)
|
||||
body = resource.inputStream()
|
||||
body = resource.inputStream().consumeAsWriteChannelContent()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -20,8 +20,10 @@ import net.mamoe.mirai.internal.network.protocol.packet.OutgoingPacketFactory
|
||||
import net.mamoe.mirai.internal.network.protocol.packet.buildOutgoingUniPacket
|
||||
import net.mamoe.mirai.internal.utils.io.serialization.readProtoBuf
|
||||
import net.mamoe.mirai.internal.utils.io.serialization.writeProtoBuf
|
||||
import net.mamoe.mirai.internal.utils.toIpV4AddressString
|
||||
import net.mamoe.mirai.utils.ExternalResource
|
||||
import net.mamoe.mirai.utils.encodeToString
|
||||
import net.mamoe.mirai.utils.toUHexString
|
||||
|
||||
internal val ExternalResource.voiceCodec: Int
|
||||
get() {
|
||||
@ -45,7 +47,18 @@ internal class PttStore {
|
||||
val fileKey: ByteArray
|
||||
) : GroupPttUp.Response() {
|
||||
override fun toString(): String {
|
||||
return "RequireUpload(fileId=$fileId, uKey=${uKey.contentToString()})"
|
||||
return "RequireUpload(" +
|
||||
"fileId=$fileId, " +
|
||||
"uploadServers=${
|
||||
uploadIpList.zip(uploadPortList)
|
||||
.joinToString(
|
||||
prefix = "[",
|
||||
postfix = "]"
|
||||
) { "${it.first.toIpV4AddressString()}:${it.second}1" }
|
||||
}, " +
|
||||
"uKey=${uKey.toUHexString("")}, " +
|
||||
"fileKey=${fileKey.toUHexString("")}" +
|
||||
")"
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -88,7 +101,8 @@ internal class PttStore {
|
||||
|
||||
override suspend fun ByteReadPacket.decode(bot: QQAndroidBot): Response {
|
||||
val resp0 = readProtoBuf(Cmd0x388.RspBody.serializer())
|
||||
val resp = resp0.msgTryupPttRsp.firstOrNull() ?: error("cannot find `msgTryupPttRsp` from `Cmd0x388.RspBody`")
|
||||
val resp =
|
||||
resp0.msgTryupPttRsp.firstOrNull() ?: error("cannot find `msgTryupPttRsp` from `Cmd0x388.RspBody`")
|
||||
if (resp.failMsg != null) {
|
||||
throw IllegalStateException(resp.failMsg.encodeToString())
|
||||
}
|
||||
@ -152,7 +166,8 @@ internal class PttStore {
|
||||
|
||||
override suspend fun ByteReadPacket.decode(bot: QQAndroidBot): Response {
|
||||
val resp0 = readProtoBuf(Cmd0x388.RspBody.serializer())
|
||||
val resp = resp0.msgGetpttUrlRsp.firstOrNull() ?: error("cannot find `msgGetpttUrlRsp` from `Cmd0x388.RspBody`")
|
||||
val resp =
|
||||
resp0.msgGetpttUrlRsp.firstOrNull() ?: error("cannot find `msgGetpttUrlRsp` from `Cmd0x388.RspBody`")
|
||||
if (!resp.failMsg.contentEquals(EMPTY_BYTE_ARRAY)) {
|
||||
throw IllegalStateException(resp.failMsg.encodeToString())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user