mirror of
https://github.com/keven1024/015.git
synced 2026-05-26 07:08:02 +00:00
feat(worker): introduce ErrFileNotFound for consistent error handling and update GenStandardFile to use it
This commit is contained in:
7
worker/internal/services/errors.go
Normal file
7
worker/internal/services/errors.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package services
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrFileNotFound = errors.New("FileNotFound")
|
||||
)
|
||||
@@ -1,7 +1,6 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"pkg/models"
|
||||
@@ -20,7 +19,7 @@ type GenStandardFileReturn struct {
|
||||
// 生成标准格式的file
|
||||
func GenStandardFile(filePath string, mimeType string) (GenStandardFileReturn, error) {
|
||||
if _, err := os.Stat(filePath); os.IsNotExist(err) {
|
||||
return GenStandardFileReturn{}, errors.New("文件不存在")
|
||||
return GenStandardFileReturn{}, ErrFileNotFound
|
||||
}
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
|
||||
@@ -3,6 +3,8 @@ package tasks
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"mime"
|
||||
"path/filepath"
|
||||
"pkg/models"
|
||||
@@ -28,6 +30,9 @@ func CompressImage(ctx context.Context, task *asynq.Task) error {
|
||||
originalPath := filepath.Join(uploadPath, payload.FileId)
|
||||
compressedPath, err := services.CompressImage(originalPath, originalFileInfo.MimeType)
|
||||
if err != nil {
|
||||
if errors.Is(err, services.ErrUnsupportedMimeType) {
|
||||
return fmt.Errorf("%w: %w", err, asynq.SkipRetry)
|
||||
}
|
||||
return err
|
||||
}
|
||||
compressedFileInfo, err := services.GenStandardFile(compressedPath, originalFileInfo.MimeType)
|
||||
@@ -70,6 +75,9 @@ func ConvertImage(ctx context.Context, task *asynq.Task) error {
|
||||
originalPath := filepath.Join(uploadPath, payload.FileId)
|
||||
convertedPath, err := services.ConvertImageWithMagick(originalPath, originalFileInfo.MimeType, payload.TargetExt)
|
||||
if err != nil {
|
||||
if errors.Is(err, services.ErrUnsupportedMimeType) {
|
||||
return fmt.Errorf("%w: %w", err, asynq.SkipRetry)
|
||||
}
|
||||
return err
|
||||
}
|
||||
mimeType := mime.TypeByExtension(payload.TargetExt)
|
||||
|
||||
Reference in New Issue
Block a user