service/library/contentaudit/video_moderation/task.go

119 lines
3.3 KiB
Go

package videomoderation
import (
"service/api/consts"
video_moderation_proto "service/api/proto/video_moderation/proto"
video_moderation_task_proto "service/api/proto/video_moderation_task/proto"
"service/bizcommon/util"
"service/dbstruct"
"service/library/logger"
"github.com/gin-gonic/gin"
goproto "google.golang.org/protobuf/proto"
)
func AddTasks(tasks []*dbstruct.VideoModerationTask) error {
for _, task := range tasks {
err := AddTask(task)
if err != nil {
return err
}
}
return nil
}
func AddTask(task *dbstruct.VideoModerationTask) error {
if task == nil || task.AuditedMedia == nil {
return nil
}
fragmentNum := len(task.AuditedMedia.GetVideoIds())
if fragmentNum == 0 {
return nil
}
task.BatchId = goproto.String(defaultVideoModerationTaskScheduler.batchId)
task.Status = goproto.Int64(consts.VideoModeration_Created)
if fragmentNum == 1 {
task.IsFragmented = goproto.Int64(0)
task.FragmentsNum = goproto.Int64(1)
// 写入视频审核表
if err := prepareNotFragmentedVideoModerationTask(task); err != nil {
return err
}
} else {
task.IsFragmented = goproto.Int64(1)
task.FragmentsNum = goproto.Int64(int64(fragmentNum))
// 写入视频审核表
if err := prepareFragmentedVideoModerationTask(task); err != nil {
return err
}
}
// 写入视频审核任务表
if err := _DefaultVideoModerationTask.OpCreate(&gin.Context{}, &video_moderation_task_proto.OpCreateReq{
VideoModerationTask: task,
}); err != nil {
logger.Error("Videomoderationtask OpCreate failed: %v", err)
return err
}
return nil
}
// 根据任务信息,创建单个视频的视频审核,并存入数据库
func prepareFragmentedVideoModerationTask(task *dbstruct.VideoModerationTask) (err error) {
ctx := &gin.Context{}
fragmentsNum := task.GetFragmentsNum()
videoModerations := make([]*dbstruct.VideoModeration, fragmentsNum)
// 将媒体拆分为单个视频并存入视频审核表
imageIds := task.AuditedMedia.GetVideoIds()
for i, imageId := range imageIds {
image := &dbstruct.MediaComponent{
VideoIds: util.Int64Slice([]int64{imageId}),
}
videoModerations[i] = &dbstruct.VideoModeration{
AuditedMedia: image,
BatchId: task.BatchId,
Status: goproto.Int64(consts.VideoModeration_Created),
}
}
if err = _DefaultVideoModeration.OpCreateFragmentGroup(ctx, &video_moderation_proto.OpCreateBatchReq{
VideoModerations: videoModerations,
FragmentsNum: fragmentsNum,
}); err != nil {
logger.Error("Videomoderation OpCreateFragmentGroup failed: %v", err)
return
}
moderationFragmentIds := make([]string, fragmentsNum)
for i, videoModeration := range videoModerations {
moderationFragmentIds[i] = videoModeration.GetId()
}
task.VideoModerationFragmentIds = &moderationFragmentIds
return
}
func prepareNotFragmentedVideoModerationTask(task *dbstruct.VideoModerationTask) (err error) {
ctx := &gin.Context{}
videoModeration := &dbstruct.VideoModeration{
AuditedMedia: task.AuditedMedia,
BatchId: task.BatchId,
Status: goproto.Int64(consts.VideoModeration_Created),
}
if err = _DefaultVideoModeration.OpCreate(ctx, &video_moderation_proto.OpCreateReq{
VideoModeration: videoModeration,
}); err != nil {
logger.Error("Videomoderation OpCreate failed: %v", err)
return
}
task.VideoModerationId = videoModeration.Id
return
}