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 <IEnumerable <MetaDataItem> > GetMetaData(string fileName, Func <File> factory) { if (!this.IsSupported(fileName)) { Logger.Write(this, LogLevel.Warn, "Unsupported file format: {0}", fileName); this.AddWarning(fileName, "Unsupported file format."); return(Enumerable.Empty <MetaDataItem>()); } var collect = default(bool); var metaData = new List <MetaDataItem>(); Logger.Write(this, LogLevel.Trace, "Reading meta data for file: {0}", fileName); try { using (var file = factory()) { if (file.PossiblyCorrupt) { this.AddWarnings(fileName, file.CorruptionReasons); } if (file.InvariantStartPosition > MAX_TAG_SIZE) { collect = true; } if (file.Tag != null) { this.AddTags(metaData, file.Tag); } if (file.Properties != null) { this.AddProperties(metaData, file.Properties); } if (this.Popularimeter.Value) { this.Try(() => PopularimeterManager.Read(this, metaData, file), this.ErrorHandler); } if (this.ReplayGain.Value) { this.Try(() => ReplayGainManager.Read(this, metaData, file), this.ErrorHandler); } if (this.Documents.Value) { this.Try(() => DocumentManager.Read(this, metaData, file), this.ErrorHandler); } this.Try(() => CompilationManager.Read(this, metaData, file), this.ErrorHandler); if (file is IMetaDataSource metaDataSource) { await this.AddAdditional(metaData, file, metaDataSource).ConfigureAwait(false); } await ImageManager.Read(this, metaData, file).ConfigureAwait(false); } } catch (UnsupportedFormatException) { Logger.Write(this, LogLevel.Warn, "Unsupported file format: {0}", fileName); this.AddWarning(fileName, "Unsupported file format."); } catch (OutOfMemoryException) { //This can happen with really big embedded images. //It's tricky to avoid because we can't check InvariantStartPosition without parsing. Logger.Write(this, LogLevel.Warn, "Out of memory: {0}", fileName); this.AddWarning(fileName, "Out of memory."); collect = true; } catch (Exception e) { Logger.Write(this, LogLevel.Warn, "Failed to read meta data: {0} => {1}", fileName, e.Message); this.AddWarning(fileName, string.Format("Failed to read meta data: {0}", e.Message)); } finally { if (collect) { //If we encountered a large meta data section (>10MB) then we need to try to reclaim the memory. GC.Collect(); } } return(metaData); }