mirror of
https://github.com/mamoe/mirai.git
synced 2025-02-06 22:54:39 +08:00
Fix image upload, close #797
This commit is contained in:
parent
b482590ce1
commit
901c8b5267
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2019-2020 Mamoe Technologies and contributors.
|
* Copyright 2019-2021 Mamoe Technologies and contributors.
|
||||||
*
|
*
|
||||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||||
@ -624,8 +624,7 @@ internal open class MiraiImpl : IMirai, LowLevelApiAccessor {
|
|||||||
bot,
|
bot,
|
||||||
response.proto.uint32UpIp.zip(response.proto.uint32UpPort),
|
response.proto.uint32UpIp.zip(response.proto.uint32UpPort),
|
||||||
response.proto.msgSig,
|
response.proto.msgSig,
|
||||||
body.md5(),
|
body.toExternalResource(null),
|
||||||
body.toExternalResource(null), // don't use toLongUnsigned: Overload resolution ambiguity
|
|
||||||
"group long message",
|
"group long message",
|
||||||
27
|
27
|
||||||
)
|
)
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2019-2020 Mamoe Technologies and contributors.
|
* Copyright 2019-2021 Mamoe Technologies and contributors.
|
||||||
*
|
*
|
||||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||||
@ -238,7 +238,6 @@ internal class GroupImpl(
|
|||||||
size = resource.size.toInt()
|
size = resource.size.toInt()
|
||||||
).sendAndExpect()
|
).sendAndExpect()
|
||||||
|
|
||||||
@Suppress("UNCHECKED_CAST") // bug
|
|
||||||
when (response) {
|
when (response) {
|
||||||
is ImgStore.GroupPicUp.Response.Failed -> {
|
is ImgStore.GroupPicUp.Response.Failed -> {
|
||||||
ImageUploadEvent.Failed(this@GroupImpl, resource, response.resultCode, response.message).broadcast()
|
ImageUploadEvent.Failed(this@GroupImpl, resource, response.resultCode, response.message).broadcast()
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2019-2020 Mamoe Technologies and contributors.
|
* Copyright 2019-2021 Mamoe Technologies and contributors.
|
||||||
*
|
*
|
||||||
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
|
||||||
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
|
||||||
@ -84,34 +84,23 @@ internal suspend fun HttpClient.postImage(
|
|||||||
|
|
||||||
|
|
||||||
internal object HighwayHelper {
|
internal object HighwayHelper {
|
||||||
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
|
|
||||||
suspend fun uploadImageToServers(
|
|
||||||
bot: QQAndroidBot,
|
|
||||||
servers: List<Pair<Int, Int>>,
|
|
||||||
uKey: ByteArray,
|
|
||||||
image: ExternalResource,
|
|
||||||
kind: String,
|
|
||||||
commandId: Int
|
|
||||||
) = uploadImageToServers(bot, servers, uKey, image.md5, image, kind, commandId)
|
|
||||||
|
|
||||||
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
|
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
|
||||||
@OptIn(ExperimentalTime::class)
|
@OptIn(ExperimentalTime::class)
|
||||||
suspend fun uploadImageToServers(
|
suspend fun uploadImageToServers(
|
||||||
bot: QQAndroidBot,
|
bot: QQAndroidBot,
|
||||||
servers: List<Pair<Int, Int>>,
|
servers: List<Pair<Int, Int>>,
|
||||||
uKey: ByteArray,
|
uKey: ByteArray,
|
||||||
md5: ByteArray,
|
resource: ExternalResource,
|
||||||
input: ExternalResource,
|
|
||||||
kind: String,
|
kind: String,
|
||||||
commandId: Int
|
commandId: Int
|
||||||
) = servers.retryWithServers(
|
) = servers.retryWithServers(
|
||||||
(input.size * 1000 / 1024 / 10).coerceAtLeast(5000),
|
(resource.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)
|
||||||
}
|
}
|
||||||
) { ip, port ->
|
) { ip, port ->
|
||||||
bot.network.logger.verbose {
|
bot.network.logger.verbose {
|
||||||
"[Highway] Uploading $kind to ${ip}:$port, size=${input.size.sizeToString()}"
|
"[Highway] Uploading $kind to ${ip}:$port, size=${resource.size.sizeToString()}"
|
||||||
}
|
}
|
||||||
|
|
||||||
val time = measureTime {
|
val time = measureTime {
|
||||||
@ -119,15 +108,15 @@ internal object HighwayHelper {
|
|||||||
client = bot.client,
|
client = bot.client,
|
||||||
serverIp = ip,
|
serverIp = ip,
|
||||||
serverPort = port,
|
serverPort = port,
|
||||||
imageInput = input,
|
resource = resource,
|
||||||
fileMd5 = md5,
|
fileMd5 = resource.md5,
|
||||||
ticket = uKey,
|
ticket = uKey,
|
||||||
commandId = commandId
|
commandId = commandId
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
bot.network.logger.verbose {
|
bot.network.logger.verbose {
|
||||||
"[Highway] Uploading $kind: succeed at ${(input.size.toDouble() / 1024 / time.inSeconds).roundToInt()} KiB/s"
|
"[Highway] Uploading $kind: succeed at ${(resource.size.toDouble() / 1024 / time.inSeconds).roundToInt()} KiB/s"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,7 +127,7 @@ internal object HighwayHelper {
|
|||||||
serverIp: String,
|
serverIp: String,
|
||||||
serverPort: Int,
|
serverPort: Int,
|
||||||
ticket: ByteArray,
|
ticket: ByteArray,
|
||||||
imageInput: ExternalResource,
|
resource: ExternalResource,
|
||||||
fileMd5: ByteArray,
|
fileMd5: ByteArray,
|
||||||
commandId: Int // group=2, friend=1
|
commandId: Int // group=2, friend=1
|
||||||
) {
|
) {
|
||||||
@ -162,7 +151,7 @@ internal object HighwayHelper {
|
|||||||
command = "PicUp.DataUp",
|
command = "PicUp.DataUp",
|
||||||
commandId = commandId,
|
commandId = commandId,
|
||||||
ticket = ticket,
|
ticket = ticket,
|
||||||
data = imageInput,
|
data = resource,
|
||||||
fileMd5 = fileMd5
|
fileMd5 = fileMd5
|
||||||
).useAll {
|
).useAll {
|
||||||
socket.send(it)
|
socket.send(it)
|
||||||
@ -238,12 +227,15 @@ internal class ChunkedFlowSession<T>(
|
|||||||
@Suppress("BlockingMethodInNonBlockingContext")
|
@Suppress("BlockingMethodInNonBlockingContext")
|
||||||
internal suspend inline fun useAll(crossinline block: suspend (T) -> Unit) = withUse {
|
internal suspend inline fun useAll(crossinline block: suspend (T) -> Unit) = withUse {
|
||||||
runBIO {
|
runBIO {
|
||||||
|
while (true) {
|
||||||
val size = input.read(buffer)
|
val size = input.read(buffer)
|
||||||
|
if (size == -1) return@runBIO
|
||||||
block(mapper(buffer, size, offset))
|
block(mapper(buffer, size, offset))
|
||||||
offset += size
|
offset += size
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
internal fun createImageDataPacketSequence(
|
internal fun createImageDataPacketSequence(
|
||||||
|
Loading…
Reference in New Issue
Block a user