Skip to content

Commit

Permalink
[core] auto close resource
Browse files Browse the repository at this point in the history
  • Loading branch information
StageGuard committed Aug 4, 2023
1 parent 461f612 commit 342b459
Showing 1 changed file with 84 additions and 82 deletions.
166 changes: 84 additions & 82 deletions mirai-core/src/commonMain/kotlin/contact/AbstractContact.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
}
}
}

Expand Down

0 comments on commit 342b459

Please sign in to comment.