feat(server/m7-379): extract MusicBrainz IDs from audio tags during scan

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-04 19:01:55 -04:00
parent 412a1c00ac
commit ab8fff834a
3 changed files with 144 additions and 6 deletions
+16 -6
View File
@@ -131,6 +131,7 @@ func (s *Scanner) scanFile(ctx context.Context, q *dbq.Queries, path string, sta
if err != nil {
return fmt.Errorf("tag read: %w", err)
}
albumMBID, artistMBID := extractMBIDs(meta.Raw())
artistName := meta.Artist()
if artistName == "" {
@@ -145,11 +146,11 @@ func (s *Scanner) scanFile(ctx context.Context, q *dbq.Queries, path string, sta
trackTitle = strings.TrimSuffix(filepath.Base(path), filepath.Ext(path))
}
artist, err := s.resolveArtist(ctx, q, artistName)
artist, err := s.resolveArtist(ctx, q, artistName, artistMBID)
if err != nil {
return fmt.Errorf("artist: %w", err)
}
album, err := s.resolveAlbum(ctx, q, artist.ID, albumTitle, meta.Year())
album, err := s.resolveAlbum(ctx, q, artist.ID, albumTitle, meta.Year(), albumMBID)
if err != nil {
return fmt.Errorf("album: %w", err)
}
@@ -197,7 +198,7 @@ func (s *Scanner) scanFile(ctx context.Context, q *dbq.Queries, path string, sta
return nil
}
func (s *Scanner) resolveArtist(ctx context.Context, q *dbq.Queries, name string) (dbq.Artist, error) {
func (s *Scanner) resolveArtist(ctx context.Context, q *dbq.Queries, name, mbid string) (dbq.Artist, error) {
existing, err := q.GetArtistByName(ctx, name)
if err == nil {
return existing, nil
@@ -205,13 +206,18 @@ func (s *Scanner) resolveArtist(ctx context.Context, q *dbq.Queries, name string
if !errors.Is(err, pgx.ErrNoRows) {
return dbq.Artist{}, err
}
return q.UpsertArtist(ctx, dbq.UpsertArtistParams{
params := dbq.UpsertArtistParams{
Name: name,
SortName: sortKey(name),
})
}
if mbid != "" {
m := mbid
params.Mbid = &m
}
return q.UpsertArtist(ctx, params)
}
func (s *Scanner) resolveAlbum(ctx context.Context, q *dbq.Queries, artistID pgtype.UUID, title string, year int) (dbq.Album, error) {
func (s *Scanner) resolveAlbum(ctx context.Context, q *dbq.Queries, artistID pgtype.UUID, title string, year int, mbid string) (dbq.Album, error) {
existing, err := q.GetAlbumByArtistAndTitle(ctx, dbq.GetAlbumByArtistAndTitleParams{ArtistID: artistID, Title: title})
if err == nil {
return existing, nil
@@ -233,6 +239,10 @@ func (s *Scanner) resolveAlbum(ctx context.Context, q *dbq.Queries, artistID pgt
s.logger.Warn("library scan: dropping invalid release year",
"year", year, "album", title)
}
if mbid != "" {
m := mbid
params.Mbid = &m
}
return q.UpsertAlbum(ctx, params)
}