2017-01-20 18:18:43 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-12-17 23:30:23 +00:00
|
|
|
"fmt"
|
2017-01-20 18:18:43 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
)
|
|
|
|
|
2018-12-06 02:46:14 +00:00
|
|
|
var DashboardDir string
|
2017-11-22 01:00:40 +00:00
|
|
|
|
2017-01-20 18:18:43 +00:00
|
|
|
func init() {
|
2017-12-17 23:30:23 +00:00
|
|
|
router.GET("/api/public/:sid", apiHandler(getPublic))
|
|
|
|
router.DELETE("/api/public/:sid", apiHandler(deletePublic))
|
|
|
|
router.PUT("/api/public/:sid", apiHandler(savePublic))
|
2017-01-20 18:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type FICPublicScene struct {
|
2018-03-09 18:07:08 +00:00
|
|
|
Type string `json:"type"`
|
|
|
|
Params map[string]interface{} `json:"params"`
|
2017-01-20 18:18:43 +00:00
|
|
|
}
|
|
|
|
|
2019-01-19 07:01:29 +00:00
|
|
|
type FICPublicDisplay struct {
|
|
|
|
Scenes []FICPublicScene `json:"scenes"`
|
|
|
|
Side []FICPublicScene `json:"side"`
|
|
|
|
CustomCountdown map[string]interface{} `json:"customCountdown"`
|
|
|
|
HideEvents bool `json:"hideEvents"`
|
|
|
|
HideCountdown bool `json:"hideCountdown"`
|
|
|
|
HideCarousel bool `json:"hideCarousel"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func readPublic(path string) (FICPublicDisplay, error) {
|
|
|
|
var s FICPublicDisplay
|
2017-01-20 18:18:43 +00:00
|
|
|
if fd, err := os.Open(path); err != nil {
|
|
|
|
return s, err
|
|
|
|
} else {
|
|
|
|
defer fd.Close()
|
|
|
|
jdec := json.NewDecoder(fd)
|
|
|
|
|
|
|
|
if err := jdec.Decode(&s); err != nil {
|
|
|
|
return s, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 07:01:29 +00:00
|
|
|
func savePublicTo(path string, s FICPublicDisplay) error {
|
2017-01-20 18:18:43 +00:00
|
|
|
if fd, err := os.Create(path); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
defer fd.Close()
|
|
|
|
jenc := json.NewEncoder(fd)
|
|
|
|
|
|
|
|
if err := jenc.Encode(s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 23:30:23 +00:00
|
|
|
func getPublic(ps httprouter.Params, body []byte) (interface{}, error) {
|
2018-12-06 02:46:14 +00:00
|
|
|
if _, err := os.Stat(path.Join(DashboardDir, fmt.Sprintf("public%s.json", ps.ByName("sid")))); !os.IsNotExist(err) {
|
|
|
|
return readPublic(path.Join(DashboardDir, fmt.Sprintf("public%s.json", ps.ByName("sid"))))
|
2017-01-20 18:18:43 +00:00
|
|
|
} else {
|
2019-01-19 07:01:29 +00:00
|
|
|
return FICPublicDisplay{Scenes: []FICPublicScene{}, Side: []FICPublicScene{}}, nil
|
2017-01-20 18:18:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 23:30:23 +00:00
|
|
|
func deletePublic(ps httprouter.Params, body []byte) (interface{}, error) {
|
2019-01-19 07:01:29 +00:00
|
|
|
if err := savePublicTo(path.Join(DashboardDir, fmt.Sprintf("public%s.json", ps.ByName("sid"))), FICPublicDisplay{}); err != nil {
|
2017-01-20 18:18:43 +00:00
|
|
|
return nil, err
|
|
|
|
} else {
|
2019-01-19 07:01:29 +00:00
|
|
|
return FICPublicDisplay{Scenes: []FICPublicScene{}, Side: []FICPublicScene{}}, nil
|
2017-01-20 18:18:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-17 23:30:23 +00:00
|
|
|
func savePublic(ps httprouter.Params, body []byte) (interface{}, error) {
|
2019-01-19 07:01:29 +00:00
|
|
|
var scenes FICPublicDisplay
|
2017-01-20 18:18:43 +00:00
|
|
|
if err := json.Unmarshal(body, &scenes); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-06 02:46:14 +00:00
|
|
|
if err := savePublicTo(path.Join(DashboardDir, fmt.Sprintf("public%s.json", ps.ByName("sid"))), scenes); err != nil {
|
2017-01-20 18:18:43 +00:00
|
|
|
return nil, err
|
|
|
|
} else {
|
|
|
|
return scenes, err
|
|
|
|
}
|
|
|
|
}
|