public async Task <TinyResponse> TinifyJsonObject(string imageUrl) { TinyResponse tinyResponse; var source = new TinyJsonObject { Source = new Source { Url = imageUrl } }; var content = new StringContent(_serializer.Serialize(source).ToLower(), Encoding.UTF8, "application/json"); try { var responseResult = await CreateRequest(content); tinyResponse = _serializer.Deserialize <TinyResponse>(responseResult); tinyResponse.Output.IsOptimized = true; } catch (HttpRequestException ex) { tinyResponse = new TinyResponse { Output = new TinyOutput { Error = ex.Message, IsOptimized = false } }; } return(tinyResponse); }
public void UpdateImageAfterSuccessfullRequest(TinyResponse tinyResponse, TImage image, IFileSystem fs) { // download optimized image var tImageBytes = TinyImageService.Instance.DownloadImage(tinyResponse.Output.Url); // preserve image metadata if (_settingsService.GetSettings().PreserveMetadata) { byte[] originImageBytes = image.ToBytes(fs); PreserveImageMetadata(originImageBytes, ref tImageBytes); } // update physical file base.UpdateMedia(image, tImageBytes); // update history _historyService.CreateResponseHistory(image.Id, tinyResponse); // update umbraco media attributes _imageRepository.Update(image.Id, tinyResponse.Output.Size); // update statistic var savedBytes = tinyResponse.Input.Size - tinyResponse.Output.Size; _statisticService.UpdateStatistic(); // update tinifying state _stateService.UpdateState(); }
public void UpdateImageAfterSuccessfullRequest(TinyResponse tinyResponse, TImage image, IFileSystem fs) { int.TryParse(image.Id, out var id); // download optimized image var tImageBytes = _tinyImageService.DownloadImage(tinyResponse.Output.Url); // preserve image metadata if (_settingsService.GetSettings().PreserveMetadata) { var originImageBytes = image.ToBytes(fs); PreserveImageMetadata(originImageBytes, ref tImageBytes); } // httpContext is null when optimization on upload // https://our.umbraco.org/projects/backoffice-extensions/tinifier/bugs/90472-error-systemargumentnullexception-value-cannot-be-null if (HttpContext.Current == null) { HttpContext.Current = new HttpContext(new SimpleWorkerRequest("dummy.aspx", "", new StringWriter())); } // update physical file UpdateMedia(image, tImageBytes); // update history _historyService.CreateResponseHistory(image.Id, tinyResponse); // update umbraco media attributes _imageRepository.Update(id, tinyResponse.Output.Size); // update statistic _statisticService.UpdateStatistic(); // update tinifying state _stateService.UpdateState(); }
public async Task <TinyResponse> TinifyByteArray(byte[] imageByteArray) { TinyResponse tinyResponse; var byteContent = new ByteArrayContent(imageByteArray); try { var responseResult = await CreateRequest(byteContent); tinyResponse = _serializer.Deserialize <TinyResponse>(responseResult); tinyResponse.Output.IsOptimized = true; } catch (HttpRequestException ex) { tinyResponse = new TinyResponse { Output = new TinyOutput { Error = ex.Message, IsOptimized = false } }; } return(tinyResponse); }
public async Task <TinyResponse> TinifyAsync(TImage tImage, IFileSystem fs) { byte[] imageBytes; TinyResponse tinyResponse; try { string path = fs.GetRelativePath(tImage.AbsoluteUrl); using (Stream file = fs.OpenFile(path)) { imageBytes = ReadFully(file); } tinyResponse = await TinifyByteArrayAsync(imageBytes).ConfigureAwait(false); } catch (Exception) { tinyResponse = new TinyResponse { Output = new TinyOutput { Error = PackageConstants.ImageDeleted, IsOptimized = false } }; } return(tinyResponse); }
public async Task <TinyResponse> TinifyAsync(TImage tImage, IFileSystem fs) { TinyResponse tinyResponse; var path = tImage.AbsoluteUrl; int.TryParse(tImage.Id, out var id); var settings = _settingsService.GetSettings(); try { var imageBytes = GetImageBytesFromPath(tImage, fs, path); tinyResponse = await TinifyByteArrayAsync(imageBytes).ConfigureAwait(false); if (id > 0 && settings.EnableUndoOptimization) { _imageHistoryService.Create(tImage, imageBytes); } } catch (Exception) { tinyResponse = new TinyResponse { Output = new TinyOutput { Error = PackageConstants.ImageDeleted, IsOptimized = false } }; } return(tinyResponse); }
public void UpdateImageAfterSuccessfullRequest(TinyResponse tinyResponse, TImage image) { // download optimized image var tImageBytes = TinyImageService.Instance.DownloadImage(tinyResponse.Output.Url); // update physical file base.UpdateMedia(image, tImageBytes); // update history _historyService.CreateResponseHistory(image.Id, tinyResponse); // update umbraco media attributes _imageRepository.Update(image.Id, tinyResponse.Output.Size); // update statistic var savedBytes = tinyResponse.Input.Size - tinyResponse.Output.Size; _statisticService.UpdateStatistic(); // update tinifying state _stateService.UpdateState(); }
public void CreateResponseHistory(string timageId, TinyResponse responseItem) { var newItem = new TinyPNGResponseHistory { OccuredAt = DateTime.UtcNow, IsOptimized = responseItem.Output.IsOptimized, ImageId = timageId, Error = responseItem.Output.Error, Ratio = responseItem.Output.Ratio, OptimizedSize = responseItem.Output.Size }; if (responseItem.Input != null) { newItem.OriginSize = responseItem.Input.Size; newItem.Error = string.Empty; } _historyRepository.Create(newItem); }
private async Task <TinyResponse> TinifyByteArrayAsync(byte[] imageByteArray) { //removed image limitation Feature #3215 UPD: just remove any limitation on an image size TinyResponse tinyResponse; //if(imageByteArray.Length > PackageConstants.MaxImageSize) //{ // tinyResponse = new TinyResponse // { // Output = new TinyOutput // { // Error = PackageConstants.TooBigImage, // IsOptimized = false // } // }; //} //else //{ var byteContent = new ByteArrayContent(imageByteArray); try { var responseResult = await CreateRequestAsync(byteContent).ConfigureAwait(false); tinyResponse = _serializer.Deserialize <TinyResponse>(responseResult); tinyResponse.Output.IsOptimized = true; } catch (HttpRequestException ex) { tinyResponse = new TinyResponse { Output = new TinyOutput { Error = ex.Message, IsOptimized = false } }; } //} return(tinyResponse); }
private async Task <TinyResponse> TinifyByteArrayAsync(byte[] imageByteArray) { TinyResponse tinyResponse; if (imageByteArray.Length > PackageConstants.MaxImageSize) { tinyResponse = new TinyResponse { Output = new TinyOutput { Error = PackageConstants.TooBigImage, IsOptimized = false } }; } else { var byteContent = new ByteArrayContent(imageByteArray); try { var responseResult = await CreateRequestAsync(byteContent).ConfigureAwait(false); tinyResponse = _serializer.Deserialize <TinyResponse>(responseResult); tinyResponse.Output.IsOptimized = true; } catch (HttpRequestException ex) { tinyResponse = new TinyResponse { Output = new TinyOutput { Error = ex.Message, IsOptimized = false } }; } } return(tinyResponse); }
public async Task <TinyResponse> TinifyAsync(TImage tImage, IFileSystem fs) { TinyResponse tinyResponse; try { byte[] data = tImage.ToBytes(fs); tinyResponse = await TinifyByteArrayAsync(data).ConfigureAwait(false); } catch (Exception ex) { tinyResponse = new TinyResponse { Output = new TinyOutput { //Error = PackageConstants.ImageDeleted, ??? Error = ex.Message, IsOptimized = false } }; } return(tinyResponse); }