Feat: generate share URL

This commit is contained in:
HFO4
2020-01-26 13:07:05 +08:00
parent 4abd5b2346
commit 0ee0ac5e89
14 changed files with 204 additions and 19 deletions

33
pkg/hashid/hash.go Normal file
View 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
}