Skip to content

Commit

Permalink
scanner/mapping: use musicbrainz ids if configured to
Browse files Browse the repository at this point in the history
Not MD5'ing them because they're UUIDs and can be
eyeball'd easily in the database.
  • Loading branch information
vs49688 committed Oct 26, 2022
1 parent 466eb4b commit 210eed7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
31 changes: 30 additions & 1 deletion scanner/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"time"

"github.com/google/uuid"
"github.com/kennygrant/sanitize"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/consts"
Expand All @@ -19,12 +20,14 @@ import (
type mediaFileMapper struct {
rootFolder string
genres model.GenreRepository
useMbzIDs bool
}

func newMediaFileMapper(rootFolder string, genres model.GenreRepository) *mediaFileMapper {
func newMediaFileMapper(rootFolder string, genres model.GenreRepository, useMbzIDs bool) *mediaFileMapper {
return &mediaFileMapper{
rootFolder: rootFolder,
genres: genres,
useMbzIDs: useMbzIDs,
}
}

Expand Down Expand Up @@ -120,20 +123,46 @@ func (s mediaFileMapper) mapAlbumName(md metadata.Tags) string {
return name
}

func (s mediaFileMapper) mapMbzID(id string) string {
if !s.useMbzIDs {
return ""
}

if _, err := uuid.Parse(id); err != nil {
return ""
}

return id
}

func (s mediaFileMapper) trackID(md metadata.Tags) string {
if mbzID := s.mapMbzID(md.MbzReleaseTrackID()); mbzID != "" {
return mbzID
}

return fmt.Sprintf("%x", md5.Sum([]byte(md.FilePath())))
}

func (s mediaFileMapper) albumID(md metadata.Tags) string {
if id := s.mapMbzID(md.MbzAlbumID()); id != "" {
return id
}

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.Tags) string {
if id := s.mapMbzID(md.MbzArtistID()); id != "" {
return id
}
return fmt.Sprintf("%x", md5.Sum([]byte(strings.ToLower(s.mapArtistName(md)))))
}

func (s mediaFileMapper) albumArtistID(md metadata.Tags) string {
if id := s.mapMbzID(md.MbzAlbumArtistID()); id != "" {
return id
}
return fmt.Sprintf("%x", md5.Sum([]byte(strings.ToLower(s.mapAlbumArtistName(md)))))
}

Expand Down
2 changes: 1 addition & 1 deletion scanner/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var _ = Describe("mapping", func() {
ds := &tests.MockDataStore{}
gr = ds.Genre(ctx)
gr = newCachedGenreRepository(ctx, gr)
mapper = newMediaFileMapper("/", gr)
mapper = newMediaFileMapper("/", gr, false)
})

It("returns empty if no genres are available", func() {
Expand Down
8 changes: 7 additions & 1 deletion scanner/tag_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ func (s *TagScanner) Scan(ctx context.Context, lastModifiedSince time.Time, prog
var changedDirs []string
s.cnt = &counters{}
genres := newCachedGenreRepository(ctx, s.ds.Genre(ctx))
s.mapper = newMediaFileMapper(s.rootFolder, genres)

useMbzIds, err := s.ds.Property(ctx).DefaultGetBool(model.PropUsingMbzIDs, false)
if err != nil {
return 0, err
}

s.mapper = newMediaFileMapper(s.rootFolder, genres, useMbzIds)

foldersFound, walkerError := s.getRootFolderWalker(ctx)
for {
Expand Down

0 comments on commit 210eed7

Please sign in to comment.