1
0

Rewritten lastPlayedSong() in a sane way. Hope it will work.

This commit is contained in:
Alexander Andreev 2022-08-29 12:47:06 +04:00
parent d608cf1a2d
commit 16e849eb65
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F

View File

@ -7,7 +7,6 @@ import (
"io" "io"
"net/http" "net/http"
"os" "os"
"strings"
"sync" "sync"
"syscall" "syscall"
"time" "time"
@ -115,12 +114,12 @@ func IcecastLastSong(playlistPath string) (Song, error) {
func lastPlayedSong(playlistPath string) (*Song, error) { func lastPlayedSong(playlistPath string) (*Song, error) {
buf := make([]byte, bufferSize) buf := make([]byte, bufferSize)
var last_song_line string
playlist, err := os.Open(playlistPath) playlist, err := os.Open(playlistPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer playlist.Close()
playlist_stat, _ := playlist.Stat() playlist_stat, _ := playlist.Stat()
@ -135,24 +134,20 @@ func lastPlayedSong(playlistPath string) (*Song, error) {
return nil, err return nil, err
} }
newline_end_pos := bytes.LastIndexByte(buf, '\n') lines := bytes.Split(buf, []byte("\n"))
if newline_end_pos == -1 { if len(lines) < 2 {
newline_end_pos = len(buf) return nil, nil
} }
newline_start_pos := bytes.LastIndexByte(buf[:newline_end_pos-1], '\n') fields := bytes.Split(lines[len(lines)-2], []byte("|"))
last_song_line = string(buf[newline_start_pos+1 : newline_end_pos-1]) tim, _ := time.Parse(IcecastPlaylistDateFormat, string(fields[0]))
fields := strings.Split(last_song_line, "|")
tim, _ := time.Parse(IcecastPlaylistDateFormat, fields[0])
return &Song{ return &Song{
Time: tim.Format(SongTimeFormat), Time: tim.Format(SongTimeFormat),
Listeners: fields[2], Listeners: string(fields[2]),
Song: fields[3]}, nil Song: string(fields[3])}, nil
} }
var playlistWatcher watcher.InotifyWatcher var playlistWatcher watcher.InotifyWatcher