Fixed last songs displaying.
This commit is contained in:
parent
1ebecc91d9
commit
517dd0e534
@ -24,7 +24,6 @@ const (
|
||||
var (
|
||||
lastPlayedCache []Song
|
||||
lastPlayedCacheMutex sync.Mutex
|
||||
isJustStarted bool = true
|
||||
)
|
||||
|
||||
type IcecastStatusDTO struct {
|
||||
@ -79,15 +78,12 @@ func IcecastGetStatus(icecastURL string) (*IcecastStatus, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func IcecastLastPlayedSongs(lastNSongs int, playlistPath string) ([]Song, error) {
|
||||
func IcecastLastSongs(playlistPath string) ([]Song, error) {
|
||||
lastPlayedCacheMutex.Lock()
|
||||
defer lastPlayedCacheMutex.Unlock()
|
||||
if lpcLen := len(lastPlayedCache); lpcLen > 0 {
|
||||
if lastNSongs > lpcLen {
|
||||
lastNSongs = lpcLen
|
||||
}
|
||||
var ret []Song = make([]Song, lastNSongs)
|
||||
copy(ret[:], lastPlayedCache[lpcLen-lastNSongs:])
|
||||
ret := make([]Song, 0, lpcLen)
|
||||
ret = append(ret, lastPlayedCache...)
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
@ -104,7 +100,6 @@ func IcecastLastSong(playlistPath string) (*Song, error) {
|
||||
}
|
||||
|
||||
func icecastLastPlayedSongs(playlistPath string, n int) ([]Song, error) {
|
||||
songs := make([]Song, n)
|
||||
var buf []byte
|
||||
var offset int64 = 0
|
||||
|
||||
@ -134,16 +129,18 @@ func icecastLastPlayedSongs(playlistPath string, n int) ([]Song, error) {
|
||||
|
||||
lines := bytes.Split(buf, []byte("\n"))
|
||||
|
||||
if len(lines) < 3 {
|
||||
if len(lines) < 2 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
lines = lines[:len(lines)-2]
|
||||
lines = lines[:len(lines)-1]
|
||||
|
||||
if len(lines) > n {
|
||||
lines = lines[len(lines)-n:]
|
||||
}
|
||||
|
||||
songs := make([]Song, 0, len(lines))
|
||||
|
||||
for _, line := range lines {
|
||||
fields := bytes.Split(line, []byte("|"))
|
||||
|
||||
@ -184,27 +181,21 @@ func (pw *PlaylistLogWatcher) Watch(playlistPath string, n int) (err error) {
|
||||
|
||||
pw.watcher.WatchForMask(pw.changed, watcher.ModIgnMask)
|
||||
|
||||
lastPlayedCache, err = icecastLastPlayedSongs(playlistPath, n)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "cannot instantiate last played cache")
|
||||
}
|
||||
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case mask := <-pw.changed:
|
||||
if mask&syscall.IN_MODIFY > 0 {
|
||||
if isJustStarted {
|
||||
isJustStarted = false
|
||||
} else {
|
||||
lastPlayedCacheMutex.Lock()
|
||||
if lastPlayedCache == nil {
|
||||
lastPlayedCache = make([]Song, n)
|
||||
}
|
||||
if songs, err := icecastLastPlayedSongs(playlistPath, 1); err == nil && len(songs) > 0 {
|
||||
if len(lastPlayedCache) == n {
|
||||
lastPlayedCache = append(lastPlayedCache[1:], songs...)
|
||||
} else {
|
||||
lastPlayedCache = append(lastPlayedCache, songs...)
|
||||
}
|
||||
if songs, err := icecastLastPlayedSongs(playlistPath, n); err == nil && len(songs) > 0 {
|
||||
lastPlayedCache = songs
|
||||
}
|
||||
lastPlayedCacheMutex.Unlock()
|
||||
}
|
||||
} else if mask&syscall.IN_IGNORED > 0 {
|
||||
pw.Close()
|
||||
pw.Watch(playlistPath, n)
|
||||
|
Loading…
Reference in New Issue
Block a user