mirror of
https://github.com/halejohn/Cloudreve.git
synced 2026-01-26 09:34:57 +08:00
Feat: generate share URL
This commit is contained in:
33
pkg/hashid/hash.go
Normal file
33
pkg/hashid/hash.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package hashid
|
||||
|
||||
import "github.com/HFO4/cloudreve/pkg/conf"
|
||||
import "github.com/speps/go-hashids"
|
||||
|
||||
// ID类型
|
||||
const (
|
||||
ShareID = iota // 分享
|
||||
UserID // 用户
|
||||
)
|
||||
|
||||
// HashEncode 对给定数据计算HashID
|
||||
func HashEncode(v []int) (string, error) {
|
||||
hd := hashids.NewData()
|
||||
hd.Salt = conf.SystemConfig.HashIDSalt
|
||||
|
||||
h, err := hashids.NewWithData(hd)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
id, err := h.Encode(v)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// HashID 计算数据库内主键对应的HashID
|
||||
func HashID(id uint, t int) string {
|
||||
v, _ := HashEncode([]int{int(id), t})
|
||||
return v
|
||||
}
|
||||
Reference in New Issue
Block a user