生成视频并且上传到minio-异步
This commit is contained in:
112
dao/video/concat_task_dao.go
Normal file
112
dao/video/concat_task_dao.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package video
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
dto "media/model/dto/video"
|
||||
entity "media/model/entity/video"
|
||||
|
||||
"gitea.com/red-future/common/db/gfdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
var ConcatTask = new(concatTaskDao)
|
||||
|
||||
type concatTaskDao struct{}
|
||||
|
||||
const concatTaskTable = "concat_task"
|
||||
|
||||
// Insert 创建任务
|
||||
func (d *concatTaskDao) Insert(ctx context.Context, data *entity.ConcatTask) (id int64, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, concatTaskTable).
|
||||
Data(data).
|
||||
Insert()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return r.LastInsertId()
|
||||
}
|
||||
|
||||
// GetByTaskID 根据taskId查询任务
|
||||
func (d *concatTaskDao) GetByTaskID(ctx context.Context, taskID string) (res *entity.ConcatTask, err error) {
|
||||
r, err := gfdb.DB(ctx).Model(ctx, concatTaskTable).
|
||||
Where(entity.ConcatTaskCols.TaskID, taskID).
|
||||
One()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if r == nil {
|
||||
return nil, nil
|
||||
}
|
||||
err = r.Struct(&res)
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateRunning 更新为运行中
|
||||
func (d *concatTaskDao) UpdateRunning(ctx context.Context, taskID string) error {
|
||||
_, err := gfdb.DB(ctx).Model(ctx, concatTaskTable).
|
||||
Data(g.Map{
|
||||
entity.ConcatTaskCols.Status: "running",
|
||||
}).
|
||||
Where(entity.ConcatTaskCols.TaskID, taskID).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdateSuccess 更新为成功
|
||||
func (d *concatTaskDao) UpdateSuccess(ctx context.Context, taskID string, fileURL string, fileSize int64, fileName, fileFormat, fileAddrPrefix, methodUsed, durationStr string) error {
|
||||
_, err := gfdb.DB(ctx).Model(ctx, concatTaskTable).
|
||||
Data(g.Map{
|
||||
entity.ConcatTaskCols.Status: "success",
|
||||
entity.ConcatTaskCols.FileURL: fileURL,
|
||||
entity.ConcatTaskCols.FileSize: fileSize,
|
||||
entity.ConcatTaskCols.FileName: fileName,
|
||||
entity.ConcatTaskCols.FileFormat: fileFormat,
|
||||
entity.ConcatTaskCols.FileAddressPrefix: fileAddrPrefix,
|
||||
entity.ConcatTaskCols.MethodUsed: methodUsed,
|
||||
entity.ConcatTaskCols.DurationStr: durationStr,
|
||||
entity.ConcatTaskCols.ErrorMessage: "",
|
||||
}).
|
||||
Where(entity.ConcatTaskCols.TaskID, taskID).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// UpdateError 更新为失败
|
||||
func (d *concatTaskDao) UpdateError(ctx context.Context, taskID string, errMsg string) error {
|
||||
_, err := gfdb.DB(ctx).Model(ctx, concatTaskTable).
|
||||
Data(g.Map{
|
||||
entity.ConcatTaskCols.Status: "failed",
|
||||
entity.ConcatTaskCols.ErrorMessage: errMsg,
|
||||
}).
|
||||
Where(entity.ConcatTaskCols.TaskID, taskID).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
|
||||
// EntityToTaskRes 实体转DTO
|
||||
func EntityToTaskRes(e *entity.ConcatTask) *dto.GetConcatTaskRes {
|
||||
res := &dto.GetConcatTaskRes{
|
||||
TaskID: e.TaskID,
|
||||
Status: e.Status,
|
||||
CreatedAt: gconv.Int64(e.CreatedAt.Timestamp()),
|
||||
}
|
||||
if e.CreatedAt == nil {
|
||||
res.CreatedAt = time.Now().UnixMilli()
|
||||
}
|
||||
if e.Status == "success" {
|
||||
res.FileURL = e.FileURL
|
||||
res.FileSize = e.FileSize
|
||||
res.FileName = e.FileName
|
||||
res.FileFormat = e.FileFormat
|
||||
res.FileAddressPrefix = e.FileAddressPrefix
|
||||
res.MethodUsed = e.MethodUsed
|
||||
res.DurationStr = e.DurationStr
|
||||
}
|
||||
if e.Status == "failed" {
|
||||
res.ErrorMessage = e.ErrorMessage
|
||||
}
|
||||
return res
|
||||
}
|
||||
Reference in New Issue
Block a user