in processing-pipelines/image-v2/resizer/csharp/Function.cs [47:92]
public async Task HandleAsync(HttpContext context)
{
_logger.LogInformation("Function received request");
try
{
var (bucket, file) = await _requestReader.ReadCloudStorageData(context);
using (var inputStream = new MemoryStream())
{
var client = await StorageClient.CreateAsync();
await client.DownloadObjectAsync(bucket, file, inputStream);
_logger.LogInformation($"Downloaded '{file}' from bucket '{bucket}'");
using (var outputStream = new MemoryStream())
{
inputStream.Position = 0; // Reset to read
using (Image image = Image.Load(inputStream))
{
image.Mutate(x => x
.Resize(ThumbWidth, ThumbHeight)
);
_logger.LogInformation($"Resized image '{file}' to {ThumbWidth}x{ThumbHeight}");
image.SaveAsPng(outputStream);
}
var outputFile = $"{Path.GetFileNameWithoutExtension(file)}-{ThumbWidth}x{ThumbHeight}.png";
await client.UploadObjectAsync(_outputBucket, outputFile, "image/png", outputStream);
_logger.LogInformation($"Uploaded '{outputFile}' to bucket '{_outputBucket}'");
var replyData = new {bucket = _outputBucket, file = outputFile};
var json = JsonConvert.SerializeObject(replyData);
_logger.LogInformation($"Replying back with json: {json}");
context.Response.ContentType = "application/json";
await context.Response.WriteAsync(json);
}
}
}
catch (Exception e)
{
_logger.LogError($"Error processing: " + e.Message);
throw e;
}
}