private async Task <AlbumMetadataChangeStatus> UpdateDatabaseAlbumMetadataAsync(FileMetadata fileMetadata, bool updateAlbumArtwork) { var albumMetadataChangeStatus = new AlbumMetadataChangeStatus(); Track dbTrack = await this.trackRepository.GetTrackAsync(fileMetadata.FileName); if (fileMetadata.Album.IsValueChanged | fileMetadata.AlbumArtists.IsValueChanged | fileMetadata.Year.IsValueChanged) { albumMetadataChangeStatus.IsAlbumTitleChanged = fileMetadata.Album.IsValueChanged; albumMetadataChangeStatus.IsAlbumArtistChanged = fileMetadata.AlbumArtists.IsValueChanged; albumMetadataChangeStatus.IsAlbumYearChanged = fileMetadata.Year.IsValueChanged; Album dbAlbum = null; string newAlbumTitle = !string.IsNullOrWhiteSpace(fileMetadata.Album.Value) ? fileMetadata.Album.Value : Defaults.UnknownAlbumString; string newAlbumArtist = fileMetadata.AlbumArtists.Values != null && !string.IsNullOrEmpty(fileMetadata.AlbumArtists.Values.FirstOrDefault()) ? fileMetadata.AlbumArtists.Values.FirstOrDefault() : Defaults.UnknownAlbumArtistString; dbAlbum = await this.albumRepository.GetAlbumAsync(newAlbumTitle, newAlbumArtist); if (dbAlbum == null) { dbAlbum = new Album { AlbumTitle = newAlbumTitle, AlbumArtist = newAlbumArtist }; await Task.Run(() => IndexerUtils.CacheArtwork(dbAlbum, dbTrack.Path)); // Artwork dbAlbum = await this.albumRepository.AddAlbumAsync(dbAlbum); } dbTrack.AlbumID = dbAlbum.AlbumID; await this.trackRepository.UpdateTrackAsync(dbTrack); await Task.Run(() => IndexerUtils.UpdateAlbumYear(dbAlbum, fileMetadata.Year.Value.SafeConvertToLong())); // Update the album's year await this.albumRepository.UpdateAlbumAsync(dbAlbum); } if (updateAlbumArtwork) { albumMetadataChangeStatus.IsAlbumArtworkChanged = true; string artworkID = String.Empty; if (fileMetadata.ArtworkData.DataValue != null) { await Task.Run(() => artworkID = IndexerUtils.CacheArtworkData(fileMetadata.ArtworkData.DataValue)); } await this.albumRepository.UpdateAlbumArtworkAsync(!string.IsNullOrWhiteSpace(fileMetadata.Album.Value)?fileMetadata.Album.Value : Defaults.UnknownAlbumString, fileMetadata.AlbumArtists.Values == null && !string.IsNullOrEmpty(fileMetadata.AlbumArtists.Values.FirstOrDefault())?fileMetadata.AlbumArtists.Values.FirstOrDefault() : Defaults.UnknownAlbumArtistString, artworkID); } return(albumMetadataChangeStatus); }
private async Task UpdateDatabaseMetadataAsync(List <FileMetadata> fileMetadatas, bool updateAlbumArtwork) { this.isUpdatingDatabaseMetadata = true; var metadataChangedEventArgs = new MetadataChangedEventArgs(); foreach (FileMetadata fmd in fileMetadatas) { try { metadataChangedEventArgs.IsTrackMetadataChanged = await this.UpdateDatabaseTrackMetadataAsync(fmd); metadataChangedEventArgs.IsArtistMetadataChanged = await this.UpdateDatabaseArtistMetadataAsync(fmd); metadataChangedEventArgs.IsGenreMetadataChanged = await this.UpdateDatabaseGenreMetadataAsync(fmd); AlbumMetadataChangeStatus albumMetadataChangeStatus = await this.UpdateDatabaseAlbumMetadataAsync(fmd, updateAlbumArtwork); metadataChangedEventArgs.IsAlbumTitleMetadataChanged = albumMetadataChangeStatus.IsAlbumTitleChanged; metadataChangedEventArgs.IsAlbumArtistMetadataChanged = albumMetadataChangeStatus.IsAlbumArtistChanged; metadataChangedEventArgs.IsAlbumYearMetadataChanged = albumMetadataChangeStatus.IsAlbumYearChanged; metadataChangedEventArgs.IsAlbumArtworkMetadataChanged = albumMetadataChangeStatus.IsAlbumArtworkChanged; } catch (Exception ex) { LogClient.Instance.Logger.Error("Unable to update database metadata for Track '{0}'. Exception: {1}", fmd.FileName, ex.Message); } } if (metadataChangedEventArgs.IsMetadataChanged) { try { await this.albumRepository.DeleteOrphanedAlbumsAsync(); await this.genreRepository.DeleteOrphanedGenresAsync(); await this.artistRepository.DeleteOrphanedArtistsAsync(); } catch (Exception ex) { LogClient.Instance.Logger.Error("Error while deleting orphans. Exception: {0}", ex.Message); } this.MetadataChanged(metadataChangedEventArgs); } this.isUpdatingDatabaseMetadata = false; }