Ejemplo n.º 1
0
        /// <summary>
        /// Handles saving the file and moving it to the correct place
        /// </summary>
        /// <param name="command">The command</param>
        /// <param name="cancellationToken">The cancellation token</param>
        /// <returns></returns>
        public async Task <SaveIntegrationExportFileCommandResult> Handle(
            SaveIntegrationExportFileCommand command,
            CancellationToken cancellationToken)
        {
            Argument.NotNull(command, nameof(command));

            var commandResult = new SaveIntegrationExportFileCommandResult();

            var factoryCommand = new SaveIntegrationExportFileFactoryCommand
            {
                IntegrationExportId     = command.IntegrationExportId,
                IntegrationFolder       = command.DestinationFolder,
                FileName                = command.FileName,
                IntegrationBackupFolder = command.BackupFolder,
            };

            var factoryResult = _factory.Create(factoryCommand);

            var writeFileCommand = new WriteFileFromStreamCommand
            {
                File     = command.File,
                FileName = factoryResult.DownloadFileName,
                Path     = factoryResult.DownloadFolder,
                Size     = command.FileSize
            };

            var writeFileResult = await _writeFileHandler
                                  .Handle(writeFileCommand, cancellationToken)
                                  .ConfigureAwait(Await.Default);

            if (writeFileResult.Result == WriteFileFromStreamCommandResultKind.Success)
            {
                var moveFileCommand = new MoveFileCommand
                {
                    Source      = factoryResult.DownloadPath,
                    Destination = factoryResult.IntegrationPath,
                    Overwrite   = command.Overwrite,
                    Backup      = factoryResult.BackupPath
                };

                var moveFileResult = await _moveFileHandler
                                     .Handle(moveFileCommand, cancellationToken)
                                     .ConfigureAwait(Await.Default);

                commandResult.FileOverwritten = moveFileResult.FileOverwritten;
                commandResult.BackupFileName  = factoryResult.BackupFileName;
                commandResult.BackupFileSize  = moveFileResult.BackupFileSize;
                commandResult.Result          = SaveIntegrationExportFileCommandResultKind.Success;
            }
            else
            {
                commandResult.Result = SaveIntegrationExportFileCommandResultKind.Failed;
            }

            return(commandResult);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Handle the command
        /// </summary>
        /// <param name="command">The command</param>
        /// <param name="cancellationToken">The cancellation token</param>
        /// <returns>The command result</returns>
        public async Task <QueueIntegrationImportCommandResult> Handle(
            QueueIntegrationImportCommand command,
            CancellationToken cancellationToken)
        {
            Argument.NotNull(command, nameof(command));

            var commandResult = new QueueIntegrationImportCommandResult();

            if (_dataContext.IntegrationImports.TryGetValue(command.IntegrationId, out var integrationImport) == false)
            {
                integrationImport = new IntegrationImport
                {
                    IntegrationId = command.IntegrationId,
                };

                _dataContext.IntegrationImports.Add(command.IntegrationId, integrationImport);
            }

            var fileInfo = _fileInfoFactory.FromFileName(command.File);

            if (fileInfo.Exists)
            {
                var versionId = _guidProvider.NewGuid();

                string extension = fileInfo.Extension;

                var now = _dateTimeProvider.UtcNow;

                string queueFolder = _configuration.Value.QueueFolder;

                string orignalFileName = fileInfo.Name;

                var fileDate = new DateTimeOffset(fileInfo.LastWriteTimeUtc, TimeSpan.Zero);

                string fileName = string.Format(
                    CultureInfo.InvariantCulture,
                    "{0}_{1:yyyyMMddHHmmssfff}_{2}{3}",
                    command.IntegrationId,
                    now,
                    versionId,
                    extension);

                var filePath = _path.Combine(queueFolder, fileName);

                long size = fileInfo.Length;

                if (command.MoveFile == true)
                {
                    fileInfo.MoveTo(filePath);

                    var queueItem = new IntegrationImportQueueItem
                    {
                        IntegrationId = command.IntegrationId,
                        File          = filePath,
                        FileName      = orignalFileName,
                        FileSize      = size,
                        FileDate      = fileDate,
                        Hash          = null,
                        FileVersionId = versionId,
                    };

                    _dataContext.IntegrationImportQueue.Add(queueItem);
                }
                else
                {
                    using (var stream = fileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        var query = new HashFileQuery
                        {
                            File = stream
                        };

                        var queryResult = await _queryHandler
                                          .Handle(query, cancellationToken)
                                          .ConfigureAwait(Await.Default);

                        _ = stream.Seek(0, SeekOrigin.Begin);

                        if (string.Equals(integrationImport.Hash, queryResult.Hash, StringComparison.Ordinal) == false)
                        {
                            integrationImport.Hash = queryResult.Hash;
                            integrationImport.Size = size;

                            var writeCommand = new WriteFileFromStreamCommand
                            {
                                Path     = queueFolder,
                                FileName = fileName,
                                File     = stream,
                                Size     = size
                            };

                            var writeCommandResult = await _commandHandler
                                                     .Handle(writeCommand, cancellationToken)
                                                     .ConfigureAwait(Await.Default);

                            switch (writeCommandResult.Result)
                            {
                            case WriteFileFromStreamCommandResultKind.Success:
                                break;

                            default:
                                throw UnexpectedEnumValueException.Create(writeCommandResult.Result);
                            }

                            string hash = queryResult.Hash;

                            var queueItem = new IntegrationImportQueueItem
                            {
                                IntegrationId = command.IntegrationId,
                                File          = filePath,
                                FileName      = orignalFileName,
                                FileSize      = size,
                                FileDate      = fileDate,
                                Hash          = hash,
                                FileVersionId = versionId,
                            };

                            _dataContext.IntegrationImportQueue.Add(queueItem);
                        }
                    }
                }
            }

            return(commandResult);
        }