2022-10-02 21:24:53 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2022-10-04 10:29:50 +00:00
|
|
|
"fmt"
|
2022-10-02 21:24:53 +00:00
|
|
|
"net/http"
|
2022-10-15 10:20:18 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
2022-10-02 21:24:53 +00:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
"git.nemunai.re/nemunaire/reveil/config"
|
2022-10-04 10:29:50 +00:00
|
|
|
"git.nemunai.re/nemunaire/reveil/model"
|
2022-10-02 21:24:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func declareTracksRoutes(cfg *config.Config, router *gin.RouterGroup) {
|
|
|
|
router.GET("/tracks", func(c *gin.Context) {
|
2022-10-04 10:29:50 +00:00
|
|
|
tracks, err := reveil.LoadTracks(cfg)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
2022-10-02 21:24:53 +00:00
|
|
|
|
2022-10-04 10:29:50 +00:00
|
|
|
c.JSON(http.StatusOK, tracks)
|
2022-10-02 21:24:53 +00:00
|
|
|
})
|
|
|
|
router.POST("/tracks", func(c *gin.Context) {
|
2022-10-15 10:20:18 +00:00
|
|
|
ftrack, err := c.FormFile("trackfile")
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "No track found"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check file extension
|
|
|
|
if path.Ext(ftrack.Filename) != ".mp3" && path.Ext(ftrack.Filename) != ".flac" {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Bad file type. You should only upload .mp3 or .flac files."})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(ftrack.Filename, "/") {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": "Bad file name."})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
dst := path.Join(cfg.TracksDir, ftrack.Filename)
|
|
|
|
err = c.SaveUploadedFile(ftrack, dst)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Something goes wrong when saving the track: %s", err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
d, err := os.Stat(dst)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Something goes wrong when saving the track: %s", err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
track, err := reveil.LoadTrack(dst, d)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to load track: %s", err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, track)
|
2022-10-02 21:24:53 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
tracksRoutes := router.Group("/tracks/:tid")
|
2022-10-04 10:29:50 +00:00
|
|
|
tracksRoutes.Use(func(c *gin.Context) {
|
|
|
|
tracks, err := reveil.LoadTracks(cfg)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range tracks {
|
2022-10-04 13:47:06 +00:00
|
|
|
if t.Id.ToString() == c.Param("tid") {
|
2022-10-04 10:29:50 +00:00
|
|
|
c.Set("track", t)
|
|
|
|
c.Next()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"errmsg": "Track not found"})
|
|
|
|
})
|
2022-10-02 21:24:53 +00:00
|
|
|
|
|
|
|
tracksRoutes.GET("", func(c *gin.Context) {
|
|
|
|
c.JSON(http.StatusOK, c.MustGet("track"))
|
|
|
|
})
|
2022-10-15 11:29:25 +00:00
|
|
|
tracksRoutes.GET("/stream", func(c *gin.Context) {
|
|
|
|
track := c.MustGet("track").(*reveil.Track)
|
|
|
|
|
|
|
|
size, err := track.Size()
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to open the track: %s", err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fd, err := track.Open()
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to open the track: %s", err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer fd.Close()
|
|
|
|
|
|
|
|
c.DataFromReader(http.StatusOK, size, track.ContentType(), fd, map[string]string{})
|
|
|
|
})
|
2022-10-02 21:24:53 +00:00
|
|
|
tracksRoutes.PUT("", func(c *gin.Context) {
|
2022-10-04 10:29:50 +00:00
|
|
|
oldtrack := c.MustGet("track").(*reveil.Track)
|
|
|
|
|
|
|
|
var track reveil.Track
|
|
|
|
if err := c.ShouldBindJSON(&track); err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"errmsg": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if track.Name != oldtrack.Name {
|
|
|
|
err := oldtrack.Rename(track.Name)
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to rename the track: %s", err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if track.Enabled != oldtrack.Enabled {
|
|
|
|
var err error
|
|
|
|
if track.Enabled {
|
|
|
|
err = oldtrack.Enable(cfg)
|
|
|
|
} else {
|
|
|
|
err = oldtrack.Disable()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to enable/disable the track: %s", err.Error())})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, oldtrack)
|
2022-10-02 21:24:53 +00:00
|
|
|
})
|
|
|
|
tracksRoutes.DELETE("", func(c *gin.Context) {
|
2022-10-04 10:29:50 +00:00
|
|
|
track := c.MustGet("track").(*reveil.Track)
|
2022-10-02 21:24:53 +00:00
|
|
|
|
2022-10-04 10:29:50 +00:00
|
|
|
err := track.Remove()
|
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to remove the track: %s", err.Error())})
|
|
|
|
return
|
|
|
|
}
|
2022-10-02 21:24:53 +00:00
|
|
|
|
2022-10-04 10:29:50 +00:00
|
|
|
c.JSON(http.StatusOK, nil)
|
|
|
|
})
|
2022-10-02 21:24:53 +00:00
|
|
|
}
|