Ejemplo n.º 1
0
        public ImageOptimizationResponse ProcessImage(ImageOptimizationRequest imageOptimizationRequest)
        {
            if (imageOptimizationRequest == null)
            {
                return new ImageOptimizationResponse();
            }

            var smushItRequest = new SmushItRequest
                                     {
                                         ImageUrl = imageOptimizationRequest.ImageUrl
                                     };
            SmushItResponse smushItResponse = this._smushItProxy.ProcessImage(smushItRequest);

            return smushItResponse.ConvertToResponse();
        }
Ejemplo n.º 2
0
        public override string Execute()
        {
            int count = 0;
            long totalBytesBefore = 0;
            long totalBytesAfter = 0;

            var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
            var blobFactory = ServiceLocator.Current.GetInstance<BlobFactory>();

            IEnumerable<ImageData> allImages = GetFolders(contentRepository).SelectMany(a => GetImageFiles(a)).Distinct();

            if (_stop)
            {
                return string.Format("Job stopped after optimizing {0} images.", count);
            }

            if (!ImageOptimizationSettings.Instance.BypassPreviouslyOptimized)
            {
                allImages = FilterPreviouslyOptimizedImages(allImages);
            }

            foreach (ImageData image in allImages)
            {
                if (_stop)
                {
                    return string.Format("Job completed after optimizing: {0} images. Before: {1} KB, after: {2} KB.", count, totalBytesBefore / 1024, totalBytesAfter / 1024);
                }

                if (!PublishedStateAssessor.IsPublished(image) || image.IsDeleted)
                {
                    continue;
                }

                var imageOptimizationRequest = new ImageOptimizationRequest
                                                   {
                                                       ImageUrl = image.ContentLink.GetFriendlyUrl()
                                                   };
                ImageOptimizationResponse imageOptimizationResponse = this._imageOptimization.ProcessImage(imageOptimizationRequest);

                Identity logEntryId = this.AddLogEntry(imageOptimizationResponse, image);

                if (imageOptimizationResponse.Successful)
                {
                    totalBytesBefore += imageOptimizationResponse.OriginalImageSize;

                    if (imageOptimizationResponse.OptimizedImageSize > 0)
                    {
                        totalBytesAfter += imageOptimizationResponse.OptimizedImageSize;
                    }
                    else
                    {
                        totalBytesAfter += imageOptimizationResponse.OriginalImageSize;
                    }

                    var file = image.CreateWritableClone() as ImageData;

                    byte[] fileContent = imageOptimizationResponse.OptimizedImage;

                    var blob = blobFactory.CreateBlob(file.BinaryDataContainer, MimeTypeHelper.GetDefaultExtension(file.MimeType));

                    blob.Write(new MemoryStream(fileContent));

                    file.BinaryData = blob;

                    contentRepository.Save(file, SaveAction.Publish, AccessLevel.NoAccess);

                    this.UpdateLogEntryToOptimized(logEntryId);

                    count++;
                }
                else
                {
                    _logger.Error("ErrorMessage from SmushItProxy: " + imageOptimizationResponse.ErrorMessage);
                }
            }

            return string.Format("Job completed after optimizing: {0} images. Before: {1} KB, after: {2} KB.", count, totalBytesBefore / 1024, totalBytesAfter / 1024);
        }
Ejemplo n.º 3
0
        public override string Execute()
        {
            int count = 0;
            int totalBytesBefore = 0;
            int totalBytesAfter = 0;
            string siteUrl = UriSupport.SiteUrl.ToString();

            var allProviders = VirtualPathHandler.Instance.VirtualPathProviders.Where(p => p.Key.GetType() == typeof(VirtualPathVersioningProvider));

            try
            {
                if (!string.IsNullOrEmpty(ImageOptimizationSettings.Settings.SiteUrl))
                {
                    siteUrl = ImageOptimizationSettings.Settings.SiteUrl;
                }

                if (!string.IsNullOrEmpty(ImageOptimizationSettings.Settings.VirtualNames))
                {
                    allProviders = FilteredVirtualPathProviders(allProviders);
                }
            }
            catch
            {
                // no configuration could be loaded
            }

            foreach (KeyValuePair<VirtualPathProvider, ProviderSettings> vpp in allProviders)
            {
                if (_stop)
                {
                    return string.Format("Job stopped after optimizing {0} images.", count);
                }

                var rootFolder = VirtualPathHandler.Instance.GetDirectory(vpp.Value.Parameters["virtualPath"], true) as UnifiedDirectory;

                var images = new HashSet<string>();

                GetImages(images, rootFolder);

                // remove previously optimized/checked images
                images = new HashSet<string>(images.Where(virtualPath => this._imageLogRepository.GetLogEntry(VirtualPathUtility.RemoveTrailingSlash(siteUrl) + virtualPath) == null));

                foreach (string virtualPath in images)
                {
                    if (_stop)
                    {
                        return string.Format("Job completed after optimizing: {0} images. Before: {1} KB, after: {2} KB.", count, totalBytesBefore / 1024, totalBytesAfter / 1024);
                    }

                    var imageOptimizationRequest = new ImageOptimizationRequest
                                                       {
                                                           ImageUrl = VirtualPathUtility.RemoveTrailingSlash(siteUrl) + virtualPath
                                                       };

                    ImageOptimizationResponse imageOptimizationResponse = this._imageOptimization.ProcessImage(imageOptimizationRequest);

                    Identity logEntryId = this.AddLogEntry(imageOptimizationResponse, virtualPath);

                    if (imageOptimizationResponse.Successful)
                    {
                        totalBytesBefore += imageOptimizationResponse.OriginalImageSize;

                        if (imageOptimizationResponse.OptimizedImageSize > 0)
                        {
                            totalBytesAfter += imageOptimizationResponse.OptimizedImageSize;
                        }
                        else
                        {
                            totalBytesAfter += imageOptimizationResponse.OriginalImageSize;
                        }

                        var file = HostingEnvironment.VirtualPathProvider.GetFile(virtualPath) as UnifiedFile;

                        if (file == null)
                        {
                            continue;
                        }

                        IVersioningFile versioningFile;
                        byte[] fileContent = imageOptimizationResponse.OptimizedImage;

                        if (file.TryAsVersioningFile(out versioningFile))
                        {
                            if (!versioningFile.IsCheckedOut)
                            {
                                versioningFile.CheckOut();
                            }

                            using (Stream stream = file.Open(FileMode.Create, FileAccess.Write))
                            {
                                stream.Write(fileContent, 0, fileContent.Length);
                            }

                            versioningFile.CheckIn(string.Format("Optimized image with Smush.it. From: {0} KB to: {1} KB. Saved: {2}%", imageOptimizationResponse.OriginalImageSize/1024, imageOptimizationResponse.OptimizedImageSize/1024, imageOptimizationResponse.PercentSaved));

                            this.UpdateLogEntryToOptimized(logEntryId);
                        }

                        count++;
                    }
                }
            }

            return string.Format("Job completed after optimizing: {0} images. Before: {1} KB, after: {2} KB.", count, totalBytesBefore/1024, totalBytesAfter/1024);
        }