コード例 #1
0
 private Task OnTransactionComplete(DocumentAsset documentAsset)
 {
     return(_messageAggregator.PublishAsync(new DocumentAssetAddedMessage()
     {
         DocumentAssetId = documentAsset.DocumentAssetId
     }));
 }
コード例 #2
0
        public async Task SaveFile(IUploadedFile uploadedFile, DocumentAsset documentAsset)
        {
            using (var inputSteam = await uploadedFile.OpenReadStreamAsync())
            {
                bool isNew = documentAsset.DocumentAssetId < 1;

                documentAsset.FileExtension   = Path.GetExtension(uploadedFile.FileName).TrimStart('.');
                documentAsset.FileSizeInBytes = Convert.ToInt32(inputSteam.Length);
                documentAsset.ContentType     = uploadedFile.MimeType;

                // Save at this point if it's a new image
                if (isNew)
                {
                    await _dbContext.SaveChangesAsync();
                }

                var fileName = Path.ChangeExtension(documentAsset.DocumentAssetId.ToString(), documentAsset.FileExtension);

                using (var scope = _transactionScopeFactory.Create(_dbContext))
                {
                    // Save the raw file directly
                    await CreateFileAsync(isNew, fileName, inputSteam);

                    if (!isNew)
                    {
                        await _dbContext.SaveChangesAsync();
                    }

                    await scope.CompleteAsync();
                }
            }
        }
コード例 #3
0
        public async Task ExecuteAsync(AddDocumentAssetCommand command, IExecutionContext executionContext)
        {
            var documentAsset = new DocumentAsset();

            documentAsset.Title             = command.Title;
            documentAsset.Description       = command.Description ?? string.Empty;
            documentAsset.FileName          = FilePathHelper.CleanFileName(command.Title);
            documentAsset.VerificationToken = _randomStringGenerator.Generate(6);

            if (string.IsNullOrWhiteSpace(documentAsset.FileName))
            {
                throw ValidationErrorException.CreateWithProperties("Document title is empty or does not contain any safe file path characters.", nameof(command.Title));
            }

            _entityTagHelper.UpdateTags(documentAsset.DocumentAssetTags, command.Tags, executionContext);
            _entityAuditHelper.SetCreated(documentAsset, executionContext);
            documentAsset.FileUpdateDate = executionContext.ExecutionDate;

            using (var scope = _transactionScopeFactory.Create(_dbContext))
            {
                _dbContext.DocumentAssets.Add(documentAsset);

                await _documentAssetCommandHelper.SaveFile(command.File, documentAsset);

                command.OutputDocumentAssetId = documentAsset.DocumentAssetId;

                scope.QueueCompletionTask(() => OnTransactionComplete(documentAsset));

                await scope.CompleteAsync();
            }
        }
コード例 #4
0
        public async Task ExecuteAsync(AddDocumentAssetCommand command, IExecutionContext executionContext)
        {
            var documentAsset = new DocumentAsset();

            documentAsset.Title       = command.Title;
            documentAsset.Description = command.Description ?? string.Empty;
            documentAsset.FileName    = SlugFormatter.ToSlug(command.Title);
            _entityTagHelper.UpdateTags(documentAsset.DocumentAssetTags, command.Tags, executionContext);
            _entityAuditHelper.SetCreated(documentAsset, executionContext);

            using (var scope = _transactionScopeFactory.Create(_dbContext))
            {
                _dbContext.DocumentAssets.Add(documentAsset);

                await _documentAssetCommandHelper.SaveFile(command.File, documentAsset);

                command.OutputDocumentAssetId = documentAsset.DocumentAssetId;
                scope.Complete();
            }

            await _messageAggregator.PublishAsync(new DocumentAssetAddedMessage()
            {
                DocumentAssetId = documentAsset.DocumentAssetId
            });
        }
コード例 #5
0
 private Task OnTransactionComplete(DocumentAsset documentAsset, bool hasNewFile)
 {
     return(_messageAggregator.PublishAsync(new DocumentAssetUpdatedMessage()
     {
         DocumentAssetId = documentAsset.DocumentAssetId,
         HasFileChanged = hasNewFile
     }));
 }
コード例 #6
0
        /// <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 DocumentAssetSummary Map(DocumentAsset dbDocument)
        {
            if (dbDocument == null)
            {
                return(null);
            }

            var document = new DocumentAssetSummary();

            Map(document, dbDocument);

            return(document);
        }
コード例 #7
0
 private void ValidateFileType(DocumentAsset file)
 {
     // TODO: this i think should be wrapped in a IAssetFileValidator service
     // Asset file settings to disable checking
     // configurable blacklist or whitelist?
     if (DangerousFileConstants.DangerousFileExtensions.Contains(file.FileExtension))
     {
         throw new PropertyValidationException("The type of file you're trying to upload isn't allowed.", "File");
     }
     else if (string.IsNullOrWhiteSpace(file.ContentType) || DangerousFileConstants.DangerousMimeTypes.Contains(file.ContentType))
     {
         throw new PropertyValidationException("The type of file you're trying to upload isn't allowed.", "File");
     }
 }
コード例 #8
0
        /// <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 DocumentAssetDetails Map(DocumentAsset dbDocument)
        {
            if (dbDocument == null)
            {
                return(null);
            }

            var document = new DocumentAssetDetails();

            _documentAssetSummaryMapper.Map(document, dbDocument);
            document.Description = dbDocument.Description;

            return(document);
        }
コード例 #9
0
        /// <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 DocumentAssetDetails Map(DocumentAsset dbDocument)
        {
            if (dbDocument == null)
            {
                return(null);
            }

            var document = new DocumentAssetDetails();

            _documentAssetSummaryMapper.Map(document, dbDocument);
            document.Description    = dbDocument.Description;
            document.FileUpdateDate = DbDateTimeMapper.AsUtc(dbDocument.FileUpdateDate);

            return(document);
        }
コード例 #10
0
        /// <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.Title           = dbDocument.Title;
            document.Tags            = dbDocument
                                       .DocumentAssetTags
                                       .Select(t => t.Tag.TagText)
                                       .OrderBy(t => t)
                                       .ToList();

            return(document);
        }
コード例 #11
0
        /// <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.Description     = dbDocument.Description;

            return(document);
        }
コード例 #12
0
        public void CallingPostAssetsReturnsCreatedResponse()
        {
            var sut         = new AssetsController();
            var assetToPost = new DocumentAsset
            {
                Identifier   = "54321",
                DocumentType = "Doc Type"
            };
            var result = sut.Post(assetToPost.SerialiseToJson());

            Assert.NotNull(result);
            Assert.IsInstanceOf <IActionResult>(result);
            Assert.IsInstanceOf <ObjectResult>(result);

            var actionResult = result as ObjectResult;

            Assert.AreEqual(201, actionResult.StatusCode);
            StringAssert.Contains("\"identifier\":\"54321\"", actionResult.Value.ToString());
        }
コード例 #13
0
        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();
                }
            }
        }
コード例 #14
0
        /// <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);
        }
コード例 #15
0
        /// <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);
        }
コード例 #16
0
        private static Asset DummyAsset(string assetType)
        {
            Asset asset;

            switch (assetType)
            {
            case "video":
                asset = new VideoAsset
                {
                    ActivePictureFormat   = "APF",
                    BarCode               = "BC",
                    CloseCaptionAvailable = false,
                    Codec             = "CODEC",
                    Container         = "C",
                    FrameRate         = "FR",
                    ImageAspectRatio  = "16:9",
                    LineStandard      = "LS",
                    MusicAndEffects   = false,
                    RunTimeInSeconds  = 0,
                    ScreenAspectRatio = "14:9",
                    Standard          = "S",
                    TextedLanguage    = "TL"
                };
                break;

            default:
                asset = new DocumentAsset
                {
                    DocumentType = "Doc Type"
                };
                break;
            }

            asset.Identifier = $"Dummy {assetType} Id";
            asset.Name       = $"{assetType} name";
            asset.Source     = new Source {
                Identifier = "Source System Id", System = "Source System Name"
            };
            asset.Content = new Content {
                Identifier = "Content Id", Code = "Content Code", Type = "Content Type"
            };
            asset.Title       = $"{assetType} title";
            asset.Description = $"{assetType} description";
            asset.Status      = "Dummy";
            asset.Sensitive   = false;
            asset.Keywords    = new List <Keyword>
            {
                new Keyword {
                    Name = "Keyword 1 Name", Value = "Keyword 1 Value"
                },
                new Keyword {
                    Name = "Keyword 2 Name", Value = "Keyword 2 Value"
                }
            };
            asset.Location = new Location
            {
                Path       = "//dummy/asset.mpg",
                FileSize   = "Dummy Asset File Size",
                Format     = "Dummy Asset Format",
                Renditions = new List <Rendition> {
                    new Rendition {
                        Type = "Dummy Asset Rendition Type"
                    }
                }
            };

            return(asset);
        }