public static void AddImageFlowBlobService(IConfiguration configuration, IServiceCollection services, FileSpec fileSpec, string path) { var imageFlowSpec = ImageFileSpecs.GetImageFileSpec(fileSpec); services.AddImageflowAzureBlobService(new AzureBlobServiceOptions(configuration["Storage:ConnectionString"], new BlobClientOptions()) .MapPrefix(path, imageFlowSpec.ContainerName)); }
private static async Task ProcessImageAsync(Image image, byte[] imageBytes, FileSpec fileSpec) { _log.Information($"LB.PhotoGalleries.Worker.Program.ProcessImageAsync() - Image {image.Id} for file spec {fileSpec}"); var imageFileSpec = ImageFileSpecs.GetImageFileSpec(fileSpec); // we only generate images if the source image is larger than the size we're being asked to resize to, i.e. we only go down in size var longestSide = image.Metadata.Width > image.Metadata.Height ? image.Metadata.Width : image.Metadata.Height; if (longestSide <= imageFileSpec.PixelLength) { _log.Warning($"LB.PhotoGalleries.Worker.Program.ProcessImageAsync() - Image too small for this file spec: {fileSpec}: {image.Metadata.Width} x {image.Metadata.Height}"); return; } // create the new image file var storageId = imageFileSpec.GetStorageId(image); await using (var imageFile = await GenerateImageAsync(image, imageBytes, imageFileSpec)) { // upload the new image file to storage var containerClient = _blobServiceClient.GetBlobContainerClient(imageFileSpec.ContainerName); var uploadStopwatch = new Stopwatch(); uploadStopwatch.Start(); await containerClient.UploadBlobAsync(storageId, imageFile); uploadStopwatch.Stop(); _log.Information($"LB.PhotoGalleries.Worker.Program.ProcessImageAsync() - Upload blob elapsed time: {uploadStopwatch.ElapsedMilliseconds}ms"); } // update the Image object with the storage id of the newly-generated image file switch (fileSpec) { case FileSpec.Spec3840: image.Files.Spec3840Id = storageId; break; case FileSpec.Spec2560: image.Files.Spec2560Id = storageId; break; case FileSpec.Spec1920: image.Files.Spec1920Id = storageId; break; case FileSpec.Spec800: image.Files.Spec800Id = storageId; break; case FileSpec.SpecLowRes: image.Files.SpecLowResId = storageId; break; } }
private async Task HandleDeletePreGenImagesAsync(Image image) { await DeleteImageFileAsync(image, ImageFileSpecs.GetImageFileSpec(FileSpec.Spec3840)); await DeleteImageFileAsync(image, ImageFileSpecs.GetImageFileSpec(FileSpec.Spec2560)); await DeleteImageFileAsync(image, ImageFileSpecs.GetImageFileSpec(FileSpec.Spec1920)); await DeleteImageFileAsync(image, ImageFileSpecs.GetImageFileSpec(FileSpec.Spec800)); await DeleteImageFileAsync(image, ImageFileSpecs.GetImageFileSpec(FileSpec.SpecLowRes)); image.Files.Spec3840Id = null; image.Files.Spec2560Id = null; image.Files.Spec1920Id = null; image.Files.Spec800Id = null; image.Files.SpecLowResId = null; await UpdateImageAsync(image); }
/// <summary> /// Deletes the original image and any generated images for an Image. /// </summary> private static async Task DeleteImageFilesAsync(Image image, bool clearImageFileReferences = false) { // delete all image files var deleteTasks = new List <Task> { DeleteImageFileAsync(image, ImageFileSpecs.GetImageFileSpec(FileSpec.SpecOriginal)), DeleteImageFileAsync(image, ImageFileSpecs.GetImageFileSpec(FileSpec.Spec3840)), DeleteImageFileAsync(image, ImageFileSpecs.GetImageFileSpec(FileSpec.Spec2560)), DeleteImageFileAsync(image, ImageFileSpecs.GetImageFileSpec(FileSpec.Spec1920)), DeleteImageFileAsync(image, ImageFileSpecs.GetImageFileSpec(FileSpec.Spec800)), DeleteImageFileAsync(image, ImageFileSpecs.GetImageFileSpec(FileSpec.SpecLowRes)) }; await Task.WhenAll(deleteTasks); if (clearImageFileReferences) { image.Files.OriginalId = null; image.Files.Spec3840Id = null; image.Files.Spec2560Id = null; image.Files.Spec1920Id = null; image.Files.Spec800Id = null; image.Files.SpecLowResId = null; } }