/// <summary>
        /// Create request to TinyPNG service and get response
        /// </summary>
        /// <param name="imagesList">Images that needs to be tinifing</param>
        /// <param name="sourceType">Folder or Image</param>
        /// <returns>Response(StatusCode, message)</returns>
        private async Task <HttpResponseMessage> CallTinyPngService(IEnumerable <TImage> imagesList, SourceTypes sourceType = SourceTypes.Folder)
        {
            var nonOptimizedImagesCount = 0;
            var fs = FileSystemProviderManager.Current.GetFileSystemProvider <MediaFileSystem>();

            foreach (TImage tImage in imagesList)
            {
                var tinyResponse = await _tinyPngConnectorService.TinifyAsync(tImage, fs);

                if (tinyResponse.Output.Url == null)
                {
                    _historyService.CreateResponseHistory(tImage.Id, tinyResponse);
                    _stateService.UpdateState();
                    nonOptimizedImagesCount++;
                    continue;
                }

                _imageService.UpdateImageAfterSuccessfullRequest(tinyResponse, tImage);
            }

            int n = imagesList.Count();
            int k = n - nonOptimizedImagesCount;

            return(GetSuccessResponse(k, n,
                                      nonOptimizedImagesCount == 0 ? EventMessageType.Success : EventMessageType.Warning));
        }
        /// <summary>
        /// Create request to TinyPNG service and get response
        /// </summary>
        /// <param name="imagesList">Images that needs to be tinifing</param>
        /// <returns>Response(StatusCode, message)</returns>
        private async Task <HttpResponseMessage> CallTinyPngService(IEnumerable <TImage> imagesList)
        {
            var nonOptimizedImagesCount = 0;
            var userDomain = HttpContext.Current.Request.Url.Host;

            foreach (var tImage in imagesList)
            {
                var tinyResponse = await _tinyPngConnectorService.TinifyAsync(tImage, _fileSystem);

                if (tinyResponse.Output.Url == null)
                {
                    _historyService.CreateResponseHistory(tImage.Id, tinyResponse);
                    _stateService.UpdateState();
                    nonOptimizedImagesCount++;
                    continue;
                }

                _imageService.UpdateImageAfterSuccessfullRequest(tinyResponse, tImage, _fileSystem);

                try
                {
                    //     HostingEnvironment.QueueBackgroundWorkItem(stat => _backendDevsConnectorService.SendStatistic(userDomain));
                }
                catch (NotSuccessfullRequestException)
                {
                }
            }

            var n = imagesList.Count();
            var k = n - nonOptimizedImagesCount;

            return(GetSuccessResponse(k, n,
                                      nonOptimizedImagesCount == 0 ? EventMessageType.Success : EventMessageType.Warning));
        }
Esempio n. 3
0
        public async Task OptimizeImageAsync(TImage image)
        {
            _stateService.CreateState(1);
            var tinyResponse = await _tinyPngConnectorService.TinifyAsync(image, base.FileSystem).ConfigureAwait(false);

            if (tinyResponse.Output.Url == null)
            {
                _historyService.CreateResponseHistory(image.Id, tinyResponse);
                return;
            }
            UpdateImageAfterSuccessfullRequest(tinyResponse, image, base.FileSystem);
            SendStatistic();
        }