Skip to content

Commit abc1ed4

Browse files
committed
Support using url to upload image and voice file, #613
1 parent 320d972 commit abc1ed4

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

docs/adapter/HttpAdapter.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -615,11 +615,12 @@ adapterSettings:
615615

616616
#### 请求:
617617

618-
| 名字 | 类型 | 可选 | 举例 | 说明 |
619-
| ------------ | ------ | ----- | ----------- | ---------------------------------- |
620-
| sessionKey | String | true | YourSession | 已经激活的Session |
621-
| type | String | false | "friend" | "friend" 或 "group" 或 "temp" |
622-
| img | File | false | - | 图片文件 |
618+
| 名字 | 类型 | 可选 | 举例 | 说明 |
619+
|------------|--------|-------| ----------- |-----------------------------|
620+
| sessionKey | String | true | YourSession | 已经激活的Session |
621+
| type | String | false | "friend" | "friend" 或 "group" 或 "temp" |
622+
| img | File | true | - | 图片文件 |
623+
| url | String | true | - | 图片URL |
623624

624625
#### 响应:
625626

@@ -642,11 +643,12 @@ adapterSettings:
642643

643644
#### 请求:
644645

645-
| 名字 | 类型 | 可选 | 举例 | 说明 |
646-
| ------------ | ------ | ----- | ----------- | ---------------------------------- |
647-
| sessionKey | String | true | YourSession | 已经激活的Session |
648-
| type | String | false | "group" | 当前仅支持 "group" |
649-
| voice | File | false | - | 语音文件 |
646+
| 名字 | 类型 | 可选 | 举例 | 说明 |
647+
|------------|--------|-------| ----------- |---------------|
648+
| sessionKey | String | true | YourSession | 已经激活的Session |
649+
| type | String | false | "group" | 当前仅支持 "group" |
650+
| voice | File | true | - | 语音文件 |
651+
| url | String | true | - | 语音文件 URL |
650652

651653
#### 响应:
652654

mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/router/message.kt

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@
99

1010
package net.mamoe.mirai.api.http.adapter.http.router
1111

12-
import io.ktor.server.application.*
1312
import io.ktor.http.content.*
13+
import io.ktor.server.application.*
1414
import io.ktor.server.routing.*
1515
import net.mamoe.mirai.api.http.adapter.common.IllegalParamException
1616
import net.mamoe.mirai.api.http.adapter.http.dto.CountDTO
1717
import net.mamoe.mirai.api.http.adapter.internal.action.*
1818
import net.mamoe.mirai.api.http.adapter.internal.consts.Paths
1919
import net.mamoe.mirai.api.http.adapter.internal.dto.EventListRestfulResult
2020
import net.mamoe.mirai.api.http.adapter.internal.dto.IntRestfulResult
21+
import java.net.URL
2122

2223
/**
2324
* 消息路由
@@ -104,8 +105,15 @@ internal fun Application.messageRouter() = routing {
104105
*/
105106
httpAuthedMultiPart(Paths.uploadImage) { session, parts ->
106107
val type = parts.value("type")
107-
val ret = parts.file("img")?.run { onUploadImage(session, streamProvider(), type) }
108-
?: throw IllegalParamException("缺少参数 img")
108+
val url = parts.valueOrNull("url")
109+
val stream = if (url != null) {
110+
URL(url).openStream()
111+
} else {
112+
val f = parts.file("img") ?: throw IllegalParamException("缺少参数 img")
113+
f.streamProvider()
114+
}
115+
116+
val ret = onUploadImage(session, stream, type)
109117
call.respondDTO(ret)
110118
}
111119

@@ -114,8 +122,14 @@ internal fun Application.messageRouter() = routing {
114122
*/
115123
httpAuthedMultiPart(Paths.uploadVoice) { session, parts ->
116124
val type = parts.value("type")
117-
val ret = parts.file("voice")?.run { onUploadVoice(session, streamProvider(), type) }
118-
?: throw IllegalParamException("缺少参数 voice")
125+
val url = parts.valueOrNull("url")
126+
val stream = if (url != null) {
127+
URL(url).openStream()
128+
} else {
129+
val f = parts.file("voice") ?: throw IllegalParamException("缺少参数 voice")
130+
f.streamProvider()
131+
}
132+
val ret = onUploadVoice(session, stream, type)
119133
call.respondDTO(ret)
120134
}
121135

0 commit comments

Comments
 (0)