Refactor file type functions

This commit is contained in:
Deluan 2022-12-23 11:32:39 -05:00 committed by Deluan Quintão
parent 9ec349dce0
commit 8c1cd9c273
10 changed files with 75 additions and 98 deletions

View File

@ -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 hashing
keys := 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)

View File

@ -1,9 +1,11 @@
package utils
package model
import (
"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"
}

61
model/file_types_test.go Normal file
View File

@ -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())
})
})
})

View File

@ -2,7 +2,6 @@ package model
import (
"fmt"
"path/filepath"
"strconv"
"strings"
"time"
@ -138,8 +137,3 @@ type PlaylistTrackRepository interface {
DeleteAll() error
Reorder(pos int, newPos int) error
}
func IsValidPlaylist(filePath string) bool {
extension := strings.ToLower(filepath.Ext(filePath))
return extension == ".m3u" || extension == ".m3u8" || extension == ".nsp"
}

View File

@ -1,27 +1,11 @@
package model_test
import (
"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.Playlist

View File

@ -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] = f

View File

@ -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()

View File

@ -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())
})
})
})

View File

@ -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:]...)...)
}

View File

@ -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"))