mirror of
https://github.com/mxpv/podsync.git
synced 2024-05-11 05:55:04 +00:00
Rework id generator
This commit is contained in:
@@ -17,7 +17,6 @@ const (
|
||||
Vimeo = Provider("vimeo")
|
||||
)
|
||||
|
||||
|
||||
type LinkType string
|
||||
|
||||
const (
|
||||
|
||||
@@ -2,19 +2,14 @@ package id
|
||||
|
||||
import (
|
||||
"hash/fnv"
|
||||
"time"
|
||||
|
||||
"github.com/mxpv/podsync/pkg/api"
|
||||
hd "github.com/speps/go-hashids"
|
||||
)
|
||||
|
||||
const (
|
||||
minLength = 4
|
||||
salt = "mVJIX8cDWQJ71oMw6xw9yYV9TA1rojDcKrhUaOqEfaE"
|
||||
alphabet = "abcdefghijklmnopqrstuvwxyz1234567890"
|
||||
"github.com/ventu-io/go-shortid"
|
||||
)
|
||||
|
||||
type hashId struct {
|
||||
hid *hd.HashID
|
||||
sid *shortid.Shortid
|
||||
}
|
||||
|
||||
func hashString(s string) int {
|
||||
@@ -24,30 +19,14 @@ func hashString(s string) int {
|
||||
}
|
||||
|
||||
func (h *hashId) Generate(feed *api.Feed) (string, error) {
|
||||
// Don't create duplicate urls for same playlist/settings
|
||||
// https://github.com/podsync/issues/issues/6
|
||||
numbers := []int{
|
||||
hashString(feed.UserId),
|
||||
hashString(string(feed.Provider)),
|
||||
hashString(string(feed.LinkType)),
|
||||
hashString(feed.ItemId),
|
||||
feed.PageSize,
|
||||
hashString(string(feed.Quality)),
|
||||
hashString(string(feed.Format)),
|
||||
feed.FeatureLevel,
|
||||
}
|
||||
|
||||
return h.hid.Encode(numbers)
|
||||
return h.sid.Generate()
|
||||
}
|
||||
|
||||
func NewIdGenerator() (*hashId, error) {
|
||||
data := hd.NewData()
|
||||
data.MinLength = minLength
|
||||
data.Salt = salt
|
||||
data.Alphabet = alphabet
|
||||
hid, err := hd.NewWithData(data)
|
||||
sid, err := shortid.New(1, shortid.DefaultABC, uint64(time.Now().UnixNano()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &hashId{hid}, nil
|
||||
|
||||
return &hashId{sid}, nil
|
||||
}
|
||||
|
||||
@@ -11,27 +11,9 @@ func TestEncode(t *testing.T) {
|
||||
hid, err := NewIdGenerator()
|
||||
require.NoError(t, err)
|
||||
|
||||
feed := &api.Feed{
|
||||
UserId: "1",
|
||||
Provider: api.Youtube,
|
||||
LinkType: api.Channel,
|
||||
ItemId: "UC2yTVSttx7lxAOAzx1opjoA",
|
||||
PageSize: 10,
|
||||
Quality: api.HighQuality,
|
||||
Format: api.AudioFormat,
|
||||
}
|
||||
feed := &api.Feed{}
|
||||
|
||||
hash1, err := hid.Generate(feed)
|
||||
hash, err := hid.Generate(feed)
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, hash1)
|
||||
|
||||
// Ensure we have same hash for same feed/parameters
|
||||
hash2, err := hid.Generate(feed)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, hash1, hash2)
|
||||
|
||||
feed.UserId = ""
|
||||
hash3, err := hid.Generate(feed)
|
||||
require.NoError(t, err)
|
||||
require.NotEqual(t, hash1, hash3)
|
||||
require.NotEmpty(t, hash)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user