/// <summary>The restore sprited images from cache.</summary>
        /// <param name="mapXmlFile">The map xml file.</param>
        /// <param name="cacheSection">The cache section.</param>
        /// <param name="results">The results.</param>
        /// <param name="destinationDirectory">The destination Directory.</param>
        /// <param name="imageAssembleScanDestinationFile">The image Assemble Scan Destination File.</param>
        /// <returns>The <see cref="bool"/>.</returns>
        private static ImageLog RestoreSpritedImagesFromCache(string mapXmlFile, ICacheSection cacheSection, BlockingCollection <ContentItem> results, string destinationDirectory, string imageAssembleScanDestinationFile)
        {
            // restore log file, is required by next step in applying sprites to the css.
            var spriteLogFileContentItem = cacheSection.GetCachedContentItems(CacheFileCategories.SpriteLogFile).FirstOrDefault();

            if (spriteLogFileContentItem == null)
            {
                return(null);
            }

            if (!string.IsNullOrWhiteSpace(imageAssembleScanDestinationFile))
            {
                var spriteLogFileXmlContentItem = cacheSection.GetCachedContentItems(CacheFileCategories.SpriteLogFileXml).FirstOrDefault();
                if (spriteLogFileXmlContentItem != null)
                {
                    spriteLogFileXmlContentItem.WriteTo(mapXmlFile);
                }
            }

            var imageLog = spriteLogFileContentItem.Content.FromJson <ImageLog>(true);

            // Restore images
            var spritedImageContentItems = cacheSection.GetCachedContentItems(CacheFileCategories.HashedSpriteImage);

            spritedImageContentItems.ForEach(sici => sici.WriteToContentPath(destinationDirectory));
            results.AddRange(spritedImageContentItems);

            return(imageLog);
        }
        private void LoadStudy(StudyTableItem study)
        {
            List <IFrameReference> frames = new List <IFrameReference>();

            var loader = study.Server.GetService <IStudyLoader>();

            loader.Start(new StudyLoaderArgs(study.StudyInstanceUid, study.Server, null));
            Sop sop;

            while ((sop = loader.LoadNextSop()) != null)
            {
                using (sop)
                {
                    if (sop.IsImage)
                    {
                        foreach (Frame frame in ((ImageSop)sop).Frames)
                        {
                            frames.Add(frame.CreateTransientReference());
                        }
                    }
                }
            }

            _framesToRetrieve.AddRange(frames);
            NotifyPropertyChanged("NumberOfRetrieveItems");
        }
        public void BlockingCollectionAddRangeWithCancellationTokenThrowsIfSequenceIsNull()
        {
            BlockingCollection <int> collection = new BlockingCollection <int>(new ConcurrentQueue <int>());
            IEnumerable <int>        sequence   = null;

            Assert.Throws <ArgumentNullException>(() => collection.AddRange(sequence, new CancellationToken()));
        }
        public void BlockingCollectionAddRangeWithCancellationTokenThrowsIfCollectionIsNull()
        {
            BlockingCollection <int> collection = null;
            IEnumerable <int>        sequence   = new List <int> {
                1, 2, 3, 4, 5
            };

            Assert.Throws <ArgumentNullException>(() => collection.AddRange(sequence, new CancellationToken()));
        }
        public void BlockingCollectionAddRangeWithCancellationTokenAddsCollectionsWithNullElements()
        {
            BlockingCollection <string> collection = new BlockingCollection <string>(new ConcurrentQueue <string>());
            IEnumerable <string>        sequence   = new List <string> {
                "Tom", "Dick", "Harry", null
            };

            collection.AddRange(sequence, new CancellationToken());

            Assert.AreEqual(4, collection.Count);
        }
        public void BlockingCollectionAddRangeWithCancellationTokenAddsCollections()
        {
            BlockingCollection <int> collection = new BlockingCollection <int>(new ConcurrentQueue <int>());
            IEnumerable <int>        sequence   = new List <int> {
                1, 2, 3, 4, 5
            };

            collection.AddRange(sequence, new CancellationToken());

            Assert.AreEqual(5, collection.Count);
        }
        private void InitializeInput(bool randommizeOrder = false)
        {
            var chunks = ByteArrayHelper.ArrayToChunks(GenFile.Content, Config.ChunkSize);

            if (randommizeOrder)
            {
                FillQueueInRandomOrder(chunks);
            }
            else
            {
                _processedQueue.AddRange(chunks);
            }
            _processedQueue.CompleteAdding();
        }
        internal MinifyCssResult Process(ContentItem contentItem, FileHasherActivity imageHasher = null)
        {
            if (imageHasher != null)
            {
                this.availableSourceImages = this.context.GetAvailableFiles(this.context.Configuration.SourceDirectory, this.ImageDirectories, this.ImageExtensions, FileTypes.Image);
            }

            var cssContent = contentItem.Content;

            var minifiedContentItems     = new BlockingCollection <ContentItem>();
            var hashedImageContentItems  = new BlockingCollection <ContentItem>();
            var spritedImageContentItems = new BlockingCollection <ContentItem>();
            var mergedResources          = ResourcePivotActivity.GetUsedGroupedResources(cssContent, this.MergedResources);

            var dpiValues = this.Dpi;

            if (dpiValues == null || !dpiValues.Any())
            {
                dpiValues = new HashSet <float>(new[] { 1f });
            }

            var pivots = GetMinifyCssPivots(contentItem, dpiValues, mergedResources, this.DpiResources).ToArray();

            var nonIgnoredPivots = pivots.Where(p => !this.context.TemporaryIgnore(p.NewContentResourcePivotKeys)).ToArray();

            var parsedStylesheetNode = CssParser.Parse(this.context, cssContent, false);

            this.context.ParallelForEach(
                item => new[] { SectionIdParts.MinifyCssActivity },
                nonIgnoredPivots,
                (threadContext, pivot, parallelLoopState) =>
            {
                ContentItem minifiedContentItem = null;
                var resourceContentItem         = ContentItem.FromContent(contentItem.Content, pivot.NewContentResourcePivotKeys);
                var result = threadContext
                             .SectionedAction(SectionIdParts.MinifyCssActivity, SectionIdParts.Process)
                             .MakeCachable(resourceContentItem, this.GetVarBySettings(imageHasher, pivot.NewContentResourcePivotKeys, pivot.MergedResource))
                             .RestoreFromCacheAction(cacheSection =>
                {
                    minifiedContentItem = cacheSection.GetCachedContentItem(CacheFileCategories.MinifiedCssResult, contentItem.RelativeContentPath, contentItem.AbsoluteDiskPath, pivot.NewContentResourcePivotKeys);
                    hashedImageContentItems.AddRange(cacheSection.GetCachedContentItems(CacheFileCategories.HashedImage));
                    spritedImageContentItems.AddRange(cacheSection.GetCachedContentItems(CacheFileCategories.HashedSpriteImage));

                    if (minifiedContentItem == null)
                    {
                        context.Log.Error("Css minify cache result is null");
                        return(false);
                    }

                    if (spritedImageContentItems.Any(hi => hi == null))
                    {
                        context.Log.Error("Sprited image cache result is null");
                        return(false);
                    }

                    if (hashedImageContentItems.Any(hi => hi == null))
                    {
                        context.Log.Error("Hashed image cache result is null");
                        return(false);
                    }

                    return(true);
                })
                             .Execute(cacheSection =>
                {
                    try
                    {
                        // Apply all configured visitors, including, validating, optimizing, minifying and spriting.

                        // Applying of resources
                        var stylesheetNode = ApplyResources(parsedStylesheetNode, pivot.MergedResource, threadContext) as StyleSheetNode;

                        // Validation
                        stylesheetNode = this.ApplyValidation(stylesheetNode, threadContext) as StyleSheetNode;

                        // Optimization
                        stylesheetNode = this.ApplyOptimization(stylesheetNode, threadContext) as StyleSheetNode;

                        // Spriting
                        stylesheetNode = this.ApplySpriting(stylesheetNode, pivot.Dpi, spritedImageContentItems, threadContext) as StyleSheetNode;

                        // Output css as string
                        var processedCssContent = threadContext.SectionedAction(SectionIdParts.MinifyCssActivity, SectionIdParts.PrintCss).Execute(() =>
                                                                                                                                                   this.ShouldMinify ? stylesheetNode.MinifyPrint() : stylesheetNode.PrettyPrint());

                        // TODO: Hash the images on the styielsheetnode not on the css result.
                        // Hash images on the result css
                        if (imageHasher != null)
                        {
                            var hashResult      = HashImages(processedCssContent, imageHasher, cacheSection, threadContext, this.availableSourceImages, this.MissingImageUrl);
                            processedCssContent = hashResult.Item1;
                            hashedImageContentItems.AddRange(hashResult.Item2);
                        }

                        minifiedContentItem = ContentItem.FromContent(processedCssContent, this.DestinationFile, null, pivot.NewContentResourcePivotKeys);
                        cacheSection.AddResult(minifiedContentItem, CacheFileCategories.MinifiedCssResult);
                    }
                    catch (Exception ex)
                    {
                        context.Log.Error(ex, ex.ToString());
                        return(false);
                    }

                    return(true);
                });

                Safe.Lock(minifiedContentItems, () => minifiedContentItems.Add(minifiedContentItem));

                if (!result)
                {
                    context.Log.Error("An errror occurred while minifying '{0}' with resources '{1}'".InvariantFormat(contentItem.RelativeContentPath, pivot));
                }

                return(result);
            });

            return(new MinifyCssResult(
                       minifiedContentItems,
                       spritedImageContentItems.DistinctBy(hi => hi.RelativeContentPath).ToArray(),
                       hashedImageContentItems.DistinctBy(hi => hi.RelativeContentPath).ToArray()));
        }