@@ -2,6 +2,7 @@ package player
22
33import (
44 "fmt"
5+ "go-api/infrastructure/helpers"
56 "go-api/infrastructure/models"
67 "go-api/infrastructure/reqRes"
78 "go-api/pages/fileExplorer"
@@ -50,7 +51,7 @@ func (controller *Controller) GetPlayer(w reqRes.MyResponseWriter, r *reqRes.MyR
5051 w .RenderTemplate (playerTemplate , pageData )
5152}
5253
53- func (controller * Controller ) AddSong (w reqRes.MyResponseWriter , r * reqRes.MyRequest ) {
54+ func (controller * Controller ) EnqueueSong (w reqRes.MyResponseWriter , r * reqRes.MyRequest ) {
5455 pathQueryParam := r .PathValue ("path" )
5556 pathToFile := filepath .Join (controller .explorerRoot , pathQueryParam )
5657
@@ -86,6 +87,39 @@ func (controller *Controller) AddSong(w reqRes.MyResponseWriter, r *reqRes.MyReq
8687 controller .GetPlayer (w , r )
8788}
8889
90+ func (controller * Controller ) EnqueueFolder (w reqRes.MyResponseWriter , r * reqRes.MyRequest ) {
91+ pathQueryParam := r .PathValue ("path" )
92+ folder := filepath .Join (controller .explorerRoot , pathQueryParam )
93+
94+ ok , dirEntries := helpers .ReadFolder (w , folder )
95+ if ! ok {
96+ return
97+ }
98+
99+ songsToAdd := make ([]models.QueuedSong , 0 )
100+ for _ , file := range dirEntries {
101+ fileName := file .Name ()
102+ isSong := fileExplorer .IsSong (fileName )
103+ if ! isSong {
104+ continue
105+ }
106+
107+ songsToAdd = append (songsToAdd , models.QueuedSong {
108+ Path : filepath .Join (pathQueryParam , fileName ),
109+ })
110+ }
111+
112+ result := controller .db .Create (& songsToAdd )
113+
114+ if result .Error != nil {
115+ message := fmt .Sprintf ("Failed to insert song into queue: \n %v" , result .Error )
116+ w .Error (message , http .StatusBadRequest )
117+ return
118+ }
119+
120+ controller .GetPlayer (w , r )
121+ }
122+
89123// ReportSongDuration should be called by client to tell the server when the song actually ends
90124// It would have been nice to be able to figure out duration server-side, but that seems to not be that easy
91125func (controller * Controller ) ReportSongDuration (w reqRes.MyResponseWriter , r * reqRes.MyRequest ) {
0 commit comments