refactor(backend): update ShareConfig to use integer for expire_time and improve error handling in CreateShareInfo

This commit is contained in:
keven1024
2025-05-05 23:38:10 +08:00
parent a78eb2efa3
commit 9b2057e6ed
2 changed files with 20 additions and 14 deletions

View File

@@ -20,7 +20,7 @@ type CreateShareProps struct {
}
type ShareConfig struct {
ExpireAt string `json:"expire_time"`
ExpireAt int `json:"expire_time"`
ViewNum int64 `json:"download_nums"`
HasPassword bool `json:"has_password"`
Password string `json:"password"`
@@ -36,11 +36,10 @@ func CreateShareInfo(c echo.Context) error {
if err := cc.Bind(r); err != nil {
return utils.HTTPErrorHandler(c, err)
}
ExpireTime, err := time.Parse(time.RFC3339, r.Config.ExpireAt)
if err != nil {
return utils.HTTPErrorHandler(c, errors.New("分享过期时间格式错误"))
if r.Config.ExpireAt < 60 {
return utils.HTTPErrorHandler(c, errors.New("非法的分享过期时间"))
}
ExpireTime := time.Now().Add(time.Minute * time.Duration(r.Config.ExpireAt))
if r.Data == "" || (r.Type != models.ShareTypeFile && r.Type != models.ShareTypeText) || ExpireTime.Before(time.Now()) || r.Config.ViewNum < 1 {
return utils.HTTPErrorHandler(c, errors.New("调用接口参数错误"))
@@ -65,16 +64,16 @@ func CreateShareInfo(c echo.Context) error {
}
models.SetRedisShareInfo(id, models.RedisShareInfo{
Data: r.Data,
Type: r.Type,
CreatedAt: time.Now().Unix(),
Owner: cc.Auth.(string),
// ExpireAt: r.Config.ExpireAt,
Data: r.Data,
Type: r.Type,
CreatedAt: time.Now().Unix(),
Owner: cc.Auth.(string),
ViewNum: r.Config.ViewNum,
Password: r.Config.Password,
NotifyEmail: r.Config.NotifyEmail,
FileName: r.FileName,
}, time.Until(ExpireTime))
ExpireAt: ExpireTime.Unix(),
})
return utils.HTTPSuccessHandler(c, map[string]any{
"id": id,
@@ -116,6 +115,7 @@ func GetShareInfo(c echo.Context) error {
}
return utils.HTTPSuccessHandler(c, map[string]any{
"id": shareId,
"type": shareInfo.Type,
"file_name": shareInfo.FileName,
"download_nums": shareInfo.ViewNum,
"expire_at": shareInfo.ExpireAt,
@@ -127,6 +127,7 @@ func GetShareInfo(c echo.Context) error {
return utils.HTTPSuccessHandler(c, map[string]any{
"id": shareId,
"type": shareInfo.Type,
"file_name": shareInfo.FileName,
"download_nums": shareInfo.ViewNum,
"expire_at": shareInfo.ExpireAt,

View File

@@ -33,7 +33,9 @@ const (
func GetRedisShareInfo(shareId string) (*RedisShareInfo, error) {
rdb, ctx := utils.GetRedisClient()
shareInfoUnmarshalData, err := rdb.Get(ctx, fmt.Sprintf("015:shareInfoMap:%s", shareId)).Result()
shareInfo := rdb.Get(ctx, fmt.Sprintf("015:shareInfoMap:%s", shareId))
shareInfoUnmarshalData, err := shareInfo.Result()
ttl, _ := rdb.TTL(ctx, fmt.Sprintf("015:shareInfoMap:%s", shareId)).Result()
if err == redis.Nil {
return nil, nil
}
@@ -41,13 +43,15 @@ func GetRedisShareInfo(shareId string) (*RedisShareInfo, error) {
return nil, err
}
var shareInfoData RedisShareInfo
if err := json.Unmarshal([]byte(shareInfoUnmarshalData), &shareInfoData); err != nil {
return nil, err
}
shareInfoData.ExpireAt = time.Now().Add(ttl).Unix()
return &shareInfoData, nil
}
func SetRedisShareInfo(shareId string, shareInfo RedisShareInfo, ttl time.Duration) error {
func SetRedisShareInfo(shareId string, shareInfo RedisShareInfo) error {
rdb, ctx := utils.GetRedisClient()
old_shareInfo, err := GetRedisShareInfo(shareId)
if err != nil {
@@ -57,6 +61,7 @@ func SetRedisShareInfo(shareId string, shareInfo RedisShareInfo, ttl time.Durati
mergo.Merge(&shareInfo, old_shareInfo)
}
jsonData, _ := json.Marshal(shareInfo)
_, err = rdb.Set(ctx, fmt.Sprintf("015:shareInfoMap:%s", shareId), string(jsonData), ttl).Result()
_, err = rdb.Set(ctx, fmt.Sprintf("015:shareInfoMap:%s", shareId), string(jsonData), time.Duration(shareInfo.ExpireAt)).Result()
return err
}