public ImageOptimizationResponse ProcessImage(ImageOptimizationRequest imageOptimizationRequest) { try { if (imageOptimizationRequest == null) { return(new ImageOptimizationResponse()); } var ParentAuth = new Auth { api_key = ImageOptimizationSettings.Instance.ApiKey, api_secret = ImageOptimizationSettings.Instance.ApiSecret }; string strimage_url = imageOptimizationRequest.ImageUrl; var krakenRequest = new KrakenRequest { auth = ParentAuth, url = strimage_url, wait = true, lossy = true }; KrakenResponse krakenResponse = this._krakenProxy.ProcessImage(krakenRequest); return(krakenResponse.ConvertToResponse()); }catch (Exception exception) { Debug.WriteLine(exception.ToString()); throw new Exception(exception.Message); } }
public static ImageOptimizationResponse ConvertToResponse(this KrakenResponse krakenResponse) { if (krakenResponse == null) { return(new ImageOptimizationResponse()); } var webClient = new WebClient(); var imageOptimizationResponse = new ImageOptimizationResponse { OriginalImageUrl = krakenResponse.file_name, OriginalImageSize = krakenResponse.original_size, PercentSaved = (krakenResponse.kraked_size / krakenResponse.original_size) * 100, OptimizedImageSize = krakenResponse.kraked_size, ErrorMessage = "An error has occurred while compressing image" }; if (!string.IsNullOrEmpty(krakenResponse.file_name)) { imageOptimizationResponse.OptimizedImage = webClient.DownloadData(krakenResponse.kraked_url); imageOptimizationResponse.Successful = true; } return(imageOptimizationResponse); }
private async Task <T> ReadResponse <T>(HttpResponseMessage resp) { string asString = await resp.Content.ReadAsStringAsync(); KrakenResponse <T> deserialized = JsonConvert.DeserializeObject <KrakenResponse <T> >(asString); if (deserialized.Error.Count > 0) { List <KrakenDiagnostic> diagnostics = deserialized.Error.Select(ParseDiagnostic).ToList(); if (!IgnoreWarnings || diagnostics.Any(kd => kd.Severity == DiagnosticSeverity.Error)) { throw KrakenException.FromDiagnostics(diagnostics); } } return(deserialized.Result); }
public KrakenResponse UploadFile(FileType fileType, string pathOrURL, bool isLossy, int imageWidth, int imageHeight, ImageResizingType resizingType) { KrakenResponse response = null; try { JObject obj; if (fileType == FileType.URL) { string jsonObj = string.Format(@"{{""auth"": {{ ""api_key"": ""{0}"", ""api_secret"": ""{1}"" }}, ""wait"": true, ""lossy"": {2}, ""resize"": {{ ""width"": {3}, ""height"": {4}, ""strategy"": ""{5}"" }}, ""url"": ""{6}"" }}", this.apiKey, this.apiSecreet, isLossy.ToString().ToLowerInvariant(), imageWidth, imageHeight, resizingType.ToString(), pathOrURL); object jsonResponse = this.Post("/url", jsonObj); obj = JObject.Parse(jsonResponse.ToString()); } else { string jsonObj = string.Format(@"{{""auth"": {{ ""api_key"": ""{0}"", ""api_secret"": ""{1}"" }}, ""wait"": true, ""lossy"": {2}, ""resize"": {{ ""width"": {3}, ""height"": {4}, ""strategy"": ""{5}"" }} }}", this.apiKey, this.apiSecreet, isLossy.ToString().ToLowerInvariant(), imageWidth, imageHeight, resizingType.ToString()); object jsonResponse = this.Post("/upload", jsonObj, pathOrURL); obj = JObject.Parse(jsonResponse.ToString()); } bool status = (bool)obj["success"]; if (!status) { string message = obj["message"].ToString(); throw new Exception(message); } response = JsonConvert.DeserializeObject <KrakenResponse>(obj.ToString()); } catch (Exception ex) { throw ex; } return(response); }