Fixed: Improve Error Handling for file metadata tagging

This commit is contained in:
bakerboy448 2025-09-09 10:57:02 -05:00
parent 80cf89c86d
commit 323b366ac6
2 changed files with 102 additions and 54 deletions

View file

@ -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)

View file

@ -250,7 +250,18 @@ public List<ImportResult> Import(List<ImportDecision<LocalTrack>> 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);