public async Task <DocumentAssetFile> ExecuteAsync(GetDocumentAssetFileByIdQuery query, IExecutionContext executionContext) { var dbResult = await _dbContext .DocumentAssets .Where(f => f.DocumentAssetId == query.DocumentAssetId && !f.IsDeleted) .Select(f => new { Extension = f.FileExtension, ContentType = f.ContentType, FileName = f.Title }) .SingleOrDefaultAsync(); if (dbResult == null) { return(null); } var fileName = Path.ChangeExtension(query.DocumentAssetId.ToString(), dbResult.Extension); var result = new DocumentAssetFile(); result.DocumentAssetId = query.DocumentAssetId; result.ContentType = dbResult.ContentType; result.ContentStream = await _fileStoreService.GetAsync(DocumentAssetConstants.FileContainerName, fileName); result.FileName = FilePathHelper.CleanFileName(Path.ChangeExtension(dbResult.FileName, dbResult.Extension), fileName); if (result.ContentStream == null) { throw new FileNotFoundException("DocumentAsset file could not be found", fileName); } return(result); }
public async Task <DocumentAssetFile> ExecuteAsync(GetDocumentAssetFileByIdQuery query, IExecutionContext executionContext) { var dbResult = await _dbContext .DocumentAssets .Where(f => f.DocumentAssetId == query.DocumentAssetId) .Select(f => new { f.FileExtension, f.ContentType, f.FileName, f.FileUpdateDate, f.FileNameOnDisk, f.VerificationToken }) .SingleOrDefaultAsync(); if (dbResult == null) { return(null); } var result = new DocumentAssetFile() { DocumentAssetId = query.DocumentAssetId, ContentType = dbResult.ContentType, FileName = dbResult.FileName, FileNameOnDisk = dbResult.FileNameOnDisk, FileExtension = dbResult.FileExtension, FileUpdateDate = dbResult.FileUpdateDate, VerificationToken = dbResult.VerificationToken }; result.FileStamp = AssetFileStampHelper.ToFileStamp(dbResult.FileUpdateDate); var fileName = Path.ChangeExtension(dbResult.FileNameOnDisk, dbResult.FileExtension); result.ContentStream = await _fileStoreService.GetAsync(DocumentAssetConstants.FileContainerName, fileName); if (result.ContentStream == null) { throw new FileNotFoundException("DocumentAsset file could not be found", fileName); } return(result); }
public async Task <ImageAssetFile> ExecuteAsync(GetImageAssetFileByIdQuery query, IExecutionContext executionContext) { // Render details is potentially cached so we query for this rather than directly with the db var getImageQuery = new GetImageAssetRenderDetailsByIdQuery(query.ImageAssetId); var dbResult = await _queryExecutor.ExecuteAsync(getImageQuery, executionContext); if (dbResult == null) { return(null); } var fileName = Path.ChangeExtension(query.ImageAssetId.ToString(), dbResult.Extension); var contentStream = await _fileStoreService.GetAsync(ImageAssetConstants.FileContainerName, fileName);; if (contentStream == null) { throw new FileNotFoundException("Image asset file could not be found", fileName); } var result = _imageAssetFileMapper.Map(dbResult, contentStream); return(result); }
public async Task <UseCaseResult <DownloadFileModel> > Handle(DownloadFile request, CancellationToken cancellationToken) { try { if (!string.IsNullOrEmpty(request.FileReference)) { var props = _fileStoreService.GetProperties(request.FileReference); var model = new DownloadFileModel { ContentType = props.ContentType, Contents = await _fileStoreService.GetAsync(request.FileReference) }; return(UseCaseResult <DownloadFileModel> .Create(model)); } else { return(UseCaseResult <DownloadFileModel> .CreateError(resultText : "File Reference not found")); } } catch (Exception e) { return(UseCaseResult <DownloadFileModel> .CreateError(resultText : e.Message)); } }
public async Task <Stream> GetAsync(IImageAssetRenderable asset, IImageResizeSettings inputSettings) { if ((inputSettings.Width < 1 && inputSettings.Height < 1) || (inputSettings.Width == asset.Width && inputSettings.Height == asset.Height)) { return(await GetFileStreamAsync(asset.ImageAssetId)); } if (_imageAssetsSettings.DisableResizing) { throw new InvalidImageResizeSettingsException("Image resizing has been requested but is disabled.", inputSettings); } var fullFileName = asset.FileNameOnDisk + "/" + CreateCacheFileName(inputSettings, asset); Stream imageStream = null; ValidateSettings(inputSettings); if (await _fileService.ExistsAsync(IMAGE_ASSET_CACHE_CONTAINER_NAME, fullFileName)) { return(await _fileService.GetAsync(IMAGE_ASSET_CACHE_CONTAINER_NAME, fullFileName)); } else { imageStream = new MemoryStream(); IImageFormat imageFormat = null; using (var originalStream = await GetFileStreamAsync(asset.ImageAssetId)) using (var image = Image.Load(originalStream, out imageFormat)) { if (imageFormat == null) { throw new Exception("Unable to determine image type for image asset " + asset.ImageAssetId); } var encoder = Configuration.Default.ImageFormatsManager.FindEncoder(imageFormat); if (encoder == null) { throw new InvalidOperationException("Encoder not found for image format " + imageFormat.Name); } var resizeOptions = ConvertSettings(inputSettings); image.Mutate(cx => { cx.Resize(resizeOptions); if (!string.IsNullOrWhiteSpace(inputSettings.BackgroundColor)) { var color = Rgba32.ParseHex(inputSettings.BackgroundColor); cx.BackgroundColor(color); } else if (CanPad(resizeOptions)) { if (SupportsTransparency(imageFormat)) { cx.BackgroundColor(Color.Transparent); encoder = EnsureEncoderSupportsTransparency(encoder); } else { // default background for jpg encoder is black, but white is a better default in most scenarios. cx.BackgroundColor(Color.White); } } }); image.Save(imageStream, encoder); } try { // Try and create the cache file, but don't throw an error if it fails - it will be attempted again on the next request await _fileService.CreateIfNotExistsAsync(IMAGE_ASSET_CACHE_CONTAINER_NAME, fullFileName, imageStream); } catch (Exception ex) { if (Debugger.IsAttached) { throw; } else { _logger.LogError(0, ex, "Error creating image asset cache file. Container name {ContainerName}, {fullFileName}", IMAGE_ASSET_CACHE_CONTAINER_NAME, fullFileName); } } imageStream.Position = 0; return(imageStream); } }