feat(storage): 更新存储结构以使用新的存储提供者并优化文件上传和下载逻辑
This commit is contained in:
@@ -5,13 +5,15 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"quyun/v2/app/services"
|
||||
"quyun/v2/providers/storage"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type Storage struct{}
|
||||
type Storage struct {
|
||||
storage *storage.Storage
|
||||
}
|
||||
|
||||
// Upload file
|
||||
//
|
||||
@@ -28,12 +30,12 @@ type Storage struct{}
|
||||
// @Bind expires query
|
||||
// @Bind sign query
|
||||
func (s *Storage) Upload(ctx fiber.Ctx, key, expires, sign string) (string, error) {
|
||||
if err := services.Storage.Verify("PUT", key, expires, sign); err != nil {
|
||||
if err := s.storage.Verify("PUT", key, expires, sign); err != nil {
|
||||
return "", fiber.NewError(fiber.StatusForbidden, err.Error())
|
||||
}
|
||||
|
||||
// Save file
|
||||
localPath := services.Storage.Config.LocalPath
|
||||
localPath := s.storage.Config.LocalPath
|
||||
if localPath == "" {
|
||||
localPath = "./storage"
|
||||
}
|
||||
@@ -70,11 +72,11 @@ func (s *Storage) Upload(ctx fiber.Ctx, key, expires, sign string) (string, erro
|
||||
// @Bind expires query
|
||||
// @Bind sign query
|
||||
func (s *Storage) Download(ctx fiber.Ctx, key, expires, sign string) error {
|
||||
if err := services.Storage.Verify("GET", key, expires, sign); err != nil {
|
||||
if err := s.storage.Verify("GET", key, expires, sign); err != nil {
|
||||
return fiber.NewError(fiber.StatusForbidden, err.Error())
|
||||
}
|
||||
|
||||
localPath := services.Storage.Config.LocalPath
|
||||
localPath := s.storage.Config.LocalPath
|
||||
if localPath == "" {
|
||||
localPath = "./storage"
|
||||
}
|
||||
|
||||
@@ -2,17 +2,17 @@ package jobs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/riverqueue/river"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"quyun/v2/app/jobs/args"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/consts"
|
||||
"quyun/v2/providers/storage"
|
||||
|
||||
"github.com/riverqueue/river"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// @provider(job)
|
||||
@@ -30,7 +30,9 @@ func (j *MediaProcessWorker) Work(ctx context.Context, job *river.Job[args.Media
|
||||
}
|
||||
|
||||
// 2. Update status to processing
|
||||
_, err = models.MediaAssetQuery.WithContext(ctx).Where(models.MediaAssetQuery.ID.Eq(asset.ID)).UpdateSimple(models.MediaAssetQuery.Status.Value(consts.MediaAssetStatusProcessing))
|
||||
_, err = models.MediaAssetQuery.WithContext(ctx).
|
||||
Where(models.MediaAssetQuery.ID.Eq(asset.ID)).
|
||||
UpdateSimple(models.MediaAssetQuery.Status.Value(consts.MediaAssetStatusProcessing))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -50,7 +52,18 @@ func (j *MediaProcessWorker) Work(ctx context.Context, job *river.Job[args.Media
|
||||
coverFile := filepath.Join(outputDir, filepath.Base(coverKey))
|
||||
|
||||
// Generate Cover
|
||||
cmd := exec.CommandContext(ctx, "ffmpeg", "-y", "-i", inputFile, "-ss", "00:00:01.000", "-vframes", "1", coverFile)
|
||||
cmd := exec.CommandContext(
|
||||
ctx,
|
||||
"ffmpeg",
|
||||
"-y",
|
||||
"-i",
|
||||
inputFile,
|
||||
"-ss",
|
||||
"00:00:01.000",
|
||||
"-vframes",
|
||||
"1",
|
||||
coverFile,
|
||||
)
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
log.Errorf("ffmpeg failed: %s, output: %s", err, string(out))
|
||||
// Don't fail the job, just skip cover
|
||||
@@ -64,9 +77,11 @@ func (j *MediaProcessWorker) Work(ctx context.Context, job *river.Job[args.Media
|
||||
}
|
||||
|
||||
// 4. Update status to ready
|
||||
_, err = models.MediaAssetQuery.WithContext(ctx).Where(models.MediaAssetQuery.ID.Eq(asset.ID)).Updates(&models.MediaAsset{
|
||||
Status: consts.MediaAssetStatusReady,
|
||||
UpdatedAt: time.Now(),
|
||||
})
|
||||
_, err = models.MediaAssetQuery.WithContext(ctx).
|
||||
Where(models.MediaAssetQuery.ID.Eq(asset.ID)).
|
||||
Updates(&models.MediaAsset{
|
||||
Status: consts.MediaAssetStatusReady,
|
||||
UpdatedAt: time.Now(),
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user