Files
cloudreve/pkg/hashid/hash.go

153 lines
3.3 KiB
Go
Raw Normal View History

2020-01-26 13:07:05 +08:00
package hashid
2020-01-26 14:57:07 +08:00
import (
"context"
2020-01-26 14:57:07 +08:00
"errors"
)
import "github.com/speps/go-hashids"
2020-01-26 13:07:05 +08:00
// ID类型
const (
ShareID = iota // 分享
UserID // 用户
FileID // 文件ID
FolderID // 目录ID
2020-02-12 11:38:04 +08:00
TagID // 标签ID
2020-02-18 13:45:59 +08:00
PolicyID // 存储策略ID
SourceLinkID
GroupID
EntityID
AuditLogID
NodeID
TaskID
DavAccountID
PaymentID
2020-01-26 13:07:05 +08:00
)
2020-01-26 14:57:07 +08:00
var (
2020-02-18 13:45:59 +08:00
// ErrTypeNotMatch ID类型不匹配
2022-10-08 18:51:52 +08:00
ErrTypeNotMatch = errors.New("mismatched ID type.")
2020-01-26 14:57:07 +08:00
)
type Encoder interface {
Encode(v []int) (string, error)
Decode(raw string, t int) (int, error)
}
// ObjectIDCtx define key for decoded hash ID.
type (
ObjectIDCtx struct{}
EncodeFunc func(encoder Encoder, uid int) string
)
2020-01-26 13:07:05 +08:00
type hashEncoder struct {
h *hashids.HashID
}
func New(salt string) (Encoder, error) {
hd := hashids.NewData()
hd.Salt = salt
2020-01-26 13:07:05 +08:00
h, err := hashids.NewWithData(hd)
if err != nil {
return nil, err
2020-01-26 13:07:05 +08:00
}
return &hashEncoder{h: h}, nil
}
func (e *hashEncoder) Encode(v []int) (string, error) {
id, err := e.h.Encode(v)
2020-01-26 13:07:05 +08:00
if err != nil {
return "", err
}
return id, nil
}
func (e *hashEncoder) Decode(raw string, t int) (int, error) {
res, err := e.h.DecodeWithError(raw)
2020-01-26 14:57:07 +08:00
if err != nil {
return 0, err
2020-01-26 14:57:07 +08:00
}
if len(res) != 2 || res[1] != t {
return 0, ErrTypeNotMatch
}
return res[0], nil
}
2020-01-26 14:57:07 +08:00
// EncodeUserID encode user id to hash id
func EncodeUserID(encoder Encoder, uid int) string {
res, _ := encoder.Encode([]int{uid, UserID})
return res
2020-01-26 14:57:07 +08:00
}
// EncodeGroupID encode group id to hash id
func EncodeGroupID(encoder Encoder, uid int) string {
res, _ := encoder.Encode([]int{uid, GroupID})
return res
2020-01-26 13:07:05 +08:00
}
2020-01-26 14:57:07 +08:00
// EncodePaymentID encode payment id to hash id
func EncodePaymentID(encoder Encoder, uid int) string {
res, _ := encoder.Encode([]int{uid, PaymentID})
return res
}
// EncodeFileID encode file id to hash id
func EncodeFileID(encoder Encoder, uid int) string {
res, _ := encoder.Encode([]int{uid, FileID})
return res
}
// EncodeAuditLogID encode audit log id to hash id
func EncodeAuditLogID(encoder Encoder, uid int) string {
res, _ := encoder.Encode([]int{uid, AuditLogID})
return res
}
// EncodeTaskID encode task id to hash id
func EncodeTaskID(encoder Encoder, uid int) string {
res, _ := encoder.Encode([]int{uid, TaskID})
return res
}
// EncodeEntityID encode entity id to hash id
func EncodeEntityID(encoder Encoder, id int) string {
res, _ := encoder.Encode([]int{id, EntityID})
return res
}
// EncodeNodeID encode node id to hash id
func EncodeNodeID(encoder Encoder, id int) string {
res, _ := encoder.Encode([]int{id, NodeID})
return res
}
// EncodeEntityID encode policy id to hash id
func EncodePolicyID(encoder Encoder, id int) string {
res, _ := encoder.Encode([]int{id, PolicyID})
return res
}
// EncodeEntityID encode share id to hash id
func EncodeShareID(encoder Encoder, id int) string {
res, _ := encoder.Encode([]int{id, ShareID})
return res
}
// EncodeDavAccountID encode dav account id to hash id
func EncodeDavAccountID(encoder Encoder, id int) string {
res, _ := encoder.Encode([]int{id, DavAccountID})
return res
}
// EncodeSourceLinkID encode source link id to hash id
func EncodeSourceLinkID(encoder Encoder, id int) string {
res, _ := encoder.Encode([]int{id, SourceLinkID})
return res
}
func FromContext(c context.Context) int {
return c.Value(ObjectIDCtx{}).(int)
2020-01-26 14:57:07 +08:00
}