Created
February 7, 2018 16:31
-
-
Save yasen-yankov/9b3689b810d5410ff89aab8c8b1bffa8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Specialized; | |
using System.IO; | |
using System.Net; | |
using Kraken; | |
using Kraken.Http; | |
using Kraken.Model; | |
using Telerik.Sitefinity.FileProcessors; | |
namespace Telerik.Sitefinity.ImageOptimization | |
{ | |
/// <summary> | |
/// Implementation of <see cref="ImageOptimizationProcessorBase"/> using Kraken.IO. | |
/// </summary> | |
internal class KrakenImageOptimizationProcessor : ImageOptimizationProcessorBase | |
{ | |
public override string ConfigName | |
{ | |
get | |
{ | |
return "Karken IO Image Optimization"; | |
} | |
} | |
public override string ConfigDescription | |
{ | |
get | |
{ | |
return "Optimizes image size using Kraken.IO"; | |
} | |
} | |
public override NameValueCollection ConfigParameters | |
{ | |
get | |
{ | |
var configParameters = base.ConfigParameters; | |
configParameters.Add(KrakenImageOptimizationProcessor.ApiKeyConfigName, ""); | |
configParameters.Add(KrakenImageOptimizationProcessor.ApiSecretConfigName, ""); | |
configParameters.Add(KrakenImageOptimizationProcessor.LossyCompressionConfigName, ""); | |
return configParameters; | |
} | |
} | |
/// <inheritdoc /> | |
protected override bool InitializeOverride(NameValueCollection config) | |
{ | |
if (config == null) | |
{ | |
return false; | |
} | |
string lossyCompressionString = config[KrakenImageOptimizationProcessor.LossyCompressionConfigName]; | |
if (!string.IsNullOrWhiteSpace(lossyCompressionString)) | |
{ | |
this.lossyCompression = bool.Parse(lossyCompressionString); | |
} | |
string apiKey = config[KrakenImageOptimizationProcessor.ApiKeyConfigName]; | |
string apiSecret = config[KrakenImageOptimizationProcessor.ApiSecretConfigName]; | |
if (string.IsNullOrWhiteSpace(apiKey) || string.IsNullOrWhiteSpace(apiSecret)) | |
{ | |
return false; | |
} | |
var connection = Connection.Create(apiKey, apiSecret); | |
this.client = new Client(connection); | |
return true; | |
} | |
/// <inheritdoc /> | |
public override Stream Process(FileProcessorInput fileInput) | |
{ | |
if (fileInput == null) | |
{ | |
throw new ArgumentException("fileInput cannot be null"); | |
} | |
byte[] imageBytes = this.GetByteArray(fileInput.FileStream); | |
string imageName = Guid.NewGuid().ToString(); | |
OptimizeUploadWaitRequest optimizeUploadWaitRequest = new OptimizeUploadWaitRequest(); | |
optimizeUploadWaitRequest.Lossy = this.lossyCompression; | |
IApiResponse<OptimizeWaitResult> response = this.client.OptimizeWait(imageBytes, imageName, optimizeUploadWaitRequest).Result; | |
if (!response.Success || response.StatusCode != HttpStatusCode.OK) | |
{ | |
return fileInput.FileStream; | |
} | |
Stream stream = this.GetImageStream(response.Body.KrakedUrl); | |
return stream; | |
} | |
/// <summary> | |
/// Gets an in memory stream created from downloading the image in the provided URL. | |
/// </summary> | |
/// <param name="url">URL of the image that needs to be downloaded.</param> | |
/// <returns>The stream.</returns> | |
protected virtual Stream GetImageStream(string url) | |
{ | |
if (string.IsNullOrWhiteSpace(url)) | |
{ | |
throw new ArgumentException("url cannot be null, empty or whitespace"); | |
} | |
WebRequest webRequest = HttpWebRequest.Create(url); | |
WebResponse webResponse = webRequest.GetResponse(); | |
return webResponse.GetResponseStream(); | |
} | |
/// <summary> | |
/// Gets a byte array from the provided stream. | |
/// </summary> | |
/// <param name="stream">The stream.</param> | |
/// <returns>The byte array.</returns> | |
protected virtual byte[] GetByteArray(Stream stream) | |
{ | |
if (stream == null) | |
{ | |
throw new ArgumentException("stream cannot be null"); | |
} | |
byte[] bytes = new byte[stream.Length]; | |
stream.Read(bytes, 0, (int)stream.Length); | |
return bytes; | |
} | |
private bool lossyCompression; | |
private Client client; | |
private const string ApiKeyConfigName = "ApiKey"; | |
private const string ApiSecretConfigName = "ApiSecret"; | |
private const string LossyCompressionConfigName = "LossyCompression"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment