/// <summary> /// Used internally to map a model that inherits from ImageAssetSummary. /// </summary> public ImageAssetSummary Map <TModel>(TModel imageToMap, ImageAsset dbImage) where TModel : ImageAssetSummary { imageToMap.AuditData = _auditDataMapper.MapUpdateAuditData(dbImage); imageToMap.ImageAssetId = dbImage.ImageAssetId; imageToMap.FileExtension = dbImage.FileExtension; imageToMap.FileName = dbImage.FileName; imageToMap.FileSizeInBytes = dbImage.FileSizeInBytes; imageToMap.Height = dbImage.HeightInPixels; imageToMap.Width = dbImage.WidthInPixels; imageToMap.Title = dbImage.Title; imageToMap.FileStamp = AssetFileStampHelper.ToFileStamp(dbImage.FileUpdateDate); imageToMap.FileNameOnDisk = dbImage.FileNameOnDisk; imageToMap.DefaultAnchorLocation = dbImage.DefaultAnchorLocation; imageToMap.VerificationToken = dbImage.VerificationToken; imageToMap.Tags = dbImage .ImageAssetTags .Select(t => t.Tag.TagText) .OrderBy(t => t) .ToList(); imageToMap.Url = _imageAssetRouteLibrary.ImageAsset(imageToMap); return(imageToMap); }
/// <summary> /// Maps an EF ImageAsset record from the db into a ImageAssetDetails /// object. If the db record is null then null is returned. /// </summary> /// <param name="dbImage">ImageAsset record from the database.</param> public ImageAssetRenderDetails Map(ImageAsset dbImage) { if (dbImage == null) { return(null); } var image = new ImageAssetRenderDetails() { ImageAssetId = dbImage.ImageAssetId, FileExtension = dbImage.FileExtension, FileName = dbImage.FileName, FileNameOnDisk = dbImage.FileNameOnDisk, Height = dbImage.HeightInPixels, Width = dbImage.WidthInPixels, Title = dbImage.Title, DefaultAnchorLocation = dbImage.DefaultAnchorLocation, FileUpdateDate = dbImage.FileUpdateDate, VerificationToken = dbImage.VerificationToken }; image.FileStamp = AssetFileStampHelper.ToFileStamp(dbImage.FileUpdateDate); image.Url = _imageAssetRouteLibrary.ImageAsset(image); return(image); }
public async Task ExecuteAsync(UpdateImageAssetCommand command, IExecutionContext executionContext) { bool hasNewFile = command.File != null; if (hasNewFile) { _assetFileTypeValidator.ValidateAndThrow(command.File.FileName, command.File.MimeType, nameof(command.File)); } var imageAsset = await _dbContext .ImageAssets .Include(a => a.ImageAssetTags) .ThenInclude(a => a.Tag) .FilterById(command.ImageAssetId) .SingleOrDefaultAsync(); imageAsset.Title = command.Title; imageAsset.FileName = SlugFormatter.ToSlug(command.Title); imageAsset.DefaultAnchorLocation = command.DefaultAnchorLocation; _entityTagHelper.UpdateTags(imageAsset.ImageAssetTags, command.Tags, executionContext); _entityAuditHelper.SetUpdated(imageAsset, executionContext); using (var scope = _transactionScopeFactory.Create(_dbContext)) { if (hasNewFile) { var deleteOldFileCommand = new QueueAssetFileDeletionCommand() { EntityDefinitionCode = ImageAssetEntityDefinition.DefinitionCode, FileNameOnDisk = imageAsset.FileNameOnDisk, FileExtension = imageAsset.FileExtension }; imageAsset.FileUpdateDate = executionContext.ExecutionDate; var fileStamp = AssetFileStampHelper.ToFileStamp(imageAsset.FileUpdateDate); imageAsset.FileNameOnDisk = $"{imageAsset.ImageAssetId}-{fileStamp}"; await _commandExecutor.ExecuteAsync(deleteOldFileCommand); await _imageAssetFileService.SaveAsync(command.File, imageAsset, nameof(command.File)); } await _dbContext.SaveChangesAsync(); scope.QueueCompletionTask(() => OnTransactionComplete(hasNewFile, imageAsset)); await scope.CompleteAsync(); } }
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 SaveFile(IUploadedFile uploadedFile, DocumentAsset documentAsset) { documentAsset.ContentType = _mimeTypeService.MapFromFileName(uploadedFile.FileName, uploadedFile.MimeType); documentAsset.FileExtension = Path.GetExtension(uploadedFile.FileName).TrimStart('.'); documentAsset.FileNameOnDisk = "file-not-saved"; _assetFileTypeValidator.ValidateAndThrow(documentAsset.FileExtension, documentAsset.ContentType, "File"); ValidateFileType(documentAsset); var fileStamp = AssetFileStampHelper.ToFileStamp(documentAsset.FileUpdateDate); using (var inputSteam = await uploadedFile.OpenReadStreamAsync()) { bool isNew = documentAsset.DocumentAssetId < 1; documentAsset.FileSizeInBytes = Convert.ToInt32(inputSteam.Length); using (var scope = _transactionScopeFactory.Create(_dbContext)) { // Save at this point if it's a new file if (isNew) { await _dbContext.SaveChangesAsync(); } // update the filename documentAsset.FileNameOnDisk = $"{documentAsset.DocumentAssetId}-{fileStamp}"; var fileName = Path.ChangeExtension(documentAsset.FileNameOnDisk, documentAsset.FileExtension); // Save the raw file directly await CreateFileAsync(isNew, fileName, inputSteam); // Update the filename await _dbContext.SaveChangesAsync(); await scope.CompleteAsync(); } } }
public async Task ExecuteAsync(AddImageAssetCommand command, IExecutionContext executionContext) { var imageAsset = new ImageAsset(); imageAsset.Title = command.Title; imageAsset.FileName = SlugFormatter.ToSlug(command.Title); imageAsset.DefaultAnchorLocation = command.DefaultAnchorLocation; imageAsset.FileUpdateDate = executionContext.ExecutionDate; imageAsset.FileNameOnDisk = "file-not-saved"; imageAsset.FileExtension = "unknown"; imageAsset.VerificationToken = _randomStringGenerator.Generate(6); _assetFileTypeValidator.ValidateAndThrow(command.File.FileName, command.File.MimeType, nameof(command.File)); var fileStamp = AssetFileStampHelper.ToFileStamp(imageAsset.FileUpdateDate); _entityTagHelper.UpdateTags(imageAsset.ImageAssetTags, command.Tags, executionContext); _entityAuditHelper.SetCreated(imageAsset, executionContext); _dbContext.ImageAssets.Add(imageAsset); using (var scope = _transactionScopeFactory.Create(_dbContext)) { // Save first to get an Id await _dbContext.SaveChangesAsync(); // Update the disk filename imageAsset.FileNameOnDisk = $"{imageAsset.ImageAssetId}-{fileStamp}"; await _imageAssetFileService.SaveAsync(command.File, imageAsset, nameof(command.File)); command.OutputImageAssetId = imageAsset.ImageAssetId; scope.QueueCompletionTask(() => OnTransactionComplete(imageAsset)); await scope.CompleteAsync(); } }
/// <summary> /// Used internally to map a model that inherits from DocumentAssetSummary. /// </summary> public DocumentAssetSummary Map <TModel>(TModel document, DocumentAsset dbDocument) where TModel : DocumentAssetSummary { document.AuditData = _auditDataMapper.MapUpdateAuditData(dbDocument); document.DocumentAssetId = dbDocument.DocumentAssetId; document.FileExtension = dbDocument.FileExtension; document.FileName = dbDocument.FileName; document.FileSizeInBytes = dbDocument.FileSizeInBytes; document.FileStamp = AssetFileStampHelper.ToFileStamp(dbDocument.FileUpdateDate); document.Title = dbDocument.Title; document.VerificationToken = dbDocument.VerificationToken; document.Tags = dbDocument .DocumentAssetTags .Select(t => t.Tag.TagText) .OrderBy(t => t) .ToList(); document.Url = _documentAssetRouteLibrary.DocumentAsset(document); document.DownloadUrl = _documentAssetRouteLibrary.DocumentAssetDownload(document); return(document); }
/// <summary> /// Maps an EF DocumentAsset record from the db into a DocumentAssetDetails /// object. If the db record is null then null is returned. /// </summary> /// <param name="dbDocument">DocumentAsset record from the database.</param> public DocumentAssetRenderDetails Map(DocumentAsset dbDocument) { if (dbDocument == null) { return(null); } var document = new DocumentAssetRenderDetails(); document.DocumentAssetId = dbDocument.DocumentAssetId; document.FileExtension = dbDocument.FileExtension; document.FileName = dbDocument.FileName; document.FileSizeInBytes = dbDocument.FileSizeInBytes; document.Title = dbDocument.Title; document.FileStamp = AssetFileStampHelper.ToFileStamp(dbDocument.FileUpdateDate); document.Description = dbDocument.Description; document.VerificationToken = dbDocument.VerificationToken; document.Url = _documentAssetRouteLibrary.DocumentAsset(document); document.DownloadUrl = _documentAssetRouteLibrary.DocumentAssetDownload(document); return(document); }