public void Can_ListDirectory() { var files = _fileSystem.List(_filesPath).ToList(); A.CallTo(() => _fakeSftpClient.ListDirectory(_filesPath, null)).MustHaveHappened(Repeated.Exactly.Once); Assert.Equal(2, files.Count); Assert.Equal("111111", files.First()); Assert.Equal("111111.csv", files.Last()); }
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 <ReceiveTimeout>((cmd) => { if (DateTimeOffset.Now - _idleFromTime > TimeSpan.FromSeconds(ConnectionTimeoutInSeconds)) { StopIdlePeriod(); _connection.Disconnect(); _connection.Dispose(); Become(Disconnected); } }); }
public SftpFileSystemProviderTest() { _assemblyPath = Path.GetDirectoryName(typeof(DotNetFileSystemProviderTest).GetTypeInfo().Assembly.Location); _textFileName = "TextFile1.txt"; _fileToMoveName = "FileToMove.txt"; _localFilesFolder = $"IO{Path.DirectorySeparatorChar}FileSystem{Path.DirectorySeparatorChar}Files"; _filesPath = Path.Combine(_assemblyPath, _localFilesFolder); _originFilesPath = Path.Combine(_assemblyPath, $"IO{Path.DirectorySeparatorChar}FileSystem{Path.DirectorySeparatorChar}OriginFolder"); _fakeSftpClient = A.Fake <ISftpClient>(); A.CallTo(() => _fakeSftpClient.GetWorkingDirectory()).Returns("not empty"); var fakeSftpFile = A.Fake <ISftpFile>(); fakeSftpFile.Name = "111111"; var fakeSftpFile2 = A.Fake <ISftpFile>(); fakeSftpFile2.Name = "111111.csv"; var fakeSftpFiles = new List <ISftpFile> { fakeSftpFile, fakeSftpFile2 }; A.CallTo(() => _fakeSftpClient.ListDirectory(_filesPath, null)).Returns(fakeSftpFiles); A.CallTo(() => _fakeSftpClient.OpenRead(Path.Combine(_filesPath, _textFileName))).Returns(new MemoryStream(Encoding.UTF8.GetBytes("my content"))); A.CallTo(() => _fakeSftpClient.ReadAllText(Path.Combine(_filesPath, _textFileName))).Returns("my content"); A.CallTo(() => _fakeSftpClient.Exists(_filesPath)).Returns(true); _fileSystem = new SftpFileSystemProvider(_fakeSftpClient); }
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(); Utils.EnsureParentDirectoryExists(_connection, cmd.RemotePath); using (var stream = _fileStreamProvider.OpenRead(cmd.LocalPath)) { _connection.UploadFile(stream, cmd.RemotePath, null); } StartIdlePeriod(); }); Receive <DownloadFile>((cmd) => { StopIdlePeriod(); using (var stream = _fileStreamProvider.OpenWrite(cmd.LocalPath)) { _connection.DownloadFile(cmd.RemotePath, stream, null); } StartIdlePeriod(); }); Receive <ReceiveTimeout>((cmd) => { if (DateTimeOffset.Now - _idleFromTime > TimeSpan.FromSeconds(ConnectionTimeoutInSeconds)) { StopIdlePeriod(); _connection.Disconnect(); _connection.Dispose(); Become(Disconnected); } }); }
/// <summary> /// List the files of the given remote path /// </summary> /// <param name="path"> /// the path /// </param> /// <returns> /// the filenames /// </returns> public IEnumerable <string> List(string path) { var response = new List <string>(); var sftpFiles = _sftpClient.ListDirectory(path); foreach (var f in sftpFiles) { response.Add(f.Name); } return(response); }
public async Task <List <SftpFile> > RetornaArquivos(string caminho) { try { var files = new List <SftpFile>(); files = await Task.Run(() => _sftpClient.ListDirectory(caminho).ToList()); return(files); } catch (Exception ex) { throw ex; } }
private void Connected() { Receive <ListDirectory>((cmd) => { IEnumerable <SftpFileInfo> result = null; try { result = _connection.ListDirectory(cmd.RemotePath, null); } catch (Exception) { result = new SftpFileInfo[] { }; } this.Sender.Tell(result); }); Receive <Disconnect>((cmd) => { _connection.Disconnect(); _connection.Dispose(); Become(Disconnected); }); }
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); } }); }