diff --git a/api/gongs.go b/api/gongs.go index 7410243..b160292 100644 --- a/api/gongs.go +++ b/api/gongs.go @@ -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 } diff --git a/api/tracks.go b/api/tracks.go index 9a2bc6a..f47af77 100644 --- a/api/tracks.go +++ b/api/tracks.go @@ -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 diff --git a/model/gong.go b/model/gong.go index 0e3c81a..6683ce0 100644 --- a/model/gong.go +++ b/model/gong.go @@ -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, diff --git a/model/identifier.go b/model/identifier.go new file mode 100644 index 0000000..d2f8f90 --- /dev/null +++ b/model/identifier.go @@ -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 +} diff --git a/model/track.go b/model/track.go index 8c6e019..5aadc86 100644 --- a/model/track.go +++ b/model/track.go @@ -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,