Remove duplicated helper functions, move them to `utils` package

This commit is contained in:
Deluan 2020-06-24 18:12:23 -04:00 committed by Deluan Quintão
parent bb9a7fadc0
commit eb109ebeb4
5 changed files with 61 additions and 21 deletions

View File

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

View File

@ -2,7 +2,6 @@ package persistence
import (
"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)

View File

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

17
utils/files.go Normal file
View File

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

38
utils/files_test.go Normal file
View File

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