From 53c29468c969f6d78a5d4f855363d4fe7ceb68b0 Mon Sep 17 00:00:00 2001 From: StageGuard <1355416608@qq.com> Date: Wed, 2 Aug 2023 11:47:05 +0800 Subject: [PATCH] [core] extract fileName from `OnlineShortVideo` to `ShortVideo` --- .../src/commonMain/kotlin/contact/Contact.kt | 6 +++++- .../kotlin/message/data/ShortVideo.kt | 18 +++++++++--------- .../src/jvmBaseMain/kotlin/contact/Contact.kt | 7 ++++++- .../internal/contact/AbstractMockContact.kt | 6 +++++- .../kotlin/contact/AbstractContact.kt | 12 +++++++++--- .../kotlin/message/data/shortVideo.kt | 5 ++++- .../protocol/impl/ShortVideoProtocol.kt | 3 +-- 7 files changed, 39 insertions(+), 18 deletions(-) diff --git a/mirai-core-api/src/commonMain/kotlin/contact/Contact.kt b/mirai-core-api/src/commonMain/kotlin/contact/Contact.kt index 1df539cdf4..8ed0b5db48 100644 --- a/mirai-core-api/src/commonMain/kotlin/contact/Contact.kt +++ b/mirai-core-api/src/commonMain/kotlin/contact/Contact.kt @@ -99,7 +99,11 @@ public expect interface Contact : ContactOrBot, CoroutineScope { * @param video 视频资源,目前仅支持上传 mp4 格式的视频。 */ - public suspend fun uploadShortVideo(thumbnail: ExternalResource, video: ExternalResource): ShortVideo + public suspend fun uploadShortVideo( + thumbnail: ExternalResource, + video: ExternalResource, + fileName: String? = null + ): ShortVideo @JvmBlockingBridge public companion object { diff --git a/mirai-core-api/src/commonMain/kotlin/message/data/ShortVideo.kt b/mirai-core-api/src/commonMain/kotlin/message/data/ShortVideo.kt index 3867bb96f3..232c5373fd 100644 --- a/mirai-core-api/src/commonMain/kotlin/message/data/ShortVideo.kt +++ b/mirai-core-api/src/commonMain/kotlin/message/data/ShortVideo.kt @@ -16,25 +16,30 @@ import net.mamoe.mirai.utils.safeCast public interface ShortVideo : MessageContent, ConstrainSingle { /** - * 文件 ID. + * 视频 ID. */ public val fileId: String /** - * 文件 MD5. 16 bytes. + * 视频文件 MD5. 16 bytes. */ public val fileMd5: ByteArray /* - * 文件大小 + * 视频大小 */ public val fileSize: Long /** - * 文件类型 + * 视频文件类型(拓展名) */ public val fileFormat: String + /* + * 视频文件名,不包括拓展名 + */ + public val fileName: String + @MiraiInternalApi override fun accept(visitor: MessageVisitor, data: D): R { @@ -53,11 +58,6 @@ public interface ShortVideo : MessageContent, ConstrainSingle { @NotStableForInheritance public interface OnlineShortVideo : ShortVideo { - /* - * 文件名 - */ - public val fileName: String - /** * 下载链接 */ diff --git a/mirai-core-api/src/jvmBaseMain/kotlin/contact/Contact.kt b/mirai-core-api/src/jvmBaseMain/kotlin/contact/Contact.kt index 37d7fff4fb..de96956fcf 100644 --- a/mirai-core-api/src/jvmBaseMain/kotlin/contact/Contact.kt +++ b/mirai-core-api/src/jvmBaseMain/kotlin/contact/Contact.kt @@ -98,8 +98,13 @@ public actual interface Contact : ContactOrBot, CoroutineScope { * * @param thumbnail 短视频封面图,为图片资源 * @param video 视频资源,目前仅支持上传 mp4 格式的视频。 + * @param fileName 期望上传的文件名,若为 `null` 则默认为视频资源的 [ExternalResource.md5]。 */ - public actual suspend fun uploadShortVideo(thumbnail: ExternalResource, video: ExternalResource): ShortVideo + public actual suspend fun uploadShortVideo( + thumbnail: ExternalResource, + video: ExternalResource, + fileName: String? + ): ShortVideo public actual companion object { /** diff --git a/mirai-core-mock/src/internal/contact/AbstractMockContact.kt b/mirai-core-mock/src/internal/contact/AbstractMockContact.kt index ef1b40dc7e..8be6eff2a1 100644 --- a/mirai-core-mock/src/internal/contact/AbstractMockContact.kt +++ b/mirai-core-mock/src/internal/contact/AbstractMockContact.kt @@ -58,7 +58,11 @@ internal abstract class AbstractMockContact( return bot.uploadMockImage(resource) } - override suspend fun uploadShortVideo(thumbnail: ExternalResource, video: ExternalResource): ShortVideo { + override suspend fun uploadShortVideo( + thumbnail: ExternalResource, + video: ExternalResource, + fileName: String? + ): ShortVideo { TODO("mock upload short video") } diff --git a/mirai-core/src/commonMain/kotlin/contact/AbstractContact.kt b/mirai-core/src/commonMain/kotlin/contact/AbstractContact.kt index 16779065da..cfc18ac1d1 100644 --- a/mirai-core/src/commonMain/kotlin/contact/AbstractContact.kt +++ b/mirai-core/src/commonMain/kotlin/contact/AbstractContact.kt @@ -38,7 +38,11 @@ internal abstract class AbstractContact( ) : Contact { final override val coroutineContext: CoroutineContext = parentCoroutineContext.childScopeContext() - override suspend fun uploadShortVideo(thumbnail: ExternalResource, video: ExternalResource): ShortVideo { + override suspend fun uploadShortVideo( + thumbnail: ExternalResource, + video: ExternalResource, + fileName: String? + ): ShortVideo { if (this !is Group && this !is Friend) { throw UnsupportedOperationException("short video can only upload to friend or group.") } @@ -51,8 +55,8 @@ internal abstract class AbstractContact( throw EventCancelledException("cancelled by BeforeShortVideoUploadEvent") } - // local uploaded offline short video uses video file md5 as its file name - val videoName = video.md5.toUHexString("") + // local uploaded offline short video uses video file md5 as its file name by default + val videoName = fileName ?: video.md5.toUHexString("") val uploadResp = bot.network.sendAndExpect( PttCenterSvr.GroupShortVideoUpReq( @@ -74,6 +78,7 @@ internal abstract class AbstractContact( if (uploadResp is PttCenterSvr.GroupShortVideoUpReq.Response.FileExists) { return OfflineShortVideoImpl( uploadResp.fileId, + videoName, video.md5, video.size, video.formatName, @@ -133,6 +138,7 @@ internal abstract class AbstractContact( return OfflineShortVideoImpl( highwayUploadResp.fileid, + videoName, video.md5, video.size, video.formatName, diff --git a/mirai-core/src/commonMain/kotlin/message/data/shortVideo.kt b/mirai-core/src/commonMain/kotlin/message/data/shortVideo.kt index 9ec22481b5..99177bee54 100644 --- a/mirai-core/src/commonMain/kotlin/message/data/shortVideo.kt +++ b/mirai-core/src/commonMain/kotlin/message/data/shortVideo.kt @@ -177,6 +177,7 @@ internal class OnlineShortVideoImpl( @Serializable internal class OfflineShortVideoImpl( override val fileId: String, + override val fileName: String, override val fileMd5: ByteArray, override val fileSize: Long, override val fileFormat: String, @@ -190,7 +191,7 @@ internal class OfflineShortVideoImpl( * offline short video uses */ override fun toString(): String { - return "[mirai:svideo:${fileMd5.toUHexString("")}.$fileFormat, video=${fileMd5.toUHexString("")}, " + + return "[mirai:svideo:$fileName.$fileFormat, video=${fileMd5.toUHexString("")}, " + "videoSize=${fileSize}, thumbnail=${thumbMd5.toUHexString("")}, thumbnailSize=${thumbSize}]" } @@ -205,6 +206,7 @@ internal class OfflineShortVideoImpl( other as OfflineShortVideoImpl if (fileId != other.fileId) return false + if (fileName != other.fileName) return false if (!fileMd5.contentEquals(other.fileMd5)) return false if (fileSize != other.fileSize) return false if (fileFormat != other.fileFormat) return false @@ -218,6 +220,7 @@ internal class OfflineShortVideoImpl( override fun hashCode(): Int { var result = fileId.hashCode() + result = 31 * result + fileName.hashCode() result = 31 * result + fileMd5.contentHashCode() result = 31 * result + fileSize.hashCode() result = 31 * result + fileFormat.hashCode() diff --git a/mirai-core/src/commonMain/kotlin/message/protocol/impl/ShortVideoProtocol.kt b/mirai-core/src/commonMain/kotlin/message/protocol/impl/ShortVideoProtocol.kt index 82072f36d6..9219259146 100644 --- a/mirai-core/src/commonMain/kotlin/message/protocol/impl/ShortVideoProtocol.kt +++ b/mirai-core/src/commonMain/kotlin/message/protocol/impl/ShortVideoProtocol.kt @@ -24,7 +24,6 @@ import net.mamoe.mirai.internal.network.protocol.data.proto.ImMsgBody import net.mamoe.mirai.message.data.MessageContent import net.mamoe.mirai.message.data.ShortVideo import net.mamoe.mirai.message.data.SingleMessage -import net.mamoe.mirai.utils.toUHexString internal class ShortVideoProtocol : MessageProtocol() { override fun ProcessorCollector.collectProcessorsImpl() { @@ -56,7 +55,7 @@ internal class ShortVideoProtocol : MessageProtocol() { videoFile = ImMsgBody.VideoFile( fileUuid = data.fileId.encodeToByteArray(), fileMd5 = data.fileMd5, - fileName = (data.fileMd5.toUHexString("") + ".mp4").encodeToByteArray(), + fileName = data.fileName.encodeToByteArray(), fileFormat = 3, // mp4, fileTime = 10, fileSize = data.fileSize.toInt(),