From 1187ee7cc1cec23a4281d7b47c23cd8657b69f14 Mon Sep 17 00:00:00 2001 From: Deluan Date: Fri, 4 Sep 2020 11:08:16 -0400 Subject: [PATCH] Moved Metadata Extraction to its own package --- scanner/mapping.go | 19 ++++++++++--------- .../ffmpeg.go} | 2 +- .../ffmpeg_test.go} | 2 +- scanner/{ => metadata}/metadata.go | 9 +++++++-- scanner/tag_scanner.go | 8 ++------ 5 files changed, 21 insertions(+), 19 deletions(-) rename scanner/{metadata_ffmpeg.go => metadata/ffmpeg.go} (99%) rename scanner/{metadata_ffmpeg_test.go => metadata/ffmpeg_test.go} (99%) rename scanner/{ => metadata}/metadata.go (76%) diff --git a/scanner/mapping.go b/scanner/mapping.go index b216f274f..2e5ff0dec 100644 --- a/scanner/mapping.go +++ b/scanner/mapping.go @@ -9,6 +9,7 @@ import ( "github.com/deluan/navidrome/consts" "github.com/deluan/navidrome/model" + "github.com/deluan/navidrome/scanner/metadata" "github.com/deluan/navidrome/utils" "github.com/kennygrant/sanitize" ) @@ -21,7 +22,7 @@ func newMediaFileMapper(rootFolder string) *mediaFileMapper { 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.ID = s.trackID(md) mf.Title = s.mapTrackTitle(md) @@ -64,7 +65,7 @@ func sanitizeFieldForSorting(originalValue string) string { return utils.NoArticle(v) } -func (s *mediaFileMapper) mapTrackTitle(md Metadata) string { +func (s *mediaFileMapper) mapTrackTitle(md metadata.Metadata) string { if md.Title() == "" { s := strings.TrimPrefix(md.FilePath(), s.rootFolder+string(os.PathSeparator)) e := filepath.Ext(s) @@ -73,7 +74,7 @@ func (s *mediaFileMapper) mapTrackTitle(md Metadata) string { return md.Title() } -func (s *mediaFileMapper) mapAlbumArtistName(md Metadata) string { +func (s *mediaFileMapper) mapAlbumArtistName(md metadata.Metadata) string { switch { case md.Compilation(): 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() != "" { return md.Artist() } return consts.UnknownArtist } -func (s *mediaFileMapper) mapAlbumName(md Metadata) string { +func (s *mediaFileMapper) mapAlbumName(md metadata.Metadata) string { name := md.Album() if name == "" { return "[Unknown Album]" @@ -101,19 +102,19 @@ func (s *mediaFileMapper) mapAlbumName(md Metadata) string { 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()))) } -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))) 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))))) } -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))))) } diff --git a/scanner/metadata_ffmpeg.go b/scanner/metadata/ffmpeg.go similarity index 99% rename from scanner/metadata_ffmpeg.go rename to scanner/metadata/ffmpeg.go index 0c59cdc4f..426231e08 100644 --- a/scanner/metadata_ffmpeg.go +++ b/scanner/metadata/ffmpeg.go @@ -1,4 +1,4 @@ -package scanner +package metadata import ( "bufio" diff --git a/scanner/metadata_ffmpeg_test.go b/scanner/metadata/ffmpeg_test.go similarity index 99% rename from scanner/metadata_ffmpeg_test.go rename to scanner/metadata/ffmpeg_test.go index e1c7d219d..a11f16791 100644 --- a/scanner/metadata_ffmpeg_test.go +++ b/scanner/metadata/ffmpeg_test.go @@ -1,4 +1,4 @@ -package scanner +package metadata import ( . "github.com/onsi/ginkgo" diff --git a/scanner/metadata.go b/scanner/metadata/metadata.go similarity index 76% rename from scanner/metadata.go rename to scanner/metadata/metadata.go index b2a656186..00139db0a 100644 --- a/scanner/metadata.go +++ b/scanner/metadata/metadata.go @@ -1,4 +1,4 @@ -package scanner +package metadata import "time" @@ -28,6 +28,11 @@ type Metadata interface { Size() int64 } -type MetadataExtractor interface { +type Extractor interface { Extract(files ...string) (map[string]Metadata, error) } + +func Extract(files ...string) (map[string]Metadata, error) { + e := &ffmpegMetadataExtractor{} + return e.Extract(files...) +} diff --git a/scanner/tag_scanner.go b/scanner/tag_scanner.go index 12ebedb69..13b95ae54 100644 --- a/scanner/tag_scanner.go +++ b/scanner/tag_scanner.go @@ -12,6 +12,7 @@ import ( "github.com/deluan/navidrome/log" "github.com/deluan/navidrome/model" "github.com/deluan/navidrome/model/request" + "github.com/deluan/navidrome/scanner/metadata" "github.com/deluan/navidrome/utils" ) @@ -340,13 +341,8 @@ func (s *TagScanner) addOrUpdateTracksInDB(ctx context.Context, dir string, curr return numUpdatedTracks, nil } -func (s *TagScanner) newMetadataExtractor() MetadataExtractor { - return &ffmpegMetadataExtractor{} -} - func (s *TagScanner) loadTracks(filePaths []string) (model.MediaFiles, error) { - e := s.newMetadataExtractor() - mds, err := e.Extract(filePaths...) + mds, err := metadata.Extract(filePaths...) if err != nil { return nil, err }