Example #1
0
        /// <summary>
        /// Extract asynchronously from a FileEntry.
        /// </summary>
        /// <param name="fileEntry">The FileEntry containing the Conteant stream to parse.</param>
        /// <param name="opts">The ExtractorOptions to use</param>
        /// <param name="governor">The Resource governor to use (or null to create a new one).</param>
        /// <returns>The FileEntries found.</returns>
        public async IAsyncEnumerable <FileEntry> ExtractAsync(FileEntry fileEntry, ExtractorOptions?opts = null, ResourceGovernor?governor = null)
        {
            var options = opts ?? new ExtractorOptions();

            var Governor = governor ?? new ResourceGovernor(options);

            if (governor is null)
            {
                Governor.ResetResourceGovernor(fileEntry.Content);
            }
            Logger.Trace("ExtractFile({0})", fileEntry.FullPath);
            Governor.CurrentOperationProcessedBytesLeft -= fileEntry.Content.Length;
            Governor.CheckResourceGovernor();
            var type = MiniMagic.DetectFileType(fileEntry);

            if (type == ArchiveFileType.UNKNOWN || !Extractors.ContainsKey(type))
            {
                yield return(fileEntry);
            }
            else
            {
                Governor.CurrentOperationProcessedBytesLeft += fileEntry.Content.Length;
                await foreach (var result in Extractors[type].ExtractAsync(fileEntry, options, Governor))
                {
                    yield return(result);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Extract from a FileEntry.
        /// </summary>
        /// <param name="fileEntry">The FileEntry containing the Conteant stream to parse.</param>
        /// <param name="opts">The ExtractorOptions to use</param>
        /// <param name="governor">The Resource governor to use (or null to create a new one).</param>
        /// <returns>The FileEntries found.</returns>
        public IEnumerable <FileEntry> Extract(FileEntry fileEntry, ExtractorOptions?opts = null, ResourceGovernor?governor = null)
        {
            var options  = opts ?? new ExtractorOptions();
            var Governor = governor;

            if (Governor == null)
            {
                Governor = new ResourceGovernor(options);
                Governor.ResetResourceGovernor(fileEntry.Content);
            }
            Logger.Trace("ExtractFile({0})", fileEntry.FullPath);
            Governor.CurrentOperationProcessedBytesLeft -= fileEntry.Content.Length;
            Governor.CheckResourceGovernor();
            IEnumerable <FileEntry> result = Array.Empty <FileEntry>();
            var useRaw = false;

            try
            {
                var type = MiniMagic.DetectFileType(fileEntry);
                if (type == ArchiveFileType.UNKNOWN || !Extractors.ContainsKey(type))
                {
                    useRaw = true;
                    result = new[]
                    {
                        fileEntry
                    };
                }
                else
                {
                    result = Extractors[type].Extract(fileEntry, options, Governor);
                }
            }
            catch (Exception ex)
            {
                Logger.Debug(ex, "Error extracting {0}: {1}", fileEntry.FullPath, ex.Message);
                useRaw = true;

                result = new[] {
                    fileEntry
                };
            }

            // After we are done with an archive subtract its bytes. Contents have been counted now separately
            if (!useRaw)
            {
                Governor.CurrentOperationProcessedBytesLeft += fileEntry.Content.Length;
            }

            return(result);
        }