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();
            }
        }
예제 #2
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();
                }
            }
        }
예제 #3
0
        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();
            }
        }
        private void ValidateFileType(AddImageAssetCommand command)
        {
            var contentType = _mimeTypeService.MapFromFileName(command.File.FileName, command.File.MimeType);

            _assetFileTypeValidator.ValidateAndThrow(command.File.FileName, contentType, nameof(command.File));
        }