feat: enqueue media asset processing
This commit is contained in:
@@ -18,10 +18,12 @@ import (
|
||||
|
||||
"quyun/v2/app/errorx"
|
||||
common_dto "quyun/v2/app/http/v1/dto"
|
||||
"quyun/v2/app/jobs/args"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database/fields"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/consts"
|
||||
"quyun/v2/providers/job"
|
||||
"quyun/v2/providers/storage"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -33,6 +35,7 @@ import (
|
||||
// @provider
|
||||
type common struct {
|
||||
storage *storage.Storage
|
||||
job *job.Job
|
||||
}
|
||||
|
||||
func (s *common) Options(ctx context.Context) (*common_dto.OptionsResponse, error) {
|
||||
@@ -88,7 +91,7 @@ func (s *common) CheckHash(ctx context.Context, tenantID, userID int64, hash str
|
||||
TenantID: tid,
|
||||
UserID: userID,
|
||||
Type: existing.Type,
|
||||
Status: consts.MediaAssetStatusUploaded,
|
||||
Status: existing.Status,
|
||||
Provider: existing.Provider,
|
||||
Bucket: existing.Bucket,
|
||||
ObjectKey: existing.ObjectKey,
|
||||
@@ -99,6 +102,9 @@ func (s *common) CheckHash(ctx context.Context, tenantID, userID int64, hash str
|
||||
if err := models.MediaAssetQuery.WithContext(ctx).Create(asset); err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
if err := s.enqueueMediaProcess(ctx, tid, asset); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.composeUploadResult(asset), nil
|
||||
}
|
||||
@@ -341,7 +347,7 @@ func (s *common) CompleteUpload(ctx context.Context, tenantID, userID int64, for
|
||||
TenantID: tid,
|
||||
UserID: userID,
|
||||
Type: consts.MediaAssetType(meta.Type),
|
||||
Status: consts.MediaAssetStatusUploaded,
|
||||
Status: existing.Status,
|
||||
Provider: existing.Provider,
|
||||
Bucket: existing.Bucket,
|
||||
ObjectKey: existing.ObjectKey,
|
||||
@@ -360,7 +366,7 @@ func (s *common) CompleteUpload(ctx context.Context, tenantID, userID int64, for
|
||||
TenantID: tid,
|
||||
UserID: userID,
|
||||
Type: consts.MediaAssetType(meta.Type),
|
||||
Status: consts.MediaAssetStatusUploaded,
|
||||
Status: s.initialMediaStatus(consts.MediaAssetType(meta.Type)),
|
||||
Provider: s.storage.Provider(),
|
||||
Bucket: s.storage.Bucket(),
|
||||
ObjectKey: objectKey,
|
||||
@@ -376,6 +382,9 @@ func (s *common) CompleteUpload(ctx context.Context, tenantID, userID int64, for
|
||||
if err := models.MediaAssetQuery.WithContext(ctx).Create(asset); err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
if err := s.enqueueMediaProcess(ctx, tid, asset); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.composeUploadResult(asset), nil
|
||||
}
|
||||
@@ -505,7 +514,7 @@ func (s *common) Upload(
|
||||
TenantID: tid,
|
||||
UserID: userID,
|
||||
Type: consts.MediaAssetType(typeArg),
|
||||
Status: consts.MediaAssetStatusUploaded,
|
||||
Status: existing.Status,
|
||||
Provider: existing.Provider,
|
||||
Bucket: existing.Bucket,
|
||||
ObjectKey: existing.ObjectKey, // Reuse key
|
||||
@@ -527,7 +536,7 @@ func (s *common) Upload(
|
||||
TenantID: tid,
|
||||
UserID: userID,
|
||||
Type: consts.MediaAssetType(typeArg),
|
||||
Status: consts.MediaAssetStatusUploaded,
|
||||
Status: s.initialMediaStatus(consts.MediaAssetType(typeArg)),
|
||||
Provider: s.storage.Provider(),
|
||||
Bucket: s.storage.Bucket(),
|
||||
ObjectKey: objectKey,
|
||||
@@ -542,6 +551,9 @@ func (s *common) Upload(
|
||||
if err := models.MediaAssetQuery.WithContext(ctx).Create(asset); err != nil {
|
||||
return nil, errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
if err := s.enqueueMediaProcess(ctx, tid, asset); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.composeUploadResult(asset), nil
|
||||
}
|
||||
@@ -575,6 +587,48 @@ func (s *common) GetAssetURL(objectKey string) string {
|
||||
return url
|
||||
}
|
||||
|
||||
func (s *common) initialMediaStatus(mediaType consts.MediaAssetType) consts.MediaAssetStatus {
|
||||
if s.needsMediaProcess(mediaType) {
|
||||
return consts.MediaAssetStatusUploaded
|
||||
}
|
||||
return consts.MediaAssetStatusReady
|
||||
}
|
||||
|
||||
func (s *common) needsMediaProcess(mediaType consts.MediaAssetType) bool {
|
||||
return mediaType == consts.MediaAssetTypeVideo
|
||||
}
|
||||
|
||||
func (s *common) enqueueMediaProcess(ctx context.Context, tenantID int64, asset *models.MediaAsset) error {
|
||||
if asset == nil {
|
||||
return nil
|
||||
}
|
||||
if !s.needsMediaProcess(asset.Type) {
|
||||
return nil
|
||||
}
|
||||
// 测试环境或本地快速验证时直接模拟处理,避免依赖任务系统。
|
||||
if os.Getenv("JOB_INLINE") == "1" {
|
||||
_, err := models.MediaAssetQuery.WithContext(ctx).
|
||||
Where(models.MediaAssetQuery.ID.Eq(asset.ID)).
|
||||
UpdateSimple(
|
||||
models.MediaAssetQuery.Status.Value(consts.MediaAssetStatusReady),
|
||||
models.MediaAssetQuery.UpdatedAt.Value(time.Now()),
|
||||
)
|
||||
if err != nil {
|
||||
return errorx.ErrDatabaseError.WithCause(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
arg := args.MediaAssetProcessJob{
|
||||
TenantID: tenantID,
|
||||
AssetID: asset.ID,
|
||||
}
|
||||
if err := s.job.Add(arg); err != nil {
|
||||
return errorx.ErrInternalError.WithCause(err).WithMsg("添加媒体处理任务失败")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func retryCriticalWrite(ctx context.Context, fn func() error) error {
|
||||
backoffs := []time.Duration{
|
||||
50 * time.Millisecond,
|
||||
|
||||
@@ -22,9 +22,11 @@ func Provide(opts ...opt.Option) error {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func(
|
||||
job *job.Job,
|
||||
storage *storage.Storage,
|
||||
) (*common, error) {
|
||||
obj := &common{
|
||||
job: job,
|
||||
storage: storage,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user