Use custom identifiers

This commit is contained in:
nemunaire 2022-10-04 15:47:06 +02:00
parent b2d50972ed
commit 299f3ba0cf
5 changed files with 45 additions and 16 deletions

View File

@ -1,7 +1,6 @@
package api
import (
"encoding/base64"
"fmt"
"net/http"
@ -33,9 +32,9 @@ func declareGongsRoutes(cfg *config.Config, router *gin.RouterGroup) {
return
}
for _, t := range gongs {
if base64.StdEncoding.EncodeToString(t.Id) == c.Param("tid") {
c.Set("gong", t)
for _, g := range gongs {
if g.Id.ToString() == c.Param("tid") {
c.Set("gong", g)
c.Next()
return
}

View File

@ -1,7 +1,6 @@
package api
import (
"encoding/base64"
"fmt"
"net/http"
@ -34,7 +33,7 @@ func declareTracksRoutes(cfg *config.Config, router *gin.RouterGroup) {
}
for _, t := range tracks {
if base64.StdEncoding.EncodeToString(t.Id) == c.Param("tid") {
if t.Id.ToString() == c.Param("tid") {
c.Set("track", t)
c.Next()
return

View File

@ -13,10 +13,10 @@ import (
const CURRENT_GONG = "_current_gong"
type Gong struct {
Id []byte `json:"id"`
Name string `json:"name"`
Path string `json:"path"`
Enabled bool `json:"enabled"`
Id Identifier `json:"id"`
Name string `json:"name"`
Path string `json:"path"`
Enabled bool `json:"enabled"`
}
func currentGongPath(cfg *config.Config) string {
@ -36,7 +36,7 @@ func LoadGongs(cfg *config.Config) (gongs []*Gong, err error) {
hash := sha512.Sum512([]byte(path))
pabs, _ := filepath.Abs(path)
gongs = append(gongs, &Gong{
Id: hash[:63],
Id: hash[:],
Name: strings.TrimSuffix(d.Name(), filepath.Ext(d.Name())),
Path: path,
Enabled: current_gong == pabs,

31
model/identifier.go Normal file
View File

@ -0,0 +1,31 @@
package reveil
import (
"encoding/base64"
"errors"
)
type Identifier []byte
func (i *Identifier) ToString() string {
return base64.RawURLEncoding.EncodeToString(*i)
}
func (i *Identifier) MarshalJSON() (dst []byte, err error) {
dst = make([]byte, base64.RawURLEncoding.EncodedLen(len(*i)))
base64.RawURLEncoding.Encode(dst, *i)
dst = append([]byte{'"'}, dst...)
dst = append(dst, '"')
return
}
func (i *Identifier) UnmarshalJSON(src []byte) error {
if len(src) < 2 || src[0] != '"' || src[len(src)-1] != '"' {
return errors.New("Unvalid character found to encapsulate the JSON value")
}
*i = make([]byte, base64.RawURLEncoding.DecodedLen(len(src)-2))
_, err := base64.RawURLEncoding.Decode(*i, src[1:len(src)-1])
return err
}

View File

@ -12,10 +12,10 @@ import (
)
type Track struct {
Id []byte `json:"id"`
Name string `json:"name"`
Path string `json:"path"`
Enabled bool `json:"enabled"`
Id Identifier `json:"id"`
Name string `json:"name"`
Path string `json:"path"`
Enabled bool `json:"enabled"`
}
func LoadTracks(cfg *config.Config) (tracks []*Track, err error) {
@ -23,7 +23,7 @@ func LoadTracks(cfg *config.Config) (tracks []*Track, err error) {
if d.Mode().IsRegular() {
hash := sha512.Sum512([]byte(path))
tracks = append(tracks, &Track{
Id: hash[:63],
Id: hash[:],
Name: strings.TrimSuffix(d.Name(), filepath.Ext(d.Name())),
Path: path,
Enabled: len(strings.Split(path, "/")) == 2,