From 5c279814094aedf25fff4ed0127e321d1d03a731 Mon Sep 17 00:00:00 2001 From: Wessie Date: Sun, 5 Jan 2025 00:27:33 +0100 Subject: [PATCH] templates: make SongFileSize work with both Song and PendingSong --- go.mod | 2 +- templates/functions.go | 23 +++++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index ff103685..e45bb015 100644 --- a/go.mod +++ b/go.mod @@ -20,6 +20,7 @@ require ( github.com/cenkalti/backoff v2.2.1+incompatible github.com/cenkalti/backoff/v4 v4.3.0 github.com/davecgh/go-spew v1.1.1 + github.com/dustin/go-humanize v1.0.1 github.com/go-chi/httprate v0.14.1 github.com/go-sql-driver/mysql v1.8.1 github.com/golang-migrate/migrate/v4 v4.18.1 @@ -96,7 +97,6 @@ require ( github.com/docker/docker v27.4.0+incompatible // indirect github.com/docker/go-connections v0.5.0 // indirect github.com/docker/go-units v0.5.0 // indirect - github.com/dustin/go-humanize v1.0.1 // indirect github.com/felixge/httpsnoop v1.0.4 // indirect github.com/go-logr/logr v1.4.2 // indirect github.com/go-logr/stdr v1.2.2 // indirect diff --git a/templates/functions.go b/templates/functions.go index ec4b4701..46d88b09 100644 --- a/templates/functions.go +++ b/templates/functions.go @@ -32,8 +32,27 @@ func (sf *StatefulFuncs) Status() radio.Status { return sf.status.Latest() } -func (sf *StatefulFuncs) SongFileSize(song radio.Song) string { - path := util.AbsolutePath(sf.Conf().MusicPath, song.FilePath) +func (sf *StatefulFuncs) SongFileSize(song any) string { + var path string + switch s := song.(type) { + case radio.Song: + path = s.FilePath + case *radio.Song: + if s != nil { + path = s.FilePath + } + case radio.PendingSong: + path = s.FilePath + case *radio.PendingSong: + if s != nil { + path = s.FilePath + } + default: + return "??? MiB" + } + + // make the path absolute + path = util.AbsolutePath(sf.Conf().MusicPath, path) fi, err := os.Stat(path) if err != nil {