Can listen to gong on the page

This commit is contained in:
nemunaire 2022-10-15 13:47:57 +02:00
parent 8e432c9b6b
commit 7db7489b4c
3 changed files with 52 additions and 0 deletions

View File

@ -85,6 +85,24 @@ func declareGongsRoutes(cfg *config.Config, router *gin.RouterGroup) {
gongsRoutes.GET("", func(c *gin.Context) { gongsRoutes.GET("", func(c *gin.Context) {
c.JSON(http.StatusOK, c.MustGet("gong")) 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) { gongsRoutes.PUT("", func(c *gin.Context) {
oldgong := c.MustGet("gong").(*reveil.Gong) oldgong := c.MustGet("gong").(*reveil.Gong)

View File

@ -66,6 +66,33 @@ func LoadGongs(cfg *config.Config) (gongs []*Gong, err error) {
return 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 { func (g *Gong) Rename(newName string) error {
newPath := filepath.Join(filepath.Dir(g.Path), newName+filepath.Ext(g.Path)) newPath := filepath.Join(filepath.Dir(g.Path), newName+filepath.Ext(g.Path))

View File

@ -49,6 +49,13 @@
</ListGroupItem> </ListGroupItem>
</ListGroup> </ListGroup>
<div class="my-2 d-flex justify-content-center">
<audio controls class="w-100 rounded">
<source src="api/gongs/{gong.id}/stream">
Your browser does not support the audio element.
</audio>
</div>
<ListGroup class="my-2 text-center"> <ListGroup class="my-2 text-center">
{#if !confirmDeletion} {#if !confirmDeletion}
<ListGroupItem <ListGroupItem