1
0
mirror of https://github.com/mxpv/podsync.git synced 2024-05-11 05:55:04 +00:00
mxpv-podsync/pkg/id/hashids.go

54 lines
1.0 KiB
Go
Raw Normal View History

2017-07-22 21:22:00 -07:00
package id
import (
2017-08-05 15:02:26 -07:00
"hash/fnv"
2017-08-19 16:58:23 -07:00
"github.com/mxpv/podsync/pkg/api"
2017-07-22 21:22:00 -07:00
hd "github.com/speps/go-hashids"
)
const (
minLength = 4
salt = "mVJIX8cDWQJ71oMw6xw9yYV9TA1rojDcKrhUaOqEfaE"
alphabet = "abcdefghijklmnopqrstuvwxyz1234567890"
)
type hashId struct {
hid *hd.HashID
}
2017-08-05 15:02:26 -07:00
func hashString(s string) int {
h := fnv.New32a()
h.Write([]byte(s))
return int(h.Sum32())
}
2017-08-13 14:50:59 -07:00
func (h *hashId) Generate(feed *api.Feed) (string, error) {
2017-08-05 15:02:26 -07:00
// Don't create duplicate urls for same playlist/settings
// https://github.com/podsync/issues/issues/6
numbers := []int{
hashString(feed.UserId),
2017-08-13 14:50:59 -07:00
hashString(string(feed.Provider)),
hashString(string(feed.LinkType)),
hashString(feed.ItemId),
2017-08-05 15:02:26 -07:00
feed.PageSize,
hashString(string(feed.Quality)),
hashString(string(feed.Format)),
2017-08-13 14:50:59 -07:00
feed.FeatureLevel,
2017-08-05 15:02:26 -07:00
}
return h.hid.Encode(numbers)
2017-07-22 21:22:00 -07:00
}
func NewIdGenerator() (*hashId, error) {
data := hd.NewData()
data.MinLength = minLength
data.Salt = salt
data.Alphabet = alphabet
2017-08-13 19:28:43 -07:00
hid, err := hd.NewWithData(data)
if err != nil {
return nil, err
}
2017-07-22 21:22:00 -07:00
return &hashId{hid}, nil
}