Exemple #1
0
        private async Task <StepResult> DoCopySingleFileAsync(CopyFileStep step)
        {
            byte[] sourceContents;
            /* Read source file */
            if (step.Direction == FileCopyDirection.RemoteToLocal)
            {
                var command = new FetchResultRange {
                    FilePath = new[] { step.SourcePath }
                };
                var response = await _channel.SendWithReplyAsync <ResultRangeFetched>(command, _controller.CancellationToken);

                if (response.Status == FetchStatus.FileNotFound)
                {
                    return(new StepResult(false, $"File is not found on the remote machine at {step.SourcePath}", ""));
                }
                sourceContents = response.Data;
            }
            else if (!ReadLocalFile(step.SourcePath, out sourceContents, out var error))
            {
                return(new StepResult(false, error, ""));
            }
            /* Write target file */
            if (step.Direction == FileCopyDirection.LocalToRemote)
            {
                ICommand command = new PutFileCommand {
                    Data = sourceContents, Path = step.TargetPath
                };
                if (step.UseCompression)
                {
                    command = new CompressedCommand(command);
                }
                var response = await _channel.SendWithReplyAsync <PutFileResponse>(command, _controller.CancellationToken);

                if (response.Status == PutFileStatus.PermissionDenied)
                {
                    return(new StepResult(false, $"Access to path {step.TargetPath} on the remote machine is denied", ""));
                }
                if (response.Status == PutFileStatus.OtherIOError)
                {
                    return(new StepResult(false, $"File {step.TargetPath} could not be created on the remote machine", ""));
                }
            }
            else
            {
                try
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(step.TargetPath));
                    File.WriteAllBytes(step.TargetPath, sourceContents);
                }
                catch (UnauthorizedAccessException)
                {
                    return(new StepResult(false, $"Access to path {step.TargetPath} on the local machine is denied", ""));
                }
            }

            return(new StepResult(true, "", ""));
        }
        public async Task <IActionResult> PutString(PutFileCommand command)
        {
            await Mediator.Send(command);

            return(Ok());
        }
 public PutFileHandler(PutFileCommand command)
 {
     _command = command;
 }