diff --git a/api/gongs.go b/api/gongs.go index 23cc900..93aaecd 100644 --- a/api/gongs.go +++ b/api/gongs.go @@ -85,6 +85,24 @@ func declareGongsRoutes(cfg *config.Config, router *gin.RouterGroup) { gongsRoutes.GET("", func(c *gin.Context) { c.JSON(http.StatusOK, c.MustGet("gong")) }) + gongsRoutes.GET("/stream", func(c *gin.Context) { + gong := c.MustGet("gong").(*reveil.Gong) + + size, err := gong.Size() + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to open the gong: %s", err.Error())}) + return + } + + fd, err := gong.Open() + if err != nil { + c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"errmsg": fmt.Sprintf("Unable to open the gong: %s", err.Error())}) + return + } + defer fd.Close() + + c.DataFromReader(http.StatusOK, size, gong.ContentType(), fd, map[string]string{}) + }) gongsRoutes.PUT("", func(c *gin.Context) { oldgong := c.MustGet("gong").(*reveil.Gong) diff --git a/model/gong.go b/model/gong.go index 5f23f45..9b7a2f2 100644 --- a/model/gong.go +++ b/model/gong.go @@ -66,6 +66,33 @@ func LoadGongs(cfg *config.Config) (gongs []*Gong, err error) { return } +func (g *Gong) Open() (*os.File, error) { + return os.Open(g.Path) +} + +func (g *Gong) Size() (int64, error) { + if st, err := os.Stat(g.Path); err != nil { + return 0, err + } else { + return st.Size(), err + } +} + +func (g *Gong) ContentType() string { + switch filepath.Ext(g.Path) { + case ".flac": + return "audio/flac" + case ".mp3": + return "audio/mpeg" + case ".ogg": + return "audio/ogg" + case ".wav": + return "audio/vnd.wav" + } + + return "application/octet-stream" +} + func (g *Gong) Rename(newName string) error { newPath := filepath.Join(filepath.Dir(g.Path), newName+filepath.Ext(g.Path)) diff --git a/ui/src/routes/musiks/gongs/[gid]/+page.svelte b/ui/src/routes/musiks/gongs/[gid]/+page.svelte index e0dc2f2..1a58f8c 100644 --- a/ui/src/routes/musiks/gongs/[gid]/+page.svelte +++ b/ui/src/routes/musiks/gongs/[gid]/+page.svelte @@ -49,6 +49,13 @@ +
+ +
+ {#if !confirmDeletion}