Example #1
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);
        }
Example #2
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);
        }