コード例 #1
0
        /// <summary>
        /// Check if loglevel is enabled
        /// </summary>
        /// <param name="level"></param>
        /// <returns></returns>
        public bool IsEnabledFor(LogLevel level)
        {
            switch (level)
            {
            case LogLevel.Off:
                return(false);

            case LogLevel.Fatal:
                return(IsFatalEnabled);

            case LogLevel.Error:
                return(IsErrorEnabled);

            case LogLevel.Warn:
                return(IsWarnEnabled);

            case LogLevel.Info:
                return(IsInfoEnabled);

            case LogLevel.Debug:
                return(IsDebugEnabled);

            default:
                throw UnexpectedEnumValueException.Create(level);
            }
        }
コード例 #2
0
        private void Log(LogLevel level, string message, Exception exception)
        {
            switch (level)
            {
            case LogLevel.Off:
                break;

            case LogLevel.Fatal:
                _logger.Fatal(message, exception);
                break;

            case LogLevel.Error:
                _logger.Error(message, exception);
                break;

            case LogLevel.Warn:
                _logger.Warn(message, exception);
                break;

            case LogLevel.Info:
                _logger.Info(message, exception);
                break;

            case LogLevel.Debug:
                _logger.Debug(message, exception);
                break;

            default:
                throw UnexpectedEnumValueException.Create(level);
            }
        }
コード例 #3
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);
        }
        /// <summary>
        /// Handles downloading the integration export
        /// </summary>
        /// <param name="command">The command</param>
        /// <param name="cancellationToken">The cancellation token</param>
        /// <returns>The response</returns>
        public async Task <DownloadIntegrationExportCommandResult> Handle(
            DownloadIntegrationExportCommand command,
            CancellationToken cancellationToken)
        {
            Argument.NotNull(command, nameof(command));

            var commandResult = new DownloadIntegrationExportCommandResult();

            var configuration = command.Configuration;

            Require.NotNull(configuration, nameof(configuration));

            var request = new DownloadIntegrationExportV1Request
            {
                IntegrationExportId = command.IntegrationExportId,
            };

            var response = await _client
                           .Execute(request, cancellationToken)
                           .ConfigureAwait(Await.Default);

            if (response.IsSuccessful)
            {
                using (var model = response.Model)
                {
                    var saveCommand = new SaveIntegrationExportFileCommand
                    {
                        IntegrationExportId = command.IntegrationExportId,
                        File              = model.File,
                        FileName          = model.FileName,
                        FileSize          = model.Size,
                        DestinationFolder = configuration.Folder,
                        BackupFolder      = configuration.BackupFolder,
                        Overwrite         = configuration.Overwrite,
                    };

                    var saveCommandResult = await _handler
                                            .Handle(saveCommand, cancellationToken)
                                            .ConfigureAwait(Await.Default);

                    switch (saveCommandResult.Result)
                    {
                    case SaveIntegrationExportFileCommandResultKind.Success:
                        commandResult.Success = true;   // TODO: ??
                        commandResult.Message = "";     // TODO: add backup file name and size
                        break;

                    case SaveIntegrationExportFileCommandResultKind.Failed:
                        commandResult.Success = false;
                        commandResult.Message = "";     // TODO:
                        break;

                    default:
                        throw UnexpectedEnumValueException.Create(saveCommandResult.Result);
                    }
                }
            }
            else
            {
                commandResult.Success = false;
                commandResult.Message = "Error downloading integration export";
            }

            return(commandResult);
        }