-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcrossonic.go
More file actions
88 lines (73 loc) · 1.8 KB
/
crossonic.go
File metadata and controls
88 lines (73 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package crossonic
import (
"embed"
"fmt"
"io/fs"
"log"
"regexp"
"slices"
"strings"
"github.com/jaevor/go-nanoid"
)
var (
ServerName = "crossonic-server"
Version = "dev"
ProtocolVersion = "0.4.0"
)
//go:embed all:repos/migrations
var migrationsFS embed.FS
var MigrationsFS fs.FS
var GenID func() string
var IDAlphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-~"
var IDRegex = regexp.MustCompile(fmt.Sprintf("^(tr)|(al)|(ar)|(pl)|(irs)_[%s]{12}$", strings.ReplaceAll(IDAlphabet, "-", "\\-")))
func init() {
var err error
MigrationsFS, err = fs.Sub(migrationsFS, "repos/migrations")
if err != nil {
log.Fatal(err)
}
GenID, err = nanoid.CustomUnicode(IDAlphabet, 12)
if err != nil {
panic(err)
}
}
type IDType string
const (
IDTypeSong IDType = "tr"
IDTypeAlbum IDType = "al"
IDTypeArtist IDType = "ar"
IDTypePlaylist IDType = "pl"
IDTypeInternetRadioStation IDType = "irs"
)
func GenIDSong() string {
return string(IDTypeSong) + "_" + GenID()
}
func GenIDAlbum() string {
return string(IDTypeAlbum) + "_" + GenID()
}
func GenIDArtist() string {
return string(IDTypeArtist) + "_" + GenID()
}
func GenIDPlaylist() string {
return string(IDTypePlaylist) + "_" + GenID()
}
func GenIDInternetRadioStation() string {
return string(IDTypeInternetRadioStation) + "_" + GenID()
}
func GetIDType(id string) (IDType, bool) {
parts := strings.Split(id, "_")
if len(parts) != 2 {
return "", false
}
types := []IDType{
IDTypeSong, IDTypeAlbum, IDTypeArtist, IDTypePlaylist, IDTypeInternetRadioStation,
}
if !slices.Contains(types, IDType(parts[0])) {
return "", false
}
return IDType(parts[0]), true
}
func IsIDType(id string, idType IDType) bool {
typ, ok := GetIDType(id)
return ok && idType == typ
}