service/app/mix/service/video_moderation_service.go

143 lines
4.2 KiB
Go

package service
import (
"service/api/consts"
"service/bizcommon/util"
"service/dbstruct"
"service/library/logger"
video_moderation_proto "service/api/proto/video_moderation/proto"
video_moderation_task_proto "service/api/proto/video_moderation_task/proto"
"github.com/gin-gonic/gin"
goproto "google.golang.org/protobuf/proto"
)
func (s *Service) CreateZoneMomentVideoModeration(ctx *gin.Context, newZoneMoment *dbstruct.ZoneMoment) (tasks []*dbstruct.VideoModerationTask) {
if newZoneMoment.MediaComp != nil && len(newZoneMoment.MediaComp.GetVideoIds()) > 0 {
tasks = append(tasks, &dbstruct.VideoModerationTask{
RouteUrl: goproto.String(ctx.Request.URL.Path),
AssociativeDatabase: goproto.String("zone_moment"),
AssociativeTableName: goproto.String("zone_moment"),
AssociativeTableId: newZoneMoment.Id,
AssociativeTableColumn: goproto.String("media_component"),
AuditedMedia: newZoneMoment.MediaComp,
OldMedia: nil,
IsAligned: goproto.Int64(consts.VideoModerationIsAligned_Yes),
})
}
addVideoModerationTasks(ctx, tasks)
return
}
func addVideoModerationTasks(ctx *gin.Context, tasks []*dbstruct.VideoModerationTask) error {
for _, task := range tasks {
err := addVideoModerationTask(ctx, task)
if err != nil {
return err
}
}
return nil
}
func addVideoModerationTask(ctx *gin.Context, task *dbstruct.VideoModerationTask) error {
if task == nil || task.AuditedMedia == nil {
return nil
}
fragmentNum := len(task.AuditedMedia.GetVideoIds())
if fragmentNum == 0 {
return nil
}
batchId, err := _DefaultContentAuditRTI.GetVideoModerationBatchId(ctx)
if err != nil {
return err
}
task.BatchId = goproto.String(batchId)
task.Status = goproto.Int64(consts.VideoModeration_Created)
if fragmentNum == 1 {
task.IsFragmented = goproto.Int64(0)
task.FragmentsNum = goproto.Int64(1)
// 写入视频审核表
if err := prepareNotFragmentedVideoModerationTask(ctx, task); err != nil {
return err
}
} else {
task.IsFragmented = goproto.Int64(1)
task.FragmentsNum = goproto.Int64(int64(fragmentNum))
// 写入视频审核表
if err := prepareFragmentedVideoModerationTask(ctx, 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(ctx *gin.Context, task *dbstruct.VideoModerationTask) (err error) {
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(ctx *gin.Context, task *dbstruct.VideoModerationTask) (err error) {
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
}