1
0

And same for a Playlist.

This commit is contained in:
Alexander Andreev 2024-05-22 04:07:33 +04:00
parent baad7da10d
commit 88ecf675b6
Signed by: Arav
GPG Key ID: 25969B23DCB5CA34
3 changed files with 5 additions and 5 deletions

View File

@ -123,6 +123,8 @@ func main() {
}
return
case syscall.SIGHUP:
plylst.Lock()
defer plylst.Unlock()
if err := plylst.Reload(); err != nil {
log.Println(err)
}

View File

@ -69,7 +69,9 @@ const defaultTitleTagNoArtist = "[And no title tag either! Pffft]"
func (dj *DJHandlers) PlaylistNext(w http.ResponseWriter, _ *http.Request) {
w.Header().Add("Content-Type", "text/plain")
dj.playlist.Lock()
nxt := dj.playlist.Next()
dj.playlist.Unlock()
if nxt == "" {
log.Println("the end of a playlist has been reached")
if nxt = dj.fallbackSong; nxt == "" {

View File

@ -10,7 +10,7 @@ import (
// Playlist holds a list of paths to a song files.
type Playlist struct {
mutex sync.Mutex
sync.Mutex
filePath string
playlist []string
cur int
@ -27,8 +27,6 @@ func NewPlaylist(filePath string, repeat bool) (*Playlist, error) {
// Next returns the next song to play. Returns an empty string if repeat is
// false and the end of a playlist was reached.
func (p *Playlist) Next() (song string) {
p.mutex.Lock()
defer p.mutex.Unlock()
if p.cur == len(p.playlist) {
// If the end of a playlist was reached and repeat is set to true,
// then go back to the head of it, thus repeating it. Return an empty
@ -55,10 +53,8 @@ func (p *Playlist) load() error {
return errors.New("a playlist file is empty. Did not update")
}
p.mutex.Lock()
p.playlist = strings.Split(string(data), "\n")
p.cur = 0
p.mutex.Unlock()
return nil
}