Example #1
0
        public async Task <string> UploadImageAsync(
            IFormFile file,
            CancellationToken cancellationToken = default
            )
        {
            if (file == null)
            {
                throw new ArgumentNullException(nameof(file));
            }

            using (var stream = new MemoryStream())
            {
                await file.CopyToAsync(stream, cancellationToken);

                string formatDescription;
                try
                {
                    formatDescription = ImageExtensions.AssertFileIsWebFriendlyImageAndGetFormatDescription(stream);
                }
                catch (ArgumentException e)
                {
                    throw new UnsupportedMediaTypeException("The file has unsupported media type", e);
                }

                stream.Seek(0, SeekOrigin.Begin);

                var blobName = $"{Guid.NewGuid()}.{formatDescription}";

                return(await UploadToStorageInternal(stream, blobName, cancellationToken));
            }
        }