diff --git a/mirai-core/src/commonMain/kotlin/contact/AbstractContact.kt b/mirai-core/src/commonMain/kotlin/contact/AbstractContact.kt index 44ea6bb959..6f53c221f1 100644 --- a/mirai-core/src/commonMain/kotlin/contact/AbstractContact.kt +++ b/mirai-core/src/commonMain/kotlin/contact/AbstractContact.kt @@ -41,42 +41,96 @@ internal abstract class AbstractContact( 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.") - } + ): ShortVideo = thumbnail.withAutoClose { + video.withAutoClose { + if (this !is Group && this !is Friend) { + throw UnsupportedOperationException("short video can only upload to friend or group.") + } - if (video.formatName != "mp4") { - throw UnsupportedOperationException("video format ${video.formatName} is not supported.") - } + if (video.formatName != "mp4") { + throw UnsupportedOperationException("video format ${video.formatName} is not supported.") + } - if (BeforeShortVideoUploadEvent(this, thumbnail, video).broadcast().isCancelled) { - throw EventCancelledException("cancelled by BeforeShortVideoUploadEvent") - } + if (BeforeShortVideoUploadEvent(this, thumbnail, video).broadcast().isCancelled) { + throw EventCancelledException("cancelled by BeforeShortVideoUploadEvent") + } - // 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( - client = bot.client, - contact = this, - thumbnailFileMd5 = thumbnail.md5, - thumbnailFileSize = thumbnail.size, - videoFileName = videoName, - videoFileMd5 = video.md5, - videoFileSize = video.size, - videoFileFormat = video.formatName + // 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( + client = bot.client, + contact = this, + thumbnailFileMd5 = thumbnail.md5, + thumbnailFileSize = thumbnail.size, + videoFileName = videoName, + videoFileMd5 = video.md5, + videoFileSize = video.size, + videoFileFormat = video.formatName + ) ) - ) - // get thumbnail image width and height - val thumbnailInfo = thumbnail.calculateImageInfo() + // get thumbnail image width and height + val thumbnailInfo = thumbnail.calculateImageInfo() + + // fast path + if (uploadResp is PttCenterSvr.GroupShortVideoUpReq.Response.FileExists) { + return OfflineShortVideoImpl( + uploadResp.fileId, + videoName, + video.md5, + video.size, + video.formatName, + thumbnail.md5, + thumbnail.size, + thumbnailInfo.width, + thumbnailInfo.height + ).also { + ShortVideoUploadEvent.Succeed(this, thumbnail, video, it).broadcast() + } + } + + val highwayRespExt = CombinedExternalResource(thumbnail, video).use { resource -> + Highway.uploadResourceBdh( + bot = bot, + resource = resource, + kind = ResourceKind.SHORT_VIDEO, + commandId = 25, + extendInfo = buildPacket { + writeProtoBuf( + PttShortVideo.PttShortVideoUploadReq.serializer(), + PttCenterSvr.GroupShortVideoUpReq.buildShortVideoFileInfo( + client = bot.client, + contact = this@AbstractContact, + thumbnailFileMd5 = thumbnail.md5, + thumbnailFileSize = thumbnail.size, + videoFileName = videoName, + videoFileMd5 = video.md5, + videoFileSize = video.size, + videoFileFormat = video.formatName + ) + ) + }.readBytes(), + encrypt = true + ).extendInfo + } + + if (highwayRespExt == null) { + ShortVideoUploadEvent.Failed( + this, + thumbnail, + video, + -1, + "highway upload short video failed, extendInfo is null." + ).broadcast() + error("highway upload short video failed, extendInfo is null.") + } + + val highwayUploadResp = highwayRespExt.loadAs(PttShortVideo.PttShortVideoUploadResp.serializer()) - // fast path - if (uploadResp is PttCenterSvr.GroupShortVideoUpReq.Response.FileExists) { - return OfflineShortVideoImpl( - uploadResp.fileId, + OfflineShortVideoImpl( + highwayUploadResp.fileid, videoName, video.md5, video.size, @@ -89,58 +143,6 @@ internal abstract class AbstractContact( ShortVideoUploadEvent.Succeed(this, thumbnail, video, it).broadcast() } } - - val highwayRespExt = CombinedExternalResource(thumbnail, video).use { resource -> - Highway.uploadResourceBdh( - bot = bot, - resource = resource, - kind = ResourceKind.SHORT_VIDEO, - commandId = 25, - extendInfo = buildPacket { - writeProtoBuf( - PttShortVideo.PttShortVideoUploadReq.serializer(), - PttCenterSvr.GroupShortVideoUpReq.buildShortVideoFileInfo( - client = bot.client, - contact = this@AbstractContact, - thumbnailFileMd5 = thumbnail.md5, - thumbnailFileSize = thumbnail.size, - videoFileName = videoName, - videoFileMd5 = video.md5, - videoFileSize = video.size, - videoFileFormat = video.formatName - ) - ) - }.readBytes(), - encrypt = true - ).extendInfo - } - - if (highwayRespExt == null) { - ShortVideoUploadEvent.Failed( - this, - thumbnail, - video, - -1, - "highway upload short video failed, extendInfo is null." - ).broadcast() - error("highway upload short video failed, extendInfo is null.") - } - - val highwayUploadResp = highwayRespExt.loadAs(PttShortVideo.PttShortVideoUploadResp.serializer()) - - return OfflineShortVideoImpl( - highwayUploadResp.fileid, - videoName, - video.md5, - video.size, - video.formatName, - thumbnail.md5, - thumbnail.size, - thumbnailInfo.width, - thumbnailInfo.height - ).also { - ShortVideoUploadEvent.Succeed(this, thumbnail, video, it).broadcast() - } } }