mirror of
https://github.com/keven1024/015.git
synced 2026-05-26 07:08:02 +00:00
refactor(backend): move RedisShareInfo struct to a new file and update its definition for improved clarity and organization
This commit is contained in:
@@ -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()
|
||||
|
||||
61
backend/internal/models/share.go
Normal file
61
backend/internal/models/share.go
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user