ソースを参照

Support `ShortVideo` uploading

ryoii 2 年 前
コミット
28429078a5

+ 27 - 0
docs/adapter/HttpAdapter.md

@@ -659,6 +659,33 @@ adapterSettings:
 }
 ```
 
+### 短视频文件上传
+
+使用此方法上传语音文件至服务器并返回videoId
+
+```
+[POST] /uploadShortVideo
+```
+
+**本接口为[POST]请求, 参数格式为`multipart/form-data`**
+
+#### 请求:
+
+| 名字         | 类型     | 可选    | 举例        | 说明                              |
+|------------|--------|-------| ----------- |---------------------------------|
+| sessionKey | String | true  | YourSession | 已经激活的Session                    |
+| type       | String | false | "group"     | 当前仅支持 "group", "friend", "temp" |
+| video      | File   | false | -           | 短视频文件                           |
+| thumbnail  | File   | false | -           | 短视频封面                           |
+
+#### 响应:
+
+```json5
+{
+  "videoId":"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", //短视频的videoId
+}
+```
+
 ### 群文件上传
 
 ```

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

@@ -62,7 +62,7 @@ internal fun Application.messageRouter() = routing {
         val data = it.unreadQueue.peek(it.count)
         call.respond(EventListRestfulResult(data = data))
     }
-    
+
     /**
      * 获取指定条数最新的消息,和 `/fetchLatestMessage` 不一样,这个方法不会删除消息
      */
@@ -134,6 +134,18 @@ internal fun Application.messageRouter() = routing {
         call.respond(ret)
     }
 
+    /**
+     * 上传短视频
+     */
+    httpAuthedMultiPart(Paths.uploadShortVideo) { session, parts ->
+        val type = parts.value("type")
+        val thumbnail = parts.file("thumbnail")?.run { streamProvider() } ?: throw IllegalParamException("缺少参数 thumbnail")
+        val video = parts.file("video")?.run{ streamProvider() } ?: throw IllegalParamException("缺少参数 video")
+
+        val ret = onUploadShortVideo(session, thumbnail, video, type)
+        call.respond(ret)
+    }
+
     /**
      * 撤回消息
      */

+ 20 - 0
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/action/message.kt

@@ -227,6 +227,26 @@ internal suspend fun onUploadVoice(session: Session, stream: InputStream, type:
     return voice?.run { UploadVoiceRetDTO(filename) } ?: throw IllegalAccessException("语音上传错误")
 }
 
+/**
+ * 上传短视频
+ */
+internal suspend fun onUploadShortVideo(session: Session, streamThumbnail: InputStream, streamVideo: InputStream, type: String): UploadShortVideoRetDTO {
+    val video = streamThumbnail.useStream { st ->
+        streamVideo.useStream { sv ->
+            when (type) {
+                "Group", "group" -> session.bot.groups.firstOrNull()?.uploadShortVideo(st, sv)
+                "Friend", "friend", "Temp", "temp" -> session.bot.friends.firstOrNull()?.uploadShortVideo(st, sv)
+
+                else -> null
+            }
+        }
+    }
+
+    return video?.run { UploadShortVideoRetDTO(videoId) }
+        ?: throw IllegalAccessException("视频上传错误")
+}
+
+
 /**
  * 消息撤回
  */

+ 2 - 1
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/consts/paths.kt

@@ -1,5 +1,5 @@
 /*
- * Copyright 2020 Mamoe Technologies and contributors.
+ * Copyright 2023 Mamoe Technologies and contributors.
  *
  * 此源代码的使用受 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.
@@ -59,6 +59,7 @@ object Paths {
     const val sendNudge = "sendNudge"
     const val uploadImage = "uploadImage"
     const val uploadVoice = "uploadVoice"
+    const val uploadShortVideo = "uploadShortVideo"
     const val recall = "recall"
     const val roamingMessages = "roamingMessages"
 

+ 6 - 0
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/internal/dto/parameter/message.kt

@@ -52,6 +52,12 @@ internal class UploadVoiceRetDTO(
     val voiceId: String,
 ) : DTO
 
+@Serializable
+@Suppress("unused")
+internal class UploadShortVideoRetDTO(
+    val videoId: String,
+) : DTO
+
 @Serializable
 internal data class MessageIdDTO(
     val target: Long,