Moved Metadata Extraction to its own package

This commit is contained in:
Deluan 2020-09-04 11:08:16 -04:00 committed by Deluan Quintão
parent 0beec552b1
commit 1187ee7cc1
5 changed files with 21 additions and 19 deletions

View file

@ -9,6 +9,7 @@ import (
"github.com/deluan/navidrome/consts" "github.com/deluan/navidrome/consts"
"github.com/deluan/navidrome/model" "github.com/deluan/navidrome/model"
"github.com/deluan/navidrome/scanner/metadata"
"github.com/deluan/navidrome/utils" "github.com/deluan/navidrome/utils"
"github.com/kennygrant/sanitize" "github.com/kennygrant/sanitize"
) )
@ -21,7 +22,7 @@ func newMediaFileMapper(rootFolder string) *mediaFileMapper {
return &mediaFileMapper{rootFolder: rootFolder} return &mediaFileMapper{rootFolder: rootFolder}
} }
func (s *mediaFileMapper) toMediaFile(md Metadata) model.MediaFile { func (s *mediaFileMapper) toMediaFile(md metadata.Metadata) model.MediaFile {
mf := &model.MediaFile{} mf := &model.MediaFile{}
mf.ID = s.trackID(md) mf.ID = s.trackID(md)
mf.Title = s.mapTrackTitle(md) mf.Title = s.mapTrackTitle(md)
@ -64,7 +65,7 @@ func sanitizeFieldForSorting(originalValue string) string {
return utils.NoArticle(v) return utils.NoArticle(v)
} }
func (s *mediaFileMapper) mapTrackTitle(md Metadata) string { func (s *mediaFileMapper) mapTrackTitle(md metadata.Metadata) string {
if md.Title() == "" { if md.Title() == "" {
s := strings.TrimPrefix(md.FilePath(), s.rootFolder+string(os.PathSeparator)) s := strings.TrimPrefix(md.FilePath(), s.rootFolder+string(os.PathSeparator))
e := filepath.Ext(s) e := filepath.Ext(s)
@ -73,7 +74,7 @@ func (s *mediaFileMapper) mapTrackTitle(md Metadata) string {
return md.Title() return md.Title()
} }
func (s *mediaFileMapper) mapAlbumArtistName(md Metadata) string { func (s *mediaFileMapper) mapAlbumArtistName(md metadata.Metadata) string {
switch { switch {
case md.Compilation(): case md.Compilation():
return consts.VariousArtists return consts.VariousArtists
@ -86,14 +87,14 @@ func (s *mediaFileMapper) mapAlbumArtistName(md Metadata) string {
} }
} }
func (s *mediaFileMapper) mapArtistName(md Metadata) string { func (s *mediaFileMapper) mapArtistName(md metadata.Metadata) string {
if md.Artist() != "" { if md.Artist() != "" {
return md.Artist() return md.Artist()
} }
return consts.UnknownArtist return consts.UnknownArtist
} }
func (s *mediaFileMapper) mapAlbumName(md Metadata) string { func (s *mediaFileMapper) mapAlbumName(md metadata.Metadata) string {
name := md.Album() name := md.Album()
if name == "" { if name == "" {
return "[Unknown Album]" return "[Unknown Album]"
@ -101,19 +102,19 @@ func (s *mediaFileMapper) mapAlbumName(md Metadata) string {
return name return name
} }
func (s *mediaFileMapper) trackID(md Metadata) string { func (s *mediaFileMapper) trackID(md metadata.Metadata) string {
return fmt.Sprintf("%x", md5.Sum([]byte(md.FilePath()))) return fmt.Sprintf("%x", md5.Sum([]byte(md.FilePath())))
} }
func (s *mediaFileMapper) albumID(md Metadata) string { func (s *mediaFileMapper) albumID(md metadata.Metadata) string {
albumPath := strings.ToLower(fmt.Sprintf("%s\\%s", s.mapAlbumArtistName(md), s.mapAlbumName(md))) albumPath := strings.ToLower(fmt.Sprintf("%s\\%s", s.mapAlbumArtistName(md), s.mapAlbumName(md)))
return fmt.Sprintf("%x", md5.Sum([]byte(albumPath))) return fmt.Sprintf("%x", md5.Sum([]byte(albumPath)))
} }
func (s *mediaFileMapper) artistID(md Metadata) string { func (s *mediaFileMapper) artistID(md metadata.Metadata) string {
return fmt.Sprintf("%x", md5.Sum([]byte(strings.ToLower(s.mapArtistName(md))))) return fmt.Sprintf("%x", md5.Sum([]byte(strings.ToLower(s.mapArtistName(md)))))
} }
func (s *mediaFileMapper) albumArtistID(md Metadata) string { func (s *mediaFileMapper) albumArtistID(md metadata.Metadata) string {
return fmt.Sprintf("%x", md5.Sum([]byte(strings.ToLower(s.mapAlbumArtistName(md))))) return fmt.Sprintf("%x", md5.Sum([]byte(strings.ToLower(s.mapAlbumArtistName(md)))))
} }

View file

@ -1,4 +1,4 @@
package scanner package metadata
import ( import (
"bufio" "bufio"

View file

@ -1,4 +1,4 @@
package scanner package metadata
import ( import (
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"

View file

@ -1,4 +1,4 @@
package scanner package metadata
import "time" import "time"
@ -28,6 +28,11 @@ type Metadata interface {
Size() int64 Size() int64
} }
type MetadataExtractor interface { type Extractor interface {
Extract(files ...string) (map[string]Metadata, error) Extract(files ...string) (map[string]Metadata, error)
} }
func Extract(files ...string) (map[string]Metadata, error) {
e := &ffmpegMetadataExtractor{}
return e.Extract(files...)
}

View file

@ -12,6 +12,7 @@ import (
"github.com/deluan/navidrome/log" "github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model" "github.com/deluan/navidrome/model"
"github.com/deluan/navidrome/model/request" "github.com/deluan/navidrome/model/request"
"github.com/deluan/navidrome/scanner/metadata"
"github.com/deluan/navidrome/utils" "github.com/deluan/navidrome/utils"
) )
@ -340,13 +341,8 @@ func (s *TagScanner) addOrUpdateTracksInDB(ctx context.Context, dir string, curr
return numUpdatedTracks, nil return numUpdatedTracks, nil
} }
func (s *TagScanner) newMetadataExtractor() MetadataExtractor {
return &ffmpegMetadataExtractor{}
}
func (s *TagScanner) loadTracks(filePaths []string) (model.MediaFiles, error) { func (s *TagScanner) loadTracks(filePaths []string) (model.MediaFiles, error) {
e := s.newMetadataExtractor() mds, err := metadata.Extract(filePaths...)
mds, err := e.Extract(filePaths...)
if err != nil { if err != nil {
return nil, err return nil, err
} }