2017-01-20 18:18:43 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-12-17 23:30:23 +00:00
|
|
|
"fmt"
|
2022-05-16 09:38:46 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2017-01-20 18:18:43 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2024-03-14 16:44:21 +00:00
|
|
|
"strconv"
|
2024-03-14 11:38:28 +00:00
|
|
|
"strings"
|
2024-03-14 16:44:21 +00:00
|
|
|
"time"
|
2017-01-20 18:18:43 +00:00
|
|
|
|
2022-05-16 09:38:46 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2017-01-20 18:18:43 +00:00
|
|
|
)
|
|
|
|
|
2018-12-06 02:46:14 +00:00
|
|
|
var DashboardDir string
|
2017-11-22 01:00:40 +00:00
|
|
|
|
2022-05-16 09:38:46 +00:00
|
|
|
func declarePublicRoutes(router *gin.RouterGroup) {
|
2024-03-14 16:44:21 +00:00
|
|
|
router.GET("/public/", listPublic)
|
2022-05-16 09:38:46 +00:00
|
|
|
router.GET("/public/:sid", getPublic)
|
|
|
|
router.DELETE("/public/:sid", deletePublic)
|
|
|
|
router.PUT("/public/:sid", 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"`
|
2024-03-14 16:44:21 +00:00
|
|
|
PropagationTime *time.Time `json:"propagationTime,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func InitDashboardPresets(dir string) error {
|
|
|
|
return nil
|
2019-01-19 07:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-14 16:44:21 +00:00
|
|
|
type DashboardFiles struct {
|
|
|
|
Presets []string `json:"presets"`
|
|
|
|
Nexts []*NextDashboardFile `json:"nexts"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type NextDashboardFile struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Screen int `json:"screen"`
|
|
|
|
Date time.Time `json:"date"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func listPublic(c *gin.Context) {
|
|
|
|
files, err := os.ReadDir(DashboardDir)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var ret DashboardFiles
|
|
|
|
for _, file := range files {
|
|
|
|
if strings.HasPrefix(file.Name(), "preset-") {
|
|
|
|
ret.Presets = append(ret.Presets, strings.TrimSuffix(strings.TrimPrefix(file.Name(), "preset-"), ".json"))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(file.Name(), "public") || len(file.Name()) < 18 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ts, err := strconv.ParseInt(file.Name()[8:18], 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
s, _ := strconv.Atoi(file.Name()[6:7])
|
|
|
|
ret.Nexts = append(ret.Nexts, &NextDashboardFile{
|
|
|
|
Name: file.Name()[6:18],
|
|
|
|
Screen: s,
|
|
|
|
Date: time.Unix(ts, 0),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, ret)
|
|
|
|
}
|
|
|
|
|
2022-05-16 09:38:46 +00:00
|
|
|
func getPublic(c *gin.Context) {
|
2024-03-14 11:38:28 +00:00
|
|
|
if strings.Contains(c.Params.ByName("sid"), "/") {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "sid cannot contains /"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-14 16:44:21 +00:00
|
|
|
filename := fmt.Sprintf("public%s.json", c.Params.ByName("sid"))
|
|
|
|
if strings.HasPrefix(c.Params.ByName("sid"), "preset-") {
|
|
|
|
filename = fmt.Sprintf("%s.json", c.Params.ByName("sid"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := os.Stat(path.Join(DashboardDir, filename)); !os.IsNotExist(err) {
|
|
|
|
p, err := readPublic(path.Join(DashboardDir, filename))
|
2022-05-16 09:38:46 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println("Unable to readPublic in getPublic:", err.Error())
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during scene retrieval."})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, p)
|
2022-06-08 14:48:21 +00:00
|
|
|
return
|
2017-01-20 18:18:43 +00:00
|
|
|
}
|
2022-05-16 09:38:46 +00:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, FICPublicDisplay{Scenes: []FICPublicScene{}, Side: []FICPublicScene{}})
|
2017-01-20 18:18:43 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 09:38:46 +00:00
|
|
|
func deletePublic(c *gin.Context) {
|
2024-03-14 11:38:28 +00:00
|
|
|
if strings.Contains(c.Params.ByName("sid"), "/") {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "sid cannot contains /"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-14 16:44:21 +00:00
|
|
|
filename := fmt.Sprintf("public%s.json", c.Params.ByName("sid"))
|
|
|
|
if strings.HasPrefix(c.Params.ByName("sid"), "preset-") {
|
|
|
|
filename = fmt.Sprintf("%s.json", c.Params.ByName("sid"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(filename) == 12 {
|
|
|
|
if err := savePublicTo(path.Join(DashboardDir, filename), FICPublicDisplay{}); err != nil {
|
|
|
|
log.Println("Unable to deletePublic:", err.Error())
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during scene deletion."})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := os.Remove(path.Join(DashboardDir, filename)); err != nil {
|
|
|
|
log.Println("Unable to deletePublic:", err.Error())
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during scene deletion."})
|
|
|
|
return
|
|
|
|
}
|
2017-01-20 18:18:43 +00:00
|
|
|
}
|
2022-05-16 09:38:46 +00:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, FICPublicDisplay{Scenes: []FICPublicScene{}, Side: []FICPublicScene{}})
|
2017-01-20 18:18:43 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 09:38:46 +00:00
|
|
|
func savePublic(c *gin.Context) {
|
2024-03-14 11:38:28 +00:00
|
|
|
if strings.Contains(c.Params.ByName("sid"), "/") {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "sid cannot contains /"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-19 07:01:29 +00:00
|
|
|
var scenes FICPublicDisplay
|
2022-05-16 09:38:46 +00:00
|
|
|
err := c.ShouldBindJSON(&scenes)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
2017-01-20 18:18:43 +00:00
|
|
|
}
|
|
|
|
|
2024-03-14 16:44:21 +00:00
|
|
|
filename := fmt.Sprintf("public%s.json", c.Params.ByName("sid"))
|
|
|
|
if c.Request.URL.Query().Has("t") {
|
|
|
|
t, err := time.Parse(time.RFC3339, c.Request.URL.Query().Get("t"))
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
filename = fmt.Sprintf("public%s-%d.json", c.Params.ByName("sid"), t.Unix())
|
|
|
|
} else if c.Request.URL.Query().Has("p") {
|
|
|
|
filename = fmt.Sprintf("preset-%s.json", c.Request.URL.Query().Get("p"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := savePublicTo(path.Join(DashboardDir, filename), scenes); err != nil {
|
2022-05-16 09:38:46 +00:00
|
|
|
log.Println("Unable to savePublicTo:", err.Error())
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": "An error occurs during scene saving."})
|
|
|
|
return
|
2017-01-20 18:18:43 +00:00
|
|
|
}
|
2022-05-16 09:38:46 +00:00
|
|
|
|
|
|
|
c.JSON(http.StatusOK, scenes)
|
2017-01-20 18:18:43 +00:00
|
|
|
}
|