Skip to content

Commit

Permalink
Add support for preserve metadata (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitch528 authored and ctolkien committed Sep 11, 2017
1 parent 4ec9ac7 commit b955587
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
49 changes: 44 additions & 5 deletions src/TinyPNG/Extensions/DownloadExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using TinyPng.Responses;
Expand All @@ -9,35 +10,42 @@ namespace TinyPng

public static class DownloadExtensions
{
private const string JpegType = "image/jpeg";

/// <summary>
/// Downloads the result of a TinyPng Compression operation
/// </summary>
/// <param name="compressResponse"></param>
/// <param name="metadata"></param>
/// <returns></returns>
public async static Task<TinyPngImageResponse> Download(this Task<TinyPngCompressResponse> compressResponse)
public async static Task<TinyPngImageResponse> Download(this Task<TinyPngCompressResponse> compressResponse, PreserveMetadata metadata = PreserveMetadata.None)
{
if (compressResponse == null)
throw new ArgumentNullException(nameof(compressResponse));

var compressResult = await compressResponse;

return await Download(compressResult);
return await Download(compressResult, metadata);

}

/// <summary>
/// Downloads the result of a TinyPng Compression operation
/// </summary>
/// <param name="compressResponse"></param>
/// <param name="metadata"></param>
/// <returns></returns>
public async static Task<TinyPngImageResponse> Download(TinyPngCompressResponse compressResponse)
public async static Task<TinyPngImageResponse> Download(TinyPngCompressResponse compressResponse, PreserveMetadata metadata = PreserveMetadata.None)
{
if (compressResponse == null)
throw new ArgumentNullException(nameof(compressResponse));

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

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

if (response.IsSuccessStatusCode)
{
Expand All @@ -47,5 +55,36 @@ public async static Task<TinyPngImageResponse> Download(TinyPngCompressResponse
var errorMsg = JsonConvert.DeserializeObject<ApiErrorResponse>(await response.Content.ReadAsStringAsync());
throw new TinyPngApiException((int)response.StatusCode, response.ReasonPhrase, errorMsg.Error, errorMsg.Message);
}

private static HttpContent CreateContent(PreserveMetadata metadata, string type)
{
if (metadata == PreserveMetadata.None)
return null;

var preserve = new List<string>();

if (metadata.HasFlag(PreserveMetadata.Copyright))
{
preserve.Add("copyright");
}
if (metadata.HasFlag(PreserveMetadata.Creation))
{
if (type != JpegType)
throw new InvalidOperationException($"Creation metadata can only be preserved with type {JpegType}");

preserve.Add("creation");
}
if (metadata.HasFlag(PreserveMetadata.Location))
{
if (type != JpegType)
throw new InvalidOperationException($"Location metadata can only be preserved with type {JpegType}");

preserve.Add("location");
}

string json = JsonConvert.SerializeObject(new { preserve });

return new StringContent(json, System.Text.Encoding.UTF8, "application/json");
}
}
}
17 changes: 17 additions & 0 deletions src/TinyPNG/PreserveMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TinyPng
{
[Flags]
public enum PreserveMetadata
{
None = 1 << 0,
Copyright = 1 << 1,
Creation = 1 << 2,
Location = 1 << 3
}
}

0 comments on commit b955587

Please sign in to comment.