feat: update jobs

This commit is contained in:
yanghao05
2025-04-22 20:01:50 +08:00
parent 163a7c11fe
commit 707cbbb639
21 changed files with 359 additions and 87 deletions

View File

@@ -62,7 +62,6 @@ func (w *VideoExtractHeadImageWorker) Work(ctx context.Context, job *Job[VideoEx
log.Errorf("Error getting media by ID: %v", err)
return JobCancel(err)
}
_ = media
input := filepath.Join(w.app.StoragePath, media.Path)
output := input[:len(input)-len(filepath.Ext(input))] + ".jpg"
@@ -73,12 +72,6 @@ func (w *VideoExtractHeadImageWorker) Work(ctx context.Context, job *Job[VideoEx
}
defer os.RemoveAll(output)
// Upload the image to OSS
if err := w.oss.Upload(ctx, output, filepath.Base(output)); err != nil {
log.Errorf("Error uploading image to OSS: %v", err)
return JobCancel(err)
}
fileSize, err := utils.GetFileSize(output)
if err != nil {
log.Errorf("Error getting file size: %v", err)
@@ -92,15 +85,29 @@ func (w *VideoExtractHeadImageWorker) Work(ctx context.Context, job *Job[VideoEx
}
filename := fileMd5 + filepath.Ext(output)
name := "[展示图]" + media.Name + ".jpg"
// create a new media record for the image
imageMedia := &model.Medias{
CreatedAt: time.Now(),
Name: "[展示图]" + media.Name,
Name: name,
MimeType: "image/jpeg",
Size: fileSize,
Path: w.oss.GetSavePath(filename),
Hash: fileMd5,
Metas: fields.Json[fields.MediaMetas]{},
Metas: fields.ToJson(fields.MediaMetas{
ParentHash: media.Hash,
}),
}
// upload to oss
if err := w.oss.Upload(ctx, output, imageMedia.Path); err != nil {
log.Errorf("Error uploading image to OSS: %v", err)
return JobCancel(err)
}
if w.job.Add(&RemoveFile{FilePath: output}); err != nil {
log.Errorf("Error removing original file: %v", err)
}
if err := models.Medias.Create(ctx, imageMedia); err != nil {
@@ -108,5 +115,15 @@ func (w *VideoExtractHeadImageWorker) Work(ctx context.Context, job *Job[VideoEx
return JobCancel(err)
}
dst := filepath.Join(w.app.StoragePath, media.Path)
if w.job.Add(&RemoveFile{FilePath: dst}); err != nil {
log.Errorf("Error removing original file: %v", err)
}
if w.job.Add(&PublishDraftPosts{MediaHash: media.Hash}); err != nil {
log.Errorf("Error adding job: %v", err)
return JobCancel(err)
}
return nil
}