/// <summary> /// Stores a previously compressed image directly into Amazon S3 storage /// </summary> /// <param name="result">The previously compressed image</param> /// <param name="path">The path to storage the image as</param> /// <param name="bucketOverride">Optional: To override the previously configured bucket</param> /// <param name="regionOverride">Optional: To override the previously configured region</param> /// <returns></returns> public async Task<Uri> SaveCompressedImageToAmazonS3(TinyPngCompressResponse result, string path, string bucketOverride = "", string regionOverride = "") { if (result == null) throw new ArgumentNullException(nameof(result)); if (AmazonS3Configuration == null) throw new InvalidOperationException("AmazonS3Configuration has not been configured"); if (string.IsNullOrEmpty(path)) throw new ArgumentNullException(nameof(path)); var amazonSettings = AmazonS3Configuration.Clone(); amazonSettings.Path = path; if (!string.IsNullOrEmpty(regionOverride)) amazonSettings.Region = regionOverride; if (!string.IsNullOrEmpty(bucketOverride)) amazonSettings.Bucket = bucketOverride; return await SaveCompressedImageToAmazonS3(result, amazonSettings, path); }
/// <summary> /// Stores a previously compressed image directly into Amazon S3 storage /// </summary> /// <param name="result">The previously compressed image</param> /// <param name="amazonSettings">The settings for the amazon connection</param> /// <param name="path">The path and bucket to store in: bucket/file.png format</param> /// <returns></returns> public async Task<Uri> SaveCompressedImageToAmazonS3(TinyPngCompressResponse result, AmazonS3Configuration amazonSettings, string path) { if (result == null) throw new ArgumentNullException(nameof(result)); if (amazonSettings == null) throw new ArgumentNullException(nameof(amazonSettings)); if (string.IsNullOrEmpty(path)) throw new ArgumentNullException(nameof(path)); amazonSettings.Path = path; var amazonSettingsAsJson = JsonConvert.SerializeObject(new { store = amazonSettings }, jsonSettings); var msg = new HttpRequestMessage(HttpMethod.Post, result.Output.Url); msg.Content = new StringContent(amazonSettingsAsJson, System.Text.Encoding.UTF8, "application/json"); var response = await httpClient.SendAsync(msg); if (response.IsSuccessStatusCode) { return response.Headers.Location; } var errorMsg = JsonConvert.DeserializeObject<ApiErrorResponse>(await response.Content.ReadAsStringAsync()); throw new TinyPngApiException((int)response.StatusCode, response.ReasonPhrase, errorMsg.Error, errorMsg.Message); }
/// <summary> /// Uses the TinyPng API to create a resized version of your uploaded image. /// </summary> /// <param name="result">This is the previous result of running a compression <see cref="Compress(string)"/></param> /// <param name="resizeOperation">Supply a strongly typed Resize Operation. See <typeparamref name="CoverResizeOperation"/>, <typeparamref name="FitResizeOperation"/>, <typeparamref name="ScaleHeightResizeOperation"/>, <typeparamref name="ScaleWidthResizeOperation"/></param> /// <returns></returns> public async Task<TinyPngResizeResponse> Resize(TinyPngCompressResponse result, ResizeOperation resizeOperation) { if (result == null) throw new ArgumentNullException(nameof(result)); if (resizeOperation == null) throw new ArgumentNullException(nameof(resizeOperation)); var requestBody = JsonConvert.SerializeObject(new { resize = resizeOperation }, jsonSettings); var msg = new HttpRequestMessage(HttpMethod.Post, result.Output.Url); msg.Content = new StringContent(requestBody, System.Text.Encoding.UTF8, "application/json"); var response = await httpClient.SendAsync(msg); if (response.IsSuccessStatusCode) { return new TinyPngResizeResponse(response); } var errorMsg = JsonConvert.DeserializeObject<ApiErrorResponse>(await response.Content.ReadAsStringAsync()); throw new TinyPngApiException((int)response.StatusCode, response.ReasonPhrase, errorMsg.Error, errorMsg.Message); }
/// <summary> /// Uses the TinyPng API to create a resized version of your uploaded image. /// </summary> /// <param name="result">This is the previous result of running a compression <see cref="Compress(string)"/></param> /// <param name="width"></param> /// <param name="height"></param> /// <param name="resizeType"></param> /// <returns></returns> public async Task<TinyPngResizeResponse> Resize(TinyPngCompressResponse result, int width, int height, ResizeType resizeType = ResizeType.Fit) { if (result == null) throw new ArgumentNullException(nameof(result)); if (width == 0) throw new ArgumentOutOfRangeException(nameof(width)); if (height == 0) throw new ArgumentOutOfRangeException(nameof(height)); var resizeOp = new ResizeOperation(resizeType, width, height); return await Resize(result, resizeOp); }
public async Task<TinyPngImageResponse> Download(TinyPngCompressResponse result) { if (result == null) throw new ArgumentNullException(nameof(result)); var msg = new HttpRequestMessage(HttpMethod.Get, result.Output.Url); var response = await httpClient.SendAsync(msg); if (response.IsSuccessStatusCode) { return new TinyPngImageResponse(response); } var errorMsg = JsonConvert.DeserializeObject<ApiErrorResponse>(await response.Content.ReadAsStringAsync()); throw new TinyPngApiException((int)response.StatusCode, response.ReasonPhrase, errorMsg.Error, errorMsg.Message); }