54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
package oss
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"lone-services/pkg/redis"
|
|
"lone-services/pkg/utils"
|
|
pkgws "lone-services/pkg/ws"
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
)
|
|
|
|
type UploadSession struct {
|
|
UserId int64 `json:"user_id"`
|
|
BizId int64 `json:"biz_id"`
|
|
ObjId int64 `json:"obj_id"` // 兼容旧缓存
|
|
DateDir string `json:"date_dir"`
|
|
}
|
|
|
|
func (s UploadSession) SkinUserId() int64 {
|
|
if s.BizId > utils.NumberZero {
|
|
return s.BizId
|
|
}
|
|
return s.ObjId
|
|
}
|
|
|
|
func SaveUploadSession(ctx context.Context, taskId string, sess UploadSession, expireSeconds int) error {
|
|
if expireSeconds <= 0 {
|
|
expireSeconds = 3600
|
|
}
|
|
raw, err := jsoniter.Marshal(sess)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return redis.Client.Set(ctx, pkgws.RedisKeyOssUpload+taskId, raw, time.Duration(expireSeconds)*time.Second).Err()
|
|
}
|
|
|
|
func LoadUploadSession(ctx context.Context, taskId string) (*UploadSession, error) {
|
|
val, err := redis.Client.Get(ctx, pkgws.RedisKeyOssUpload+taskId).Result()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var sess UploadSession
|
|
if err = jsoniter.UnmarshalFromString(val, &sess); err != nil {
|
|
return nil, err
|
|
}
|
|
if sess.UserId < utils.NumberOne {
|
|
return nil, fmt.Errorf("upload session user empty")
|
|
}
|
|
return &sess, nil
|
|
}
|