예제 #1
0
 /// <summary>
 /// Writes lines to a remote file
 /// </summary>
 /// <param name="path">
 /// the path
 /// </param>
 /// <param name="contents">
 /// the lines to write
 /// </param>
 public void WriteAllLines(string path, IEnumerable <string> contents)
 {
     using (Stream s = GenerateStreamFromStrings(contents))
     {
         _sftpClient.BeginUploadFile(s, path);
     }
 }
예제 #2
0
        public void Can_WriteAllLinesToAFile()
        {
            var text = new List <string>
            {
                "my text",
                "lines"
            };
            var path = Path.Combine(_filesPath, "testFile.txt");

            _fileSystem.WriteAllLines(path, text);

            A.CallTo(() => _fakeSftpClient.BeginUploadFile(A <Stream> ._, path)).MustHaveHappened(Repeated.Exactly.Once);
        }
        private void Connected()
        {
            Receive <ListDirectory>((cmd) =>
            {
                StopIdlePeriod();

                IEnumerable <SftpFileInfo> result = null;
                try
                {
                    result = _connection.ListDirectory(cmd.RemotePath, null);
                }
                catch (Exception)
                {
                    result = new SftpFileInfo[] { };
                }
                this.Sender.Tell(result, Self);

                StartIdlePeriod();
            });

            Receive <UploadFile>((cmd) =>
            {
                StopIdlePeriod();

                AsyncCallback callback = ar =>
                {
                    try
                    {
                        _connection.EndUploadFile(ar);
                        var result = _clientFactory.CreateSftpAsyncResult(ar);
                        if (result.IsCanceled)
                        {
                            this.Self.Tell(new Cancelled());
                        }
                        else
                        {
                            this.Self.Tell(new Completed());
                        }
                    }
                    catch (Exception ex)
                    {
                        this.Self.Tell(new Error(ex.Message));
                    }
                };
                Utils.EnsureParentDirectoryExists(_connection, cmd.RemotePath);
                _stream      = _fileStreamProvider.OpenRead(cmd.LocalPath);
                _asyncResult = _connection.BeginUploadFile(_stream, cmd.RemotePath, callback, null);

                Become(Transferring);
            });

            Receive <DownloadFile>((cmd) =>
            {
                StopIdlePeriod();

                AsyncCallback callback = ar =>
                {
                    try
                    {
                        _connection.EndDownloadFile(ar);
                        var result = _clientFactory.CreateSftpAsyncResult(ar);
                        if (result.IsCanceled)
                        {
                            this.Self.Tell(new Cancelled());
                        }
                        else
                        {
                            this.Self.Tell(new Completed());
                        }
                    }
                    catch (Exception ex)
                    {
                        this.Self.Tell(new Error(ex.Message));
                    }
                };
                _stream      = _fileStreamProvider.OpenWrite(cmd.LocalPath);
                _asyncResult = _connection.BeginDownloadFile(cmd.RemotePath, _stream, callback, null);

                Become(Transferring);
            });

            Receive <ReceiveTimeout>((cmd) =>
            {
                if (DateTimeOffset.Now - _idleFromTime > TimeSpan.FromSeconds(ConnectionTimeoutInSeconds))
                {
                    StopIdlePeriod();

                    _connection.Disconnect();
                    _connection.Dispose();

                    Become(Disconnected);
                }
            });
        }