/// <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 void CreateFileStamp_WithValidData_CreatesCorrectFileStamp(string fileUpdateDate, string expected) { var dateToTest = DateTime.Parse(fileUpdateDate); var result = AssetFileStampHelper.ToFileStamp(dateToTest); Assert.Equal(expected, result); }
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 ExecuteAsync(AddImageAssetCommand command, IExecutionContext executionContext) { ValidateFileType(command); 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); 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> /// 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); }
/// <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); }
public void CreateFileStamp_WhenPriorToEpoch_Throws(string fileUpdateDate) { var dateToTest = DateTime.Parse(fileUpdateDate); Assert.Throws <ArgumentException>(() => AssetFileStampHelper.ToFileStamp(dateToTest)); }