Use custom identifiers
This commit is contained in:
parent
b2d50972ed
commit
299f3ba0cf
@ -1,7 +1,6 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@ -33,9 +32,9 @@ func declareGongsRoutes(cfg *config.Config, router *gin.RouterGroup) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, t := range gongs {
|
for _, g := range gongs {
|
||||||
if base64.StdEncoding.EncodeToString(t.Id) == c.Param("tid") {
|
if g.Id.ToString() == c.Param("tid") {
|
||||||
c.Set("gong", t)
|
c.Set("gong", g)
|
||||||
c.Next()
|
c.Next()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@ -34,7 +33,7 @@ func declareTracksRoutes(cfg *config.Config, router *gin.RouterGroup) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, t := range tracks {
|
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.Set("track", t)
|
||||||
c.Next()
|
c.Next()
|
||||||
return
|
return
|
||||||
|
@ -13,10 +13,10 @@ import (
|
|||||||
const CURRENT_GONG = "_current_gong"
|
const CURRENT_GONG = "_current_gong"
|
||||||
|
|
||||||
type Gong struct {
|
type Gong struct {
|
||||||
Id []byte `json:"id"`
|
Id Identifier `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
Enabled bool `json:"enabled"`
|
Enabled bool `json:"enabled"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func currentGongPath(cfg *config.Config) string {
|
func currentGongPath(cfg *config.Config) string {
|
||||||
@ -36,7 +36,7 @@ func LoadGongs(cfg *config.Config) (gongs []*Gong, err error) {
|
|||||||
hash := sha512.Sum512([]byte(path))
|
hash := sha512.Sum512([]byte(path))
|
||||||
pabs, _ := filepath.Abs(path)
|
pabs, _ := filepath.Abs(path)
|
||||||
gongs = append(gongs, &Gong{
|
gongs = append(gongs, &Gong{
|
||||||
Id: hash[:63],
|
Id: hash[:],
|
||||||
Name: strings.TrimSuffix(d.Name(), filepath.Ext(d.Name())),
|
Name: strings.TrimSuffix(d.Name(), filepath.Ext(d.Name())),
|
||||||
Path: path,
|
Path: path,
|
||||||
Enabled: current_gong == pabs,
|
Enabled: current_gong == pabs,
|
||||||
|
31
model/identifier.go
Normal file
31
model/identifier.go
Normal 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
|
||||||
|
}
|
@ -12,10 +12,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Track struct {
|
type Track struct {
|
||||||
Id []byte `json:"id"`
|
Id Identifier `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Path string `json:"path"`
|
Path string `json:"path"`
|
||||||
Enabled bool `json:"enabled"`
|
Enabled bool `json:"enabled"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadTracks(cfg *config.Config) (tracks []*Track, err error) {
|
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() {
|
if d.Mode().IsRegular() {
|
||||||
hash := sha512.Sum512([]byte(path))
|
hash := sha512.Sum512([]byte(path))
|
||||||
tracks = append(tracks, &Track{
|
tracks = append(tracks, &Track{
|
||||||
Id: hash[:63],
|
Id: hash[:],
|
||||||
Name: strings.TrimSuffix(d.Name(), filepath.Ext(d.Name())),
|
Name: strings.TrimSuffix(d.Name(), filepath.Ext(d.Name())),
|
||||||
Path: path,
|
Path: path,
|
||||||
Enabled: len(strings.Split(path, "/")) == 2,
|
Enabled: len(strings.Split(path, "/")) == 2,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user