public ImagemDTO BaixarImagem(int id) { ImagemDTO imagemDto = null; var produto = _repository.Obter(id); if (produto != null) { if (!string.IsNullOrEmpty(produto.Imagem)) { imagemDto = new ImagemDTO() { Stream = _imageStorage.BaixarImagem(produto.Imagem), ContentType = LocalImageStorage.GetImageMimeTypeFromImageFileExtension(produto.Imagem) } } ; else { imagemDto = new ImagemDTO() { Stream = _imageStorage.BaixarImagem(), ContentType = LocalImageStorage.GetImageMimeTypeFromImageFileExtension(LocalImageStorage.NOT_FOUND_IMAGE) } }; } return(imagemDto); }
private IImageStorage CreateImageStorage() { IImageStorage storage; if (Environment.IsProduction()) { storage = new AzureBlobImageStorage(new AzureBlobClientFactory(Configuration.BlobStorageConnectionString, "images")); } else { string imageOutputPath = Path.Combine(Environment.WebRootPath, IMAGE_DIRECTORY_NAME); string imageBaseUri = Path.Combine(Configuration.BaseUrl, STATIC_FILE_BASE_URI, IMAGE_DIRECTORY_NAME); storage = new LocalImageStorage(imageOutputPath, imageBaseUri); } return(storage); }
public async Task Invoke(HttpContext context) { logger.LogTrace(MainCfg.LogEventId, "Start image middleware..."); string imagePath = context.Request.Path; DateTime now = DateTime.UtcNow; IFileInfo fileInfo; if (!string.IsNullOrWhiteSpace(imagePath) && imagePath.Length > 4) { logger.LogInformation(MainCfg.LogEventId, "The request path is '{0}'", imagePath); IImageStorage imageStorage = new LocalImageStorage(imagePath, fileProvider); fileInfo = GetImageFromCache(imageStorage, now); if ((fileInfo == null || !fileInfo.Exists) && imageStorage.IsImageSupported) { fileInfo = await GetImageFromStorageAsync(imageStorage, now).ConfigureAwait(false); if (fileInfo == null || !fileInfo.Exists) { logger.LogError(MainCfg.LogEventId, "Couldn't get the image '{0}' from storage!", imagePath); fileInfo = await new LocalImageStorage(MainCfg.Images.EmptyPreview, imageStorage.SizeSetting, fileProvider) .GetImageVariantAsync() .ConfigureAwait(false); } } if (fileInfo != null && fileInfo.Exists) { await context.Response.SendFileAsync(fileInfo).ConfigureAwait(false); return; } } await this.next.Invoke(context).ConfigureAwait(false); }