From 323b366ac6eb0da4e8186aed870cf529f760aabb Mon Sep 17 00:00:00 2001 From: bakerboy448 <55419169+bakerboy448@users.noreply.github.com> Date: Tue, 9 Sep 2025 10:57:02 -0500 Subject: [PATCH] Fixed: Improve Error Handling for file metadata tagging --- .../MediaFiles/AudioTagService.cs | 143 +++++++++++------- .../TrackImport/ImportApprovedTracks.cs | 13 +- 2 files changed, 102 insertions(+), 54 deletions(-) diff --git a/src/NzbDrone.Core/MediaFiles/AudioTagService.cs b/src/NzbDrone.Core/MediaFiles/AudioTagService.cs index 01bf410c7..e02d09e8b 100644 --- a/src/NzbDrone.Core/MediaFiles/AudioTagService.cs +++ b/src/NzbDrone.Core/MediaFiles/AudioTagService.cs @@ -74,66 +74,103 @@ public ParsedTrackInfo ReadTags(string path) public AudioTag GetTrackMetadata(TrackFile trackfile) { - var track = trackfile.Tracks.Value[0]; - var release = track.AlbumRelease.Value; - var album = release.Album.Value; - var albumartist = album.Artist.Value; - var artist = track.ArtistMetadata.Value; - - string imageFile = null; - long imageSize = 0; - - if (_configService.EmbedCoverArt) + try { - var cover = album.Images.FirstOrDefault(x => x.CoverType == MediaCoverTypes.Cover); - if (cover != null) + if (trackfile.Tracks?.Value == null || !trackfile.Tracks.Value.Any()) { - imageFile = _mediaCoverService.GetCoverPath(album.Id, MediaCoverEntity.Album, cover.CoverType, cover.Extension, null); - _logger.Trace("Embedding: {0}", imageFile); - var fileInfo = _diskProvider.GetFileInfo(imageFile); - if (fileInfo.Exists) + throw new InvalidOperationException("Unable to write tags: Track information is missing from the database"); + } + + var track = trackfile.Tracks.Value[0]; + + if (track.AlbumRelease?.Value == null) + { + throw new InvalidOperationException("Unable to write tags: Album release information is missing from the database"); + } + + var release = track.AlbumRelease.Value; + + if (release.Album?.Value == null) + { + throw new InvalidOperationException("Unable to write tags: Album information is missing from the database"); + } + + var album = release.Album.Value; + + if (album.Artist?.Value == null) + { + throw new InvalidOperationException("Unable to write tags: Artist information is missing from the database"); + } + + var albumartist = album.Artist.Value; + + if (track.ArtistMetadata?.Value == null) + { + throw new InvalidOperationException("Unable to write tags: Artist metadata is missing from the database"); + } + + var artist = track.ArtistMetadata.Value; + + string imageFile = null; + long imageSize = 0; + + if (_configService.EmbedCoverArt) + { + var cover = album.Images.FirstOrDefault(x => x.CoverType == MediaCoverTypes.Cover); + if (cover != null) { - imageSize = fileInfo.Length; - } - else - { - imageFile = null; + imageFile = _mediaCoverService.GetCoverPath(album.Id, MediaCoverEntity.Album, cover.CoverType, cover.Extension, null); + _logger.Trace("Embedding: {0}", imageFile); + var fileInfo = _diskProvider.GetFileInfo(imageFile); + if (fileInfo.Exists) + { + imageSize = fileInfo.Length; + } + else + { + imageFile = null; + } } } + + return new AudioTag + { + Title = track.Title, + Performers = new[] { artist.Name }, + AlbumArtists = new[] { albumartist.Name }, + Track = (uint)track.AbsoluteTrackNumber, + TrackCount = (uint)release.Tracks.Value.Count(x => x.MediumNumber == track.MediumNumber), + Album = album.Title, + Disc = (uint)track.MediumNumber, + DiscCount = (uint)release.Media.Count, + + // We may have omitted media so index in the list isn't the same as medium number + Media = release.Media.SingleOrDefault(x => x.Number == track.MediumNumber)?.Format, + Date = release.ReleaseDate, + Year = (uint)(album.ReleaseDate?.Year ?? 0), + OriginalReleaseDate = album.ReleaseDate, + OriginalYear = (uint)(album.ReleaseDate?.Year ?? 0), + Publisher = release.Label.FirstOrDefault(), + Genres = album.Genres.Any() ? album.Genres.ToArray() : artist.Genres.ToArray(), + ImageFile = imageFile, + ImageSize = imageSize, + MusicBrainzReleaseCountry = IsoCountries.Find(release.Country.FirstOrDefault())?.TwoLetterCode, + MusicBrainzReleaseStatus = release.Status.ToLower(), + MusicBrainzReleaseType = album.AlbumType.ToLower(), + MusicBrainzReleaseId = release.ForeignReleaseId, + MusicBrainzArtistId = artist.ForeignArtistId, + MusicBrainzReleaseArtistId = albumartist.ForeignArtistId, + MusicBrainzReleaseGroupId = album.ForeignAlbumId, + MusicBrainzTrackId = track.ForeignRecordingId, + MusicBrainzReleaseTrackId = track.ForeignTrackId, + MusicBrainzAlbumComment = album.Disambiguation, + }; } - - return new AudioTag + catch (Exception ex) { - Title = track.Title, - Performers = new[] { artist.Name }, - AlbumArtists = new[] { albumartist.Name }, - Track = (uint)track.AbsoluteTrackNumber, - TrackCount = (uint)release.Tracks.Value.Count(x => x.MediumNumber == track.MediumNumber), - Album = album.Title, - Disc = (uint)track.MediumNumber, - DiscCount = (uint)release.Media.Count, - - // We may have omitted media so index in the list isn't the same as medium number - Media = release.Media.SingleOrDefault(x => x.Number == track.MediumNumber)?.Format, - Date = release.ReleaseDate, - Year = (uint)(album.ReleaseDate?.Year ?? 0), - OriginalReleaseDate = album.ReleaseDate, - OriginalYear = (uint)(album.ReleaseDate?.Year ?? 0), - Publisher = release.Label.FirstOrDefault(), - Genres = album.Genres.Any() ? album.Genres.ToArray() : artist.Genres.ToArray(), - ImageFile = imageFile, - ImageSize = imageSize, - MusicBrainzReleaseCountry = IsoCountries.Find(release.Country.FirstOrDefault())?.TwoLetterCode, - MusicBrainzReleaseStatus = release.Status.ToLower(), - MusicBrainzReleaseType = album.AlbumType.ToLower(), - MusicBrainzReleaseId = release.ForeignReleaseId, - MusicBrainzArtistId = artist.ForeignArtistId, - MusicBrainzReleaseArtistId = albumartist.ForeignArtistId, - MusicBrainzReleaseGroupId = album.ForeignAlbumId, - MusicBrainzTrackId = track.ForeignRecordingId, - MusicBrainzReleaseTrackId = track.ForeignTrackId, - MusicBrainzAlbumComment = album.Disambiguation, - }; + _logger.Error(ex, "Failed to get track metadata for {0}", trackfile.Path); + throw; + } } private void UpdateTrackfileSizeAndModified(TrackFile trackfile, string path) diff --git a/src/NzbDrone.Core/MediaFiles/TrackImport/ImportApprovedTracks.cs b/src/NzbDrone.Core/MediaFiles/TrackImport/ImportApprovedTracks.cs index 1c5f46343..37128cee8 100644 --- a/src/NzbDrone.Core/MediaFiles/TrackImport/ImportApprovedTracks.cs +++ b/src/NzbDrone.Core/MediaFiles/TrackImport/ImportApprovedTracks.cs @@ -250,7 +250,18 @@ public List Import(List> decisions, boo _mediaFileService.Delete(previousFile, DeleteMediaFileReason.ManualOverride); } - _audioTagService.WriteTags(trackFile, false); + try + { + _audioTagService.WriteTags(trackFile, false); + } + catch (InvalidOperationException ex) + { + _logger.Warn(ex, "Failed to write tags for {0}: {1}. Try refreshing the artist to fix missing information.", trackFile.Path, ex.Message); + } + catch (Exception ex) + { + _logger.Error(ex, "Unexpected error writing tags for existing track file {0}", trackFile.Path); + } } filesToAdd.Add(trackFile);