Example #1
0
        public async Task <List <ImageThumbnailGeneratingResult> > GenerateThumbnails(string imageId, string partitionKey)
        {
            var imageMetadataList = await _metadataStorage.GetMetadata(new List <string> {
                imageId
            }, partitionKey, CancellationToken.None);

            if (imageMetadataList == null || !imageMetadataList.Any())
            {
                _logger.LogInformation(ImageNotFound, imageId);
                return(null);
            }

            var imageMetadata = imageMetadataList[0];

            imageMetadata.Thumbnails = new List <ImageThumbnail>();
            _logger.LogDebug(MetadataReceived, imageMetadata.Id, imageMetadata.Width, imageMetadata.Height);
            if (imageMetadata.Width <= _thumbnailTargetWidth.Last())
            {
                _logger.LogInformation(ImageSmallerThanTargetWidth, imageMetadata.Id, imageMetadata.Width);
                return(new List <ImageThumbnailGeneratingResult>(0));
            }

            using var originalImageStream = await _blobStorage.DownloadImage(imageMetadata.Name, ImageSizeType.Original, CancellationToken.None);

            _logger.LogDebug(ImageDownloaded, imageMetadata.Id);

            StorageImageType imageType     = imageMetadata.ImageType;
            string           mimeType      = imageMetadata.MimeType;
            string           fileExtension = imageMetadata.FileExtension;

            if (imageType == StorageImageType.BMP)
            {
                imageType     = StorageImageType.PNG;
                mimeType      = PngMimeType;
                fileExtension = PngFileExtension;
            }

            var imageStream = originalImageStream;

            foreach (var targetWidth in _thumbnailTargetWidth)
            {
                if (targetWidth >= imageMetadata.Width)
                {
                    continue;
                }

                var resizingResult = _imageResizing.Resize(imageStream, (ImageType)imageType, targetWidth);
                _logger.LogDebug(ImageResized, imageMetadata.Id, resizingResult.Size);

                var thumbnailImageId   = _imageIdentifierProvider.GenerateUniqueId();
                var thumbnailImageName = _imageIdentifierProvider.GetImageName(thumbnailImageId, fileExtension);

                var uploadedBlob = await _blobStorage.UploadImage(thumbnailImageName, resizingResult.Image, ImageSizeType.Thumbnail, mimeType, CancellationToken.None);

                imageMetadata.Thumbnails.Add(new ImageThumbnail
                {
                    Id           = thumbnailImageId,
                    Name         = thumbnailImageName,
                    MD5Hash      = uploadedBlob.MD5Hash,
                    DateAddedUtc = uploadedBlob.DateAdded.DateTime,
                    MimeType     = mimeType,
                    Height       = resizingResult.Size.Height,
                    Width        = resizingResult.Size.Width,
                    SizeBytes    = resizingResult.Size.Bytes
                });

                imageStream = resizingResult.Image;
            }

            imageStream.Dispose();

            imageMetadata.Thumbnails.Reverse();
            await _metadataStorage.SetMetadata(imageMetadata, CancellationToken.None);

            _logger.LogInformation(ThumbnailsGenerated, imageMetadata.Thumbnails.Count, imageId);

            return(imageMetadata.Thumbnails.Select(x => new ImageThumbnailGeneratingResult
            {
                Id = x.Id,
                Width = x.Width,
                Height = x.Height,
                MimeType = x.MimeType
            }).ToList());
        }
        private async Task <ImageMetadata> UploadImage(string imagePath, int width, int height, string contentType, ImageType imageType, string partitionKey)
        {
            string extension = "";

            switch (imageType)
            {
            case ImageType.BMP:
                extension = ".bmp";
                break;

            case ImageType.JPEG:
                extension = ".jpeg";
                break;

            case ImageType.PNG:
                extension = ".png";
                break;

            case ImageType.GIF:
                extension = ".gif";
                break;
            }

            await using FileStream file = OpenFileForReading(imagePath);
            string imageId           = Guid.NewGuid().ToString();
            string imageName         = imageId + extension;
            var    uploadImageResult = await _blobStorage.UploadImage(imageName, file, ImageSizeType.Original, contentType, CancellationToken.None);

            var imageMetadata = new ImageMetadata
            {
                Id           = imageId,
                Name         = imageName,
                DateAddedUtc = uploadImageResult.DateAdded.DateTime,
                Height       = height,
                Width        = width,
                MD5Hash      = uploadImageResult.MD5Hash,
                SizeBytes    = file.Length,
                MimeType     = contentType,
                ImageType    = imageType,
                PartitionKey = partitionKey
            };

            await _metadataStorage.SetMetadata(imageMetadata, CancellationToken.None);

            return(imageMetadata);
        }