Beispiel #1
0
        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);
        }
Beispiel #2
0
        public async Task UpdateAlbumAsync(Album album, MetadataArtworkValue artwork, bool updateFileArtwork)
        {
            string artworkID = String.Empty;

            // Cache new artwork
            if (artwork != null)
            {
                await Task.Run(() =>
                {
                    try
                    {
                        artworkID = IndexerUtils.CacheArtworkData(artwork.DataValue);
                    }
                    catch (Exception ex)
                    {
                        LogClient.Instance.Logger.Error("An error occured while caching artwork data for album with title='{0}' and album artist='{1}'. Exception: {2}", album.AlbumTitle, album.AlbumArtist, ex.Message);
                    }
                });
            }

            // Update artwork in database
            await this.albumRepository.UpdateAlbumArtworkAsync(album.AlbumTitle, album.AlbumArtist, artworkID);

            if (updateFileArtwork)
            {
                List <TrackInfo> albumTracks = await this.trackRepository.GetTracksAsync(album.ToList());

                List <FileMetadata> fileMetadatas = (from t in albumTracks select new FileMetadata(t.Path)
                {
                    ArtworkData = artwork
                }).ToList();

                // Queue update of the file metadata
                await this.QueueFileMetadata(fileMetadatas);
            }

            // Raise event
            var metadataChangedEventArgs = new MetadataChangedEventArgs();

            metadataChangedEventArgs.IsAlbumArtworkMetadataChanged = true;
            this.MetadataChanged(metadataChangedEventArgs);
        }