🎧 Your Personal Streaming Service
Go airsonicmadsonicmedia-servermusicmusic-servernavidromeopensubsonicraspberry-piself-hostedstreamerstreaming-apistreaming-audiosubsonicsubsonic-server
Refactor file type functions
Deluan8c1cd9c273d9
diff --git a/core/agents/lastfm/client.go b/core/agents/lastfm/client.goindex cdb297997..2757c96f8 100644--- a/core/agents/lastfm/client.go+++ b/core/agents/lastfm/client.go@@ -14,7 +14,7 @@ import ("time""github.com/navidrome/navidrome/log"- "github.com/navidrome/navidrome/utils"+ "golang.org/x/exp/slices")const (@@ -204,7 +204,7 @@ func (c *Client) sign(params url.Values) {// the parameters must be in order before hashingkeys := make([]string, 0, len(params))for k := range params {- if utils.StringInSlice(k, []string{"format", "callback"}) {+ if slices.Contains([]string{"format", "callback"}, k) {continue}keys = append(keys, k)diff --git a/utils/files.go b/model/file_types.gosimilarity index 55%rename from utils/files.gorename to model/file_types.goindex 5e9150c94..78dd7eaff 100644--- a/utils/files.go+++ b/model/file_types.go@@ -1,9 +1,11 @@-package utils+package modelimport ("mime""path/filepath""strings"++ "golang.org/x/exp/slices")var excludeAudioType = []string{@@ -14,10 +16,15 @@ var excludeAudioType = []string{func IsAudioFile(filePath string) bool {extension := filepath.Ext(filePath)mimeType := mime.TypeByExtension(extension)- return !StringInSlice(mimeType, excludeAudioType) && strings.HasPrefix(mimeType, "audio/")+ return !slices.Contains(excludeAudioType, mimeType) && strings.HasPrefix(mimeType, "audio/")}func IsImageFile(filePath string) bool {extension := filepath.Ext(filePath)return strings.HasPrefix(mime.TypeByExtension(extension), "image/")}++func IsValidPlaylist(filePath string) bool {+ extension := strings.ToLower(filepath.Ext(filePath))+ return extension == ".m3u" || extension == ".m3u8" || extension == ".nsp"+}diff --git a/model/file_types_test.go b/model/file_types_test.gonew file mode 100644index 000000000..93301e151--- /dev/null+++ b/model/file_types_test.go@@ -0,0 +1,61 @@+package model_test++import (+ "path/filepath"++ "github.com/navidrome/navidrome/model"+ . "github.com/onsi/ginkgo/v2"+ . "github.com/onsi/gomega"+)++var _ = Describe("File Types()", func() {+ Describe("IsAudioFile", func() {+ It("returns true for a MP3 file", func() {+ Expect(model.IsAudioFile(filepath.Join("path", "to", "test.mp3"))).To(BeTrue())+ })++ It("returns true for a FLAC file", func() {+ Expect(model.IsAudioFile("test.flac")).To(BeTrue())+ })++ It("returns false for a non-audio file", func() {+ Expect(model.IsAudioFile("test.jpg")).To(BeFalse())+ })++ It("returns false for m3u files", func() {+ Expect(model.IsAudioFile("test.m3u")).To(BeFalse())+ })++ It("returns false for pls files", func() {+ Expect(model.IsAudioFile("test.pls")).To(BeFalse())+ })+ })++ Describe("IsImageFile()", func() {+ It("returns true for a PNG file", func() {+ Expect(model.IsImageFile(filepath.Join("path", "to", "test.png"))).To(BeTrue())+ })++ It("returns true for a JPEG file", func() {+ Expect(model.IsImageFile("test.JPEG")).To(BeTrue())+ })++ It("returns false for a non-image file", func() {+ Expect(model.IsImageFile("test.mp3")).To(BeFalse())+ })+ })++ Describe("IsValidPlaylist()", func() {+ It("returns true for a M3U file", func() {+ Expect(model.IsValidPlaylist(filepath.Join("path", "to", "test.m3u"))).To(BeTrue())+ })++ It("returns true for a M3U8 file", func() {+ Expect(model.IsValidPlaylist(filepath.Join("path", "to", "test.m3u8"))).To(BeTrue())+ })++ It("returns false for a non-playlist file", func() {+ Expect(model.IsValidPlaylist("testm3u")).To(BeFalse())+ })+ })+})diff --git a/model/playlist.go b/model/playlist.goindex cef45c473..06329cf29 100644--- a/model/playlist.go+++ b/model/playlist.go@@ -2,7 +2,6 @@ package modelimport ("fmt"- "path/filepath""strconv""strings""time"@@ -138,8 +137,3 @@ type PlaylistTrackRepository interface {DeleteAll() errorReorder(pos int, newPos int) error}--func IsValidPlaylist(filePath string) bool {- extension := strings.ToLower(filepath.Ext(filePath))- return extension == ".m3u" || extension == ".m3u8" || extension == ".nsp"-}diff --git a/model/playlists_test.go b/model/playlists_test.goindex b5e2deb95..600e116cc 100644--- a/model/playlists_test.go+++ b/model/playlists_test.go@@ -1,27 +1,11 @@package model_testimport (- "path/filepath"-"github.com/navidrome/navidrome/model". "github.com/onsi/ginkgo/v2". "github.com/onsi/gomega")-var _ = Describe("IsValidPlaylist()", func() {- It("returns true for a M3U file", func() {- Expect(model.IsValidPlaylist(filepath.Join("path", "to", "test.m3u"))).To(BeTrue())- })-- It("returns true for a M3U8 file", func() {- Expect(model.IsValidPlaylist(filepath.Join("path", "to", "test.m3u8"))).To(BeTrue())- })-- It("returns false for a non-playlist file", func() {- Expect(model.IsValidPlaylist("testm3u")).To(BeFalse())- })-})-var _ = Describe("Playlist", func() {Describe("ToM3U8()", func() {var pls model.Playlistdiff --git a/scanner/tag_scanner.go b/scanner/tag_scanner.goindex 9cfe0e32f..37168b2cc 100644--- a/scanner/tag_scanner.go+++ b/scanner/tag_scanner.go@@ -402,7 +402,7 @@ func loadAllAudioFiles(dirPath string) (map[string]fs.DirEntry, error) {continue}filePath := filepath.Join(dirPath, f.Name())- if !utils.IsAudioFile(filePath) {+ if !model.IsAudioFile(filePath) {continue}fileInfos[filePath] = fdiff --git a/scanner/walk_dir_tree.go b/scanner/walk_dir_tree.goindex b32ba11f3..3f8d10f4d 100644--- a/scanner/walk_dir_tree.go+++ b/scanner/walk_dir_tree.go@@ -95,11 +95,11 @@ func loadDir(ctx context.Context, dirPath string) ([]string, *dirStats, error) {stats.ModTime = fileInfo.ModTime()}switch {- case utils.IsAudioFile(entry.Name()):+ case model.IsAudioFile(entry.Name()):stats.AudioFilesCount++case model.IsValidPlaylist(entry.Name()):stats.HasPlaylist = true- case utils.IsImageFile(entry.Name()):+ case model.IsImageFile(entry.Name()):stats.Images = append(stats.Images, entry.Name())if fileInfo.ModTime().After(stats.ImagesUpdatedAt) {stats.ImagesUpdatedAt = fileInfo.ModTime()diff --git a/utils/files_test.go b/utils/files_test.godeleted file mode 100644index 917960c8c..000000000--- a/utils/files_test.go+++ /dev/null@@ -1,46 +0,0 @@-package utils--import (- "path/filepath"-- . "github.com/onsi/ginkgo/v2"- . "github.com/onsi/gomega"-)--var _ = Describe("Files", func() {- Describe("IsAudioFile", func() {- It("returns true for a MP3 file", func() {- Expect(IsAudioFile(filepath.Join("path", "to", "test.mp3"))).To(BeTrue())- })-- It("returns true for a FLAC file", func() {- Expect(IsAudioFile("test.flac")).To(BeTrue())- })-- It("returns false for a non-audio file", func() {- Expect(IsAudioFile("test.jpg")).To(BeFalse())- })-- It("returns false for m3u files", func() {- Expect(IsAudioFile("test.m3u")).To(BeFalse())- })-- It("returns false for pls files", func() {- Expect(IsAudioFile("test.pls")).To(BeFalse())- })- })-- Describe("IsImageFile", func() {- It("returns true for a PNG file", func() {- Expect(IsImageFile(filepath.Join("path", "to", "test.png"))).To(BeTrue())- })-- It("returns true for a JPEG file", func() {- Expect(IsImageFile("test.JPEG")).To(BeTrue())- })-- It("returns false for a non-image file", func() {- Expect(IsImageFile("test.mp3")).To(BeFalse())- })- })-})diff --git a/utils/strings.go b/utils/strings.goindex 45bb39b16..c88c96b3e 100644--- a/utils/strings.go+++ b/utils/strings.go@@ -17,15 +17,6 @@ func NoArticle(name string) string {return name}-func StringInSlice(a string, slice []string) bool {- for _, b := range slice {- if b == a {- return true- }- }- return false-}-func InsertString(slice []string, value string, index int) []string {return append(slice[:index], append([]string{value}, slice[index:]...)...)}diff --git a/utils/strings_test.go b/utils/strings_test.goindex 20a59e166..165990754 100644--- a/utils/strings_test.go+++ b/utils/strings_test.go@@ -35,20 +35,6 @@ var _ = Describe("Strings", func() {})})- Describe("StringInSlice", func() {- It("returns false if slice is empty", func() {- Expect(StringInSlice("test", nil)).To(BeFalse())- })-- It("returns false if string is not found in slice", func() {- Expect(StringInSlice("aaa", []string{"bbb", "ccc"})).To(BeFalse())- })-- It("returns true if string is found in slice", func() {- Expect(StringInSlice("bbb", []string{"bbb", "aaa", "ccc"})).To(BeTrue())- })- })-Describe("MoveString", func() {It("moves item to end of slice", func() {Expect(MoveString([]string{"1", "2", "3"}, 0, 2)).To(ConsistOf("2", "3", "1"))