示例#1
0
        public async Task <IActionResult> GetAvatar(string avatarId)
        {
            if (string.IsNullOrWhiteSpace(avatarId))
            {
                return(BadRequest());
            }

            return(File(
                       await avatarStore.GetAvatarBytesAsync(avatarId),
                       mimeMapping.GetMimeMapping(Path.GetExtension(avatarId))));
        }
#pragma warning restore 1998

        /// <inheritdoc />
        public async Task <Photo> GetPhotoAsync(PhotoIdentifier photoIdentifier)
        {
            if (photoIdentifier == null)
            {
                throw new ArgumentNullException(nameof(photoIdentifier));
            }

            var filePath = filePathCalculator.CalculatePath(configuration.RootFolder, photoIdentifier);


            using (var fileStream = new FileStream(filePath, FileMode.Open))
            {
                var memStream = new MemoryStream();
                await fileStream.CopyToAsync(memStream);

                memStream.Seek(0, 0);

                return(new Photo(
                           photoIdentifier,
                           mimeMapping.GetMimeMapping(Path.GetExtension(photoIdentifier.Filename)),
                           memStream));
            }
        }
        /// <inheritdoc />
        public async Task <Photo> GetPhotoAsync(PhotoIdentifier photoIdentifier)
        {
            if (photoIdentifier == null)
            {
                throw new ArgumentNullException(nameof(photoIdentifier));
            }

            logger.LogInformation("Retrieving photo (Get) with identifier [{PhotoIdentifier}]", photoIdentifier);

            try
            {
                var cloudBlobContainer = await OpenOrCreateBlobContainerAsync();

                var cloudBlob = cloudBlobContainer.GetBlockBlobReference(GetBlobName(photoIdentifier));

                var memStream = new MemoryStream();
                await cloudBlob.DownloadToStreamAsync(memStream);

                memStream.Seek(0, 0);
                await cloudBlob.FetchAttributesAsync();

                var contentType = mimeMapping.GetMimeMapping(Path.GetExtension(photoIdentifier.Filename));

                logger.LogInformation("Retrieved photo (Get) with identifier [{PhotoIdentifier}] successfully.", photoIdentifier);

                return(new Photo(photoIdentifier, contentType, memStream));
            }
            catch (Exception ex)
            {
                logger.LogError(
                    ex,
                    "Error retrieving image from Azure Blob Storage! Photo-Identifier: [{PhotoIdentifier}]",
                    photoIdentifier);
                throw;
            }
        }