Skip to content

Commit

Permalink
[core] extract fileName from OnlineShortVideo to ShortVideo
Browse files Browse the repository at this point in the history
  • Loading branch information
StageGuard committed Aug 2, 2023
1 parent 1a19b23 commit 53c2946
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 18 deletions.
6 changes: 5 additions & 1 deletion mirai-core-api/src/commonMain/kotlin/contact/Contact.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 9 additions & 9 deletions mirai-core-api/src/commonMain/kotlin/message/data/ShortVideo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <D, R> accept(visitor: MessageVisitor<D, R>, data: D): R {
Expand All @@ -53,11 +58,6 @@ public interface ShortVideo : MessageContent, ConstrainSingle {

@NotStableForInheritance
public interface OnlineShortVideo : ShortVideo {
/*
* 文件名
*/
public val fileName: String

/**
* 下载链接
*/
Expand Down
7 changes: 6 additions & 1 deletion mirai-core-api/src/jvmBaseMain/kotlin/contact/Contact.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down
6 changes: 5 additions & 1 deletion mirai-core-mock/src/internal/contact/AbstractMockContact.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down
12 changes: 9 additions & 3 deletions mirai-core/src/commonMain/kotlin/contact/AbstractContact.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}
Expand All @@ -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(
Expand All @@ -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,
Expand Down Expand Up @@ -133,6 +138,7 @@ internal abstract class AbstractContact(

return OfflineShortVideoImpl(
highwayUploadResp.fileid,
videoName,
video.md5,
video.size,
video.formatName,
Expand Down
5 changes: 4 additions & 1 deletion mirai-core/src/commonMain/kotlin/message/data/shortVideo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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}]"
}

Expand All @@ -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
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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(),
Expand Down

0 comments on commit 53c2946

Please sign in to comment.