protected override async Task OnRun() { if (MetaDataBehaviourConfiguration.GetWriteBehaviour(this.Write.Value) == WriteBehaviour.None) { Logger.Write(this, LogLevel.Debug, "Meta data writing is disabled."); return; } foreach (var playlistItem in this.PlaylistItems) { if (this.Output.IsLoaded(playlistItem.FileName)) { Logger.Write(this, LogLevel.Debug, "File \"{0}\" could not be written, the update will be retried: The file is in use.", playlistItem.FileName); this.AddError(playlistItem, string.Format("File \"{0}\" could not be written, the update will be retried: The file is in use.", playlistItem.FileName)); await this.Schedule(playlistItem).ConfigureAwait(false); continue; } try { await this.Synchronize(playlistItem).ConfigureAwait(false); } catch (IOException e) { Logger.Write(this, LogLevel.Debug, "File \"{0}\" could not be written, the update will be retried: {1}", playlistItem.FileName, e.Message); this.AddError(playlistItem, string.Format("File \"{0}\" could not be written, the update will be retried: {1}", playlistItem.FileName, e.Message)); await this.Schedule(playlistItem).ConfigureAwait(false); } catch (Exception e) { Logger.Write(this, LogLevel.Debug, "File \"{0}\" could not be written: {1}", playlistItem.FileName, e.Message); this.AddError(playlistItem, string.Format("File \"{0}\" could not be written: {1}", playlistItem.FileName, e.Message)); } } }
private void Refresh() { if (this.MetaDataEnabled.Value && this.BackgroundEnabled.Value && MetaDataBehaviourConfiguration.GetWriteBehaviour(this.Write.Value) != WriteBehaviour.None) { this.Enable(); } else { this.Disable(); } }
protected override async Task OnRun() { lock (this.PlaylistItem.MetaDatas) { var metaDatas = this.PlaylistItem.MetaDatas.ToDictionary( metaDataItem => metaDataItem.Name, StringComparer.OrdinalIgnoreCase ); this.UpdatePlayCount(metaDatas); this.UpdateLastPlayed(metaDatas); } var write = MetaDataBehaviourConfiguration.GetWriteBehaviour( this.Write.Value ).HasFlag(WriteBehaviour.Statistics); await this.MetaDataManager.Save( new[] { this.PlaylistItem }, write, false, CommonStatistics.PlayCount, CommonStatistics.LastPlayed ).ConfigureAwait(false); }
private void SetTag(MetaDataItem metaDataItem, File file) { var tag = file.Tag; if (string.Equals(metaDataItem.Name, CommonMetaData.Album, StringComparison.OrdinalIgnoreCase)) { this.Try(() => tag.Album = metaDataItem.Value, this.ErrorHandler); } else if (string.Equals(metaDataItem.Name, CommonMetaData.Artist, StringComparison.OrdinalIgnoreCase)) { this.Try(() => tag.AlbumArtists = new[] { metaDataItem.Value }, this.ErrorHandler); } else if (string.Equals(metaDataItem.Name, CommonMetaData.Composer, StringComparison.OrdinalIgnoreCase)) { this.Try(() => tag.Composers = new[] { metaDataItem.Value }, this.ErrorHandler); } else if (string.Equals(metaDataItem.Name, CommonMetaData.Conductor, StringComparison.OrdinalIgnoreCase)) { this.Try(() => tag.Conductor = metaDataItem.Value, this.ErrorHandler); } else if (string.Equals(metaDataItem.Name, CommonMetaData.Disc, StringComparison.OrdinalIgnoreCase)) { this.Try(() => { var disc = default(uint); if (uint.TryParse(metaDataItem.Value, out disc)) { tag.Disc = disc; } else { tag.Disc = 0; } }, this.ErrorHandler); } else if (string.Equals(metaDataItem.Name, CommonMetaData.DiscCount, StringComparison.OrdinalIgnoreCase)) { this.Try(() => { var discCount = default(uint); if (uint.TryParse(metaDataItem.Value, out discCount)) { tag.DiscCount = discCount; } else { tag.DiscCount = 0; } }, this.ErrorHandler); } else if (string.Equals(metaDataItem.Name, CommonMetaData.Genre, StringComparison.OrdinalIgnoreCase)) { this.Try(() => tag.Genres = new[] { metaDataItem.Value }, this.ErrorHandler); } else if (string.Equals(metaDataItem.Name, CommonStatistics.LastPlayed, StringComparison.OrdinalIgnoreCase)) { if (this.Popularimeter.Value && MetaDataBehaviourConfiguration.GetWriteBehaviour(this.Write.Value).HasFlag(WriteBehaviour.Statistics)) { this.Try(() => PopularimeterManager.Write(this, metaDataItem, file), this.ErrorHandler); } } else if (string.Equals(metaDataItem.Name, CommonMetaData.Performer, StringComparison.OrdinalIgnoreCase)) { this.Try(() => tag.Performers = new[] { metaDataItem.Value }, this.ErrorHandler); } else if (string.Equals(metaDataItem.Name, CommonStatistics.PlayCount, StringComparison.OrdinalIgnoreCase)) { if (this.Popularimeter.Value && MetaDataBehaviourConfiguration.GetWriteBehaviour(this.Write.Value).HasFlag(WriteBehaviour.Statistics)) { this.Try(() => PopularimeterManager.Write(this, metaDataItem, file), this.ErrorHandler); } } else if (string.Equals(metaDataItem.Name, CommonStatistics.Rating, StringComparison.OrdinalIgnoreCase)) { if (this.Popularimeter.Value) { this.Try(() => PopularimeterManager.Write(this, metaDataItem, file), this.ErrorHandler); } } else if (string.Equals(metaDataItem.Name, CommonMetaData.ReplayGainAlbumGain, StringComparison.OrdinalIgnoreCase) || string.Equals(metaDataItem.Name, CommonMetaData.ReplayGainAlbumPeak, StringComparison.OrdinalIgnoreCase) || string.Equals(metaDataItem.Name, CommonMetaData.ReplayGainTrackGain, StringComparison.OrdinalIgnoreCase) || string.Equals(metaDataItem.Name, CommonMetaData.ReplayGainTrackPeak, StringComparison.OrdinalIgnoreCase)) { if (this.ReplayGain.Value) { this.Try(() => ReplayGainManager.Write(this, metaDataItem, file), this.ErrorHandler); } } else if (string.Equals(metaDataItem.Name, CommonMetaData.Title, StringComparison.OrdinalIgnoreCase)) { this.Try(() => tag.Title = metaDataItem.Value, this.ErrorHandler); } else if (string.Equals(metaDataItem.Name, CommonMetaData.Track, StringComparison.OrdinalIgnoreCase)) { this.Try(() => { var track = default(uint); if (uint.TryParse(metaDataItem.Value, out track)) { tag.Track = track; } else { tag.Track = 0; } }, this.ErrorHandler); } else if (string.Equals(metaDataItem.Name, CommonMetaData.TrackCount, StringComparison.OrdinalIgnoreCase)) { this.Try(() => { var trackCount = default(uint); if (uint.TryParse(metaDataItem.Value, out trackCount)) { tag.TrackCount = trackCount; } else { tag.TrackCount = 0; } }, this.ErrorHandler); } else if (string.Equals(metaDataItem.Name, CommonMetaData.Year, StringComparison.OrdinalIgnoreCase)) { this.Try(() => { var year = default(uint); if (uint.TryParse(metaDataItem.Value, out year)) { tag.Year = year; } else { tag.Year = 0; } }, this.ErrorHandler); } else if (string.Equals(metaDataItem.Name, CommonMetaData.IsCompilation, StringComparison.OrdinalIgnoreCase)) { this.Try(() => CompilationManager.Write(this, metaDataItem, file), this.ErrorHandler); } }
public async Task SetMetaData(string fileName, IEnumerable <MetaDataItem> metaData, Func <MetaDataItem, bool> predicate) { if (MetaDataBehaviourConfiguration.GetWriteBehaviour(this.Write.Value) == WriteBehaviour.None) { Logger.Write(this, LogLevel.Warn, "Writing is disabled: {0}", fileName); return; } if (!this.IsSupported(fileName)) { Logger.Write(this, LogLevel.Warn, "Unsupported file format: {0}", fileName); return; } var metaDataItems = default(IEnumerable <MetaDataItem>); lock (metaData) { if (predicate != null) { metaDataItems = metaData.Where(predicate).ToArray(); } else { metaDataItems = metaData.ToArray(); } if (!metaDataItems.Any()) { //Nothing to update. return; } } var collect = default(bool); using (var file = this.FileFactory.Create(fileName)) { if (file.PossiblyCorrupt) { this.AddWarnings(fileName, file.CorruptionReasons); } foreach (var metaDataItem in metaDataItems) { switch (metaDataItem.Type) { case MetaDataItemType.Tag: this.SetTag(metaDataItem, file); break; case MetaDataItemType.Image: if (file.InvariantStartPosition > MAX_TAG_SIZE) { collect = true; } await ImageManager.Write(this, metaDataItem, file).ConfigureAwait(false); break; case MetaDataItemType.Document: if (this.Documents.Value) { DocumentManager.Write(this, metaDataItem, file); } break; } } if (file is IMetaDataSource metaDataSource) { await this.SetAdditional(metaData, file, metaDataSource).ConfigureAwait(false); } file.Save(); } if (collect) { //If we encountered a large meta data section (>10MB) then we need to try to reclaim the memory. GC.Collect(); } }