refactor(tasks): streamline file removal logic and enhance temporary file handling

This commit is contained in:
keven1024
2026-05-25 12:06:11 +08:00
parent f1e956ad4c
commit ed9d39301f
2 changed files with 9 additions and 14 deletions

View File

@@ -6,7 +6,10 @@ import (
"os"
"path/filepath"
"pkg/models"
pkgservices "pkg/services"
u "pkg/utils"
"strings"
"time"
"github.com/hibiken/asynq"
)
@@ -40,6 +43,10 @@ func RemoveFile(ctx context.Context, task *asynq.Task) error {
return err
}
filePath := filepath.Join(uploadPath, payload.FileId)
// 如果是临时文件删除文件夹
if fileInfo.FileType == models.FileTypeInit {
filePath += "_tmp"
}
if err := rdb.Do(ctx, rdb.B().Hdel().Key("015:fileInfoMap").Field(payload.FileId).Build()).Error(); err != nil {
return err
}

View File

@@ -5,10 +5,9 @@ import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"pkg/geoip"
"pkg/models"
pkgservices "pkg/services"
u "pkg/utils"
"worker/internal/services"
@@ -35,21 +34,10 @@ func RemoveShare(ctx context.Context, task *asynq.Task) error {
})
if len(shareIDs) == 0 {
rdb := u.GetRedisClient()
uploadPath, err := u.GetUploadDirPath()
if err != nil {
return err
}
filePath := filepath.Join(uploadPath, payload.FileId)
if err := rdb.Do(ctx, rdb.B().Hdel().Key("015:fileShareRelational").Field(payload.FileId).Build()).Error(); err != nil {
return err
}
if err := rdb.Do(ctx, rdb.B().Hdel().Key("015:fileInfoMap").Field(payload.FileId).Build()).Error(); err != nil {
return err
}
if err := os.RemoveAll(filePath); err != nil {
return err
}
return nil
return pkgservices.SetFileRemoveTask(payload.FileId, 0)
}
if err := models.SetRedisFileShareRelational(payload.FileId, shareIDs); err != nil {
return err