Exemplo n.º 1
0
        /// <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 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(SaveCompressedImageToAmazonS3(result, amazonSettings, path));
        }
Exemplo n.º 2
0
        /// <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)
            {
                Content = new StringContent(amazonSettingsAsJson, System.Text.Encoding.UTF8, "application/json")
            };
            var response = await HttpClient.SendAsync(msg).ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                return(response.Headers.Location);
            }

            var errorMsg = JsonConvert.DeserializeObject <ApiErrorResponse>(await response.Content.ReadAsStringAsync().ConfigureAwait(false));

            throw new TinyPngApiException((int)response.StatusCode, response.ReasonPhrase, errorMsg.Error, errorMsg.Message);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Downloads the result of a TinyPng Compression operation
        /// </summary>
        /// <param name="compressResponse"></param>
        /// <param name="metadata"></param>
        /// <returns></returns>
        public static async Task <TinyPngImageResponse> Download(this TinyPngCompressResponse compressResponse, PreserveMetadata metadata = PreserveMetadata.None)
        {
            if (compressResponse == null)
            {
                throw new ArgumentNullException(nameof(compressResponse));
            }

            var msg = new HttpRequestMessage(HttpMethod.Get, compressResponse.Output.Url)
            {
                Content = CreateContent(metadata, compressResponse.Output.Type)
            };

            var response = await compressResponse.HttpClient.SendAsync(msg).ConfigureAwait(false);

            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);
        }