Created
June 25, 2020 18:16
-
-
Save ajtatum/3a91d3bd65b5c4d200f4b8235c6bbf95 to your computer and use it in GitHub Desktop.
azure-function-kraken.cs
This file contains hidden or 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 OptimizeSetWaitRequest = Kraken.Model.Azure.OptimizeSetWaitRequest; | |
using DataStore = Kraken.Model.Azure.DataStore; | |
namespace CreateMedia | |
{ | |
public class HttpCreate | |
{ | |
private static readonly string BlobStorageConnectionString = Environment.GetEnvironmentVariable("AzureWebJobsStorage"); | |
private static readonly string KrakenApiKey = Environment.GetEnvironmentVariable("KrakenApiKey"); | |
private static readonly string KrakenApiSecret = Environment.GetEnvironmentVariable("KrakenApiSecret"); | |
private static readonly string StorageAccountName = Environment.GetEnvironmentVariable("StorageAccountName"); | |
private static readonly string StorageAccountKey = Environment.GetEnvironmentVariable("StorageAccountKey"); | |
private static readonly string StorageAccountContainerMediaProcessing = Environment.GetEnvironmentVariable("StorageAccountContainerMediaProcessing"); | |
private static readonly string StorageAccountContainerPhotos = Environment.GetEnvironmentVariable("StorageAccountContainerPhotos"); | |
private static readonly string StorageAccountContainerVideos = Environment.GetEnvironmentVariable("StorageAccountContainerVideos"); | |
private static readonly string StorageBaseUrlFormat = Environment.GetEnvironmentVariable("StorageBaseUrlFormat"); | |
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] | |
HttpRequest req, ILogger log) | |
{ | |
var imageUrlList = new List<string>(); | |
//imageUrlList contains URLs of images that need to be resized | |
var processedPhotoUrls = new List<string>(); | |
var photosBaseUrl = $"{string.Format(StorageBaseUrlFormat, StorageAccountContainerPhotos)}"; | |
var krakenConnection = Connection.Create(KrakenApiKey, KrakenApiSecret); | |
var krakenClient = new Kraken.Client(krakenConnection); | |
var azureDataStore = new DataStore(StorageAccountName, StorageAccountKey, StorageAccountContainerPhotos); | |
azureDataStore.AddHeaders("Content-Type", "image/jpeg"); | |
foreach (var imageUrl in imageUrlList) | |
{ | |
var imageUri = new Uri(imageUrl); | |
var request = new OptimizeSetWaitRequest(imageUri, azureDataStore) | |
{ | |
Lossy = true, | |
WebP = false, | |
Quality = 75 | |
}; | |
var imageName = Path.GetFileNameWithoutExtension(imageUrl); | |
var isCoverPhoto = imageName == "01"; | |
request.AddSet(new ResizeImageSet() | |
{ | |
Name = "normal", | |
Strategy = Strategy.None, | |
StoragePath = $"{media.Studio.UrlSlug}/{media.Id}/{imageName}.jpg", | |
}); | |
processedPhotoUrls.Add($"{photosBaseUrl}/{media.Studio.UrlSlug}/{media.Id}/{imageName}.jpg"); | |
if (isCoverPhoto) | |
{ | |
request.AddSet(new ResizeImageSet() | |
{ | |
Name = "thumbnail", | |
Width = 250, | |
Strategy = Strategy.Landscape, | |
Enhance = true, | |
StoragePath = $"{media.Studio.UrlSlug}/{media.Id}/{imageName}-thumbnail.jpg", | |
}); | |
processedPhotoUrls.Add($"{photosBaseUrl}/{media.Studio.UrlSlug}/{media.Id}/{imageName}-thumbnail.jpg"); | |
request.AddSet(new ResizeImageSet() | |
{ | |
Name = "mini", | |
Width = 70, | |
Strategy = Strategy.Landscape, | |
Enhance = true, | |
StoragePath = $"{media.Studio.UrlSlug}/{media.Id}/{imageName}-mini.jpg", | |
}); | |
processedPhotoUrls.Add($"{photosBaseUrl}/{media.Studio.UrlSlug}/{media.Id}/{imageName}-mini.jpg"); | |
} | |
else | |
{ | |
request.AddSet(new ResizeImageSet() | |
{ | |
Name = "thumbnail", | |
Height = 150, | |
Strategy = Strategy.Portrait, | |
Enhance = true, | |
StoragePath = $"{media.Studio.UrlSlug}/{media.Id}/{imageName}-thumbnail.jpg", | |
}); | |
processedPhotoUrls.Add($"{photosBaseUrl}/{media.Studio.UrlSlug}/{media.Id}/{imageName}-thumbnail.jpg"); | |
} | |
var response = krakenClient.OptimizeWait(request); | |
if (response.Result.StatusCode == HttpStatusCode.OK) | |
{ | |
log.LogInformation("Image {ImageName} has been resized.", imageName); | |
} | |
else | |
{ | |
log.LogError("Error renaming image {ImageName}. Error message: {ErrorMessage}.", imageName, response.Result.Error); | |
} | |
} | |
//update database | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment