Exemplo n.º 1
0
        private void SetTag(MetaDataItem metaDataItem, File file, Tag tag)
        {
            switch (metaDataItem.Name)
            {
            case CommonMetaData.Album:
                tag.Album = metaDataItem.Value;
                break;

            case CommonMetaData.Artist:
                tag.AlbumArtists = new[] { metaDataItem.Value };
                break;

            case CommonMetaData.Composer:
                tag.Composers = new[] { metaDataItem.Value };
                break;

            case CommonMetaData.Conductor:
                tag.Conductor = metaDataItem.Value;
                break;

            case CommonMetaData.Disc:
                tag.Disc = Convert.ToUInt32(metaDataItem.Value);
                break;

            case CommonMetaData.DiscCount:
                tag.DiscCount = Convert.ToUInt32(metaDataItem.Value);
                break;

            case CommonMetaData.Genre:
                tag.Genres = new[] { metaDataItem.Value };
                break;

            case CommonMetaData.Performer:
                tag.Performers = new[] { metaDataItem.Value };
                break;

            case CommonMetaData.PlayCount:
            case CommonMetaData.Rating:
                PopularimeterManager.Write(this, metaDataItem, file);
                break;

            case CommonMetaData.Title:
                tag.Title = metaDataItem.Value;
                break;

            case CommonMetaData.Track:
                tag.Track = Convert.ToUInt32(metaDataItem.Value);
                break;

            case CommonMetaData.TrackCount:
                tag.TrackCount = Convert.ToUInt32(metaDataItem.Value);
                break;

            case CommonMetaData.Year:
                tag.Year = Convert.ToUInt32(metaDataItem.Value);
                break;
            }
        }
Exemplo n.º 2
0
        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);
            }
        }
Exemplo n.º 3
0
        public async Task <IEnumerable <MetaDataItem> > GetMetaData(string fileName)
        {
            if (!this.IsSupported(fileName))
            {
                Logger.Write(this, LogLevel.Warn, "Unsupported file format: {0}", fileName);
                return(Enumerable.Empty <MetaDataItem>());
            }
            var metaData = new List <MetaDataItem>();

            Logger.Write(this, LogLevel.Trace, "Reading meta data for file: {0}", fileName);
            try
            {
                var collect = default(bool);
                var images  = default(bool);
                using (var file = this.Create(fileName))
                {
                    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.EmbeddedImages.Value)
                    {
                        if (file.InvariantStartPosition > MAX_TAG_SIZE)
                        {
                            Logger.Write(this, LogLevel.Warn, "Not importing images from file \"{0}\" due to size: {1} > {2}", file.Name, file.InvariantStartPosition, MAX_TAG_SIZE);
                            collect = true;
                        }
                        else
                        {
                            var pictures = file.Tag.Pictures;
                            if (pictures != null)
                            {
                                images = await this.AddImages(metaData, CommonMetaData.Pictures, file, file.Tag, pictures).ConfigureAwait(false);
                            }
                        }
                    }
                }
                if (collect)
                {
                    //If we encountered a large meta data section (>10MB) then we need to try to reclaim the memory.
                    GC.Collect();
                }
                if (this.LooseImages.Value && !images)
                {
                    await this.AddImages(metaData, fileName).ConfigureAwait(false);
                }
            }
            catch (UnsupportedFormatException)
            {
                Logger.Write(this, LogLevel.Warn, "Unsupported file format: {0}", fileName);
            }
            catch (Exception e)
            {
                Logger.Write(this, LogLevel.Warn, "Failed to read meta data: {0} => {1}", fileName, e.Message);
            }
            return(metaData);
        }
Exemplo n.º 4
0
        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);
        }