🎧 Your Personal Streaming Service
Go airsonicmadsonicmedia-servermusicmusic-servernavidromeopensubsonicraspberry-piself-hostedstreamerstreaming-apistreaming-audiosubsonicsubsonic-server
Remove duplicated helper functions, move them to `utils` package
Deluaneb109ebeb462
diff --git a/engine/cover.go b/engine/cover.goindex df054ae83..7712ee804 100644--- a/engine/cover.go+++ b/engine/cover.go@@ -10,9 +10,7 @@ import ("image/jpeg"_ "image/png""io"- "mime""os"- "path/filepath""strings""time"@@ -21,6 +19,7 @@ import ("github.com/deluan/navidrome/log""github.com/deluan/navidrome/model""github.com/deluan/navidrome/resources"+ "github.com/deluan/navidrome/utils""github.com/dhowden/tag""github.com/disintegration/imaging""github.com/djherbis/fscache"@@ -141,7 +140,7 @@ func (c *cover) getCover(ctx context.Context, path string, size int) (reader io.}var data []byte- if isAudioFile(filepath.Ext(path)) {+ if utils.IsAudioFile(path) {data, err = readFromTag(path)} else {data, err = readFromFile(path)@@ -207,10 +206,6 @@ func readFromFile(path string) ([]byte, error) {return buf.Bytes(), nil}-func isAudioFile(extension string) bool {- return strings.HasPrefix(mime.TypeByExtension(extension), "audio/")-}-func NewImageCache() (ImageCache, error) {return newFileCache("Image", conf.Server.ImageCacheSize, consts.ImageCacheDir, consts.DefaultImageCacheMaxItems)}diff --git a/persistence/album_repository.go b/persistence/album_repository.goindex 0ef49c6cd..a30ab4046 100644--- a/persistence/album_repository.go+++ b/persistence/album_repository.go@@ -2,7 +2,6 @@ package persistenceimport ("context"- "mime""os""path/filepath""sort"@@ -16,6 +15,7 @@ import ("github.com/deluan/navidrome/consts""github.com/deluan/navidrome/log""github.com/deluan/navidrome/model"+ "github.com/deluan/navidrome/utils""github.com/deluan/rest")@@ -222,7 +222,7 @@ func getCoverFromPath(path string, hasEmbeddedCover bool) string {for _, name := range names {match, _ := filepath.Match(pat, strings.ToLower(name))- if match && isImageFile(filepath.Ext(name)) {+ if match && utils.IsImageFile(name) {return filepath.Join(filepath.Dir(path), name)}}@@ -231,10 +231,6 @@ func getCoverFromPath(path string, hasEmbeddedCover bool) string {return ""}-func isImageFile(extension string) bool {- return strings.HasPrefix(mime.TypeByExtension(extension), "image/")-}-func (r *albumRepository) purgeEmpty() error {del := Delete(r.tableName).Where("id not in (select distinct(album_id) from media_file)")c, err := r.executeSQL(del)diff --git a/scanner/metadata_ffmpeg.go b/scanner/metadata_ffmpeg.goindex e647d8cca..c74965536 100644--- a/scanner/metadata_ffmpeg.go+++ b/scanner/metadata_ffmpeg.go@@ -4,7 +4,6 @@ import ("bufio""errors""fmt"- "mime""os""os/exec""path"@@ -16,6 +15,7 @@ import ("github.com/deluan/navidrome/conf""github.com/deluan/navidrome/log"+ "github.com/deluan/navidrome/utils")type Metadata struct {@@ -66,8 +66,7 @@ func LoadAllAudioFiles(dirPath string) (map[string]os.FileInfo, error) {continue}filePath := filepath.Join(dirPath, f.Name())- extension := path.Ext(filePath)- if !isAudioFile(extension) {+ if !utils.IsAudioFile(filePath) {continue}fi, err := os.Stat(filePath)@@ -159,11 +158,6 @@ func extractMetadata(filePath, info string) (*Metadata, error) {return m, nil}-func isAudioFile(extension string) bool {- typ := mime.TypeByExtension(extension)- return strings.HasPrefix(typ, "audio/")-}-func (m *Metadata) parseInfo(info string) {reader := strings.NewReader(info)scanner := bufio.NewScanner(reader)diff --git a/utils/files.go b/utils/files.gonew file mode 100644index 000000000..449c6d365--- /dev/null+++ b/utils/files.go@@ -0,0 +1,17 @@+package utils++import (+ "mime"+ "path/filepath"+ "strings"+)++func IsAudioFile(filePath string) bool {+ extension := filepath.Ext(filePath)+ return strings.HasPrefix(mime.TypeByExtension(extension), "audio/")+}++func IsImageFile(filePath string) bool {+ extension := filepath.Ext(filePath)+ return strings.HasPrefix(mime.TypeByExtension(extension), "image/")+}diff --git a/utils/files_test.go b/utils/files_test.gonew file mode 100644index 000000000..3ba2148bb--- /dev/null+++ b/utils/files_test.go@@ -0,0 +1,38 @@+package utils++import (+ "path/filepath"++ . "github.com/onsi/ginkgo"+ . "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())+ })+ })++ 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())+ })+ })+})