1
0

Do not overwrite mostlistenedsong file if a song wasn't changed.

This commit is contained in:
Alexander Andreev 2023-09-10 18:23:06 +04:00
parent cbf8eb8747
commit c020031127
Signed by: Arav
GPG Key ID: D22A817D95815393
2 changed files with 13 additions and 3 deletions

View File

@ -74,9 +74,12 @@ func main() {
}
defer func() {
err := os.WriteFile(*mostListenedSongPath, radio.StoreMostListenedSong(), fs.ModePerm)
if err != nil {
log.Println("failed to store a most listened song:", err)
fileData := radio.StoreMostListenedSong()
if fileData != nil {
err := os.WriteFile(*mostListenedSongPath, fileData, fs.ModePerm)
if err != nil {
log.Println("failed to store a most listened song:", err)
}
}
if err := srv.Stop(); err != nil {

View File

@ -9,6 +9,8 @@ import (
const MostListenedDateFormat = "02 January 2006"
var mlsChanged = false
type MostListenedSong struct {
Listeners int
Date time.Time
@ -43,6 +45,7 @@ func CheckAndUpdateMostListenedSong(cur, prev Song) {
Date: time.Now().UTC(),
Song: prev.Song}
}
mlsChanged = true
}
// MostListened returns song that currently is the song with most simultaneous
@ -68,6 +71,10 @@ func LoadMostListenedSong(data []byte) (err error) {
}
func StoreMostListenedSong() []byte {
if !mlsChanged {
return nil
}
buf := make([]byte, 0, 30+len(mostListened.Song))
b := bytes.NewBuffer(buf)