From 3b7e687a9e6458bd56920c7ce90d333a720ec984 Mon Sep 17 00:00:00 2001 From: keven1024 <99848979+keven1024@users.noreply.github.com> Date: Fri, 2 May 2025 20:47:54 +0800 Subject: [PATCH] refactor(backend): move RedisShareInfo struct to a new file and update its definition for improved clarity and organization --- backend/internal/models/file.go | 7 ---- backend/internal/models/share.go | 61 ++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 backend/internal/models/share.go diff --git a/backend/internal/models/file.go b/backend/internal/models/file.go index 96033ab..b8a816a 100644 --- a/backend/internal/models/file.go +++ b/backend/internal/models/file.go @@ -29,13 +29,6 @@ type RedisFileInfo struct { Expire int64 `json:"expire"` } -type RedisShareInfo struct { - Id string `json:"id"` - Owner string `json:"owner"` - FileId string `json:"fileId"` - CreatedAt int64 `json:"created_at"` -} - func GetRedisFileInfo(fileId string) (*RedisFileInfo, error) { rdb, ctx := utils.GetRedisClient() fileInfoUnmarshalData, err := rdb.HGet(ctx, "015:fileInfoMap", fileId).Result() diff --git a/backend/internal/models/share.go b/backend/internal/models/share.go new file mode 100644 index 0000000..e78df61 --- /dev/null +++ b/backend/internal/models/share.go @@ -0,0 +1,61 @@ +package models + +import ( + "backend/internal/utils" + "encoding/json" + "fmt" + "time" + + "dario.cat/mergo" + "github.com/redis/go-redis/v9" +) + +type RedisShareInfo struct { + // Id string `json:"id"` + CreatedAt time.Time `json:"created_at"` + Owner string `json:"owner"` + Type ShareType `json:"type"` + Data string `json:"data"` // 分享数据 文件分享为文件id 文本分享为文本内容 + ExpireAt time.Time `json:"expire_time"` + ViewNum int64 `json:"download_nums"` + Password string `json:"password"` + NotifyEmail []string `json:"notify_email"` + // PickupCode bool `json:"pickup_code"` +} + +type ShareType string + +const ( + ShareTypeFile ShareType = "file" + ShareTypeText ShareType = "text" +) + +func GetRedisShareInfo(shareId string) (*RedisShareInfo, error) { + rdb, ctx := utils.GetRedisClient() + shareInfoUnmarshalData, err := rdb.Get(ctx, fmt.Sprintf("015:shareInfoMap:%s", shareId)).Result() + if err == redis.Nil { + return nil, nil + } + if err != nil { + return nil, err + } + var shareInfoData RedisShareInfo + if err := json.Unmarshal([]byte(shareInfoUnmarshalData), &shareInfoData); err != nil { + return nil, err + } + return &shareInfoData, nil +} + +func SetRedisShareInfo(shareId string, shareInfo RedisShareInfo, ttl time.Duration) error { + rdb, ctx := utils.GetRedisClient() + old_shareInfo, err := GetRedisShareInfo(shareId) + if err != nil { + return err + } + if old_shareInfo != nil { + mergo.Merge(&shareInfo, old_shareInfo) + } + jsonData, _ := json.Marshal(shareInfo) + _, err = rdb.Set(ctx, fmt.Sprintf("015:shareInfoMap:%s", shareId), string(jsonData), ttl).Result() + return err +}