feat(backend): add file and share removal tasks with Redis integration for better file management

This commit is contained in:
keven1024
2025-05-25 23:41:02 +08:00
parent c32cffeb83
commit c5ee629053
6 changed files with 130 additions and 1 deletions

View File

@@ -14,6 +14,7 @@ require (
github.com/redis/go-redis/v9 v9.7.0 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/sagikazarmark/locafero v0.7.0 // indirect
github.com/samber/lo v1.50.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.12.0 // indirect
github.com/spf13/cast v1.8.0 // indirect
@@ -24,7 +25,7 @@ require (
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/sys v0.29.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/text v0.22.0 // indirect
golang.org/x/time v0.8.0 // indirect
google.golang.org/protobuf v1.36.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect

View File

@@ -25,6 +25,8 @@ github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/sagikazarmark/locafero v0.7.0 h1:5MqpDsTGNDhY8sGp0Aowyf0qKsPrhewaLSsFaodPcyo=
github.com/sagikazarmark/locafero v0.7.0/go.mod h1:2za3Cg5rMaTMoG/2Ulr9AwtFaIppKXTRYnozin4aB5k=
github.com/samber/lo v1.50.0 h1:XrG0xOeHs+4FQ8gJR97zDz5uOFMW7OwFWiFVzqopKgY=
github.com/samber/lo v1.50.0/go.mod h1:RjZyNk6WSnUFRKK6EyOhsRJMqft3G+pg7dCWHQCWvsc=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero v1.12.0 h1:UcOPyRBYczmFn6yvphxkn9ZEOY65cpwGKb5mL36mrqs=
@@ -57,6 +59,8 @@ golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/text v0.22.0 h1:bofq7m3/HAFvbF51jz3Q9wLg3jkvSPuiZu/pD1XwgtM=
golang.org/x/text v0.22.0/go.mod h1:YRoo4H8PVmsu+E3Ou7cqLVH8oXWIHVoX0jqUWALQhfY=
golang.org/x/time v0.8.0 h1:9i3RxcPv3PZnitoVGMPDKZSq1xW1gK1Xy3ArNOGZfEg=
golang.org/x/time v0.8.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=

View File

@@ -0,0 +1,31 @@
package models
import (
"encoding/json"
"worker/internal/utils"
"github.com/redis/go-redis/v9"
)
func GetRedisFileShareRelational(fileId string) ([]string, error) {
rdb, ctx := utils.GetRedisClient()
fileShareRelationalUnmarshalData, err := rdb.HGet(ctx, "015:fileShareRelational", fileId).Result()
if err == redis.Nil {
return nil, nil
}
if err != nil {
return nil, err
}
var shareIDs []string
if err := json.Unmarshal([]byte(fileShareRelationalUnmarshalData), &shareIDs); err != nil {
return nil, err
}
return shareIDs, nil
}
func SetRedisFileShareRelational(fileId string, shareIDs []string) error {
rdb, ctx := utils.GetRedisClient()
jsonData, _ := json.Marshal(shareIDs)
_, err := rdb.HSet(ctx, "015:fileShareRelational", fileId, string(jsonData)).Result()
return err
}

View File

@@ -0,0 +1,41 @@
package tasks
import (
"context"
"encoding/json"
"os"
"path/filepath"
"worker/internal/models"
"worker/internal/utils"
"github.com/hibiken/asynq"
)
type RemoveFileTaskPayload struct {
FileId string `json:"file_id"`
}
func RemoveFile(ctx context.Context, task *asynq.Task) error {
var payload RemoveFileTaskPayload
if err := json.Unmarshal(task.Payload(), &payload); err != nil {
return err
}
fileInfo, err := models.GetRedisFileInfo(payload.FileId)
if err != nil {
return err
}
if fileInfo == nil || fileInfo.FileType == models.FileTypeUpload {
return nil
}
rdb, rctx := utils.GetRedisClient()
uploadPath, err := utils.GetUploadDirPath()
if err != nil {
return err
}
filePath := filepath.Join(uploadPath, payload.FileId)
rdb.HDel(rctx, "015:fileInfoMap", payload.FileId)
os.RemoveAll(filePath)
return nil
}

View File

@@ -0,0 +1,50 @@
package tasks
import (
"context"
"encoding/json"
"os"
"path/filepath"
"worker/internal/models"
"worker/internal/utils"
"github.com/hibiken/asynq"
"github.com/samber/lo"
)
type ShareRemoveTaskPayload struct {
ShareId string `json:"share_id"`
FileId string `json:"file_id"`
}
func RemoveShare(ctx context.Context, task *asynq.Task) error {
var payload ShareRemoveTaskPayload
if err := json.Unmarshal(task.Payload(), &payload); err != nil {
return err
}
shareIDs, err := models.GetRedisFileShareRelational(payload.FileId)
if err != nil {
return err
}
shareIDs = lo.Filter(shareIDs, func(x string, _ int) bool {
return x != payload.ShareId
})
if len(shareIDs) == 0 {
rdb, ctx := utils.GetRedisClient()
uploadPath, err := utils.GetUploadDirPath()
if err != nil {
return err
}
filePath := filepath.Join(uploadPath, payload.FileId)
rdb.HDel(ctx, "015:fileShareRelational", payload.FileId)
rdb.HDel(ctx, "015:fileInfoMap", payload.FileId)
os.RemoveAll(filePath)
return nil
}
models.SetRedisFileShareRelational(payload.FileId, shareIDs)
return nil
}
func ShareNotify(ctx context.Context, task *asynq.Task) error {
return nil
}

View File

@@ -29,6 +29,8 @@ func main() {
mux := asynq.NewServeMux()
mux.Use(middleware.LoggerMiddleware)
mux.HandleFunc("share:remove", tasks.RemoveShare)
mux.HandleFunc("file:remove", tasks.RemoveFile)
mux.HandleFunc("image:compress", tasks.CompressImage)
if err := srv.Run(mux); err != nil {