コード例 #1
0
        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);
        }
コード例 #2
0
 public SFTPService(
     string host,
     int port,
     string username,
     string password
     )
 {
     _sftpClient = CriaCliente(host, port, username, password);
 }
コード例 #3
0
 public static void CreateDirectoryTree(ISftpClient connection, string parentPath, IEnumerable <string> dirs)
 {
     if (dirs.Any())
     {
         var dir = string.Join("/", parentPath, dirs.First());
         connection.CreateDirectory(dir);
         CreateDirectoryTree(connection, dir, dirs.Skip(1));
     }
 }
コード例 #4
0
        private void Disconnected()
        {
            Receive <Connect>((cmd) =>
            {
                _connection = _clientFactory.CreateSftpClient();
                _connection.Connect();

                Become(Connected);
            });
        }
コード例 #5
0
        private void Disconnected()
        {
            Receive <ListDirectory>((cmd) =>
            {
                this.Stash.Stash();

                _connection = _clientFactory.CreateSftpClient();
                _connection.Connect();

                this.Stash.UnstashAll();
                StartIdlePeriod();
                Become(Connected);
            });
        }
コード例 #6
0
        public static void EnsureParentDirectoryExists(ISftpClient connection, string remotePath)
        {
            var pos = remotePath.LastIndexOf('/');

            if (pos > 0)
            {
                var dir = remotePath.Substring(0, pos);
                if (!connection.DirectoryExists(dir))
                {
                    CreateDirectoryTree(connection, "",
                                        dir.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries));
                }
            }
        }
コード例 #7
0
 public SftpFileSystemProvider(ISftpClient sftpClient)
 {
     _sftpClient = sftpClient;
 }
コード例 #8
0
 private void Connect()
 {
     _connection = _clientFactory.CreateSftpClient();
     _connection.Connect();
 }
コード例 #9
0
 public SftpDelivery(ISftpClient client)
     : this()
 {
     _log.Trace(m => m("Initializing SftpDelivery with an ISftpClient object."));
     _sftp = client;
 }
コード例 #10
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="writeMode"></param>
        /// <returns></returns>
        public override bool Put(DeliveryWriteMode writeMode)
        {
            if (SaveFileCopy)
            {
                string filename = Path.Combine(Context.BaseDirectory, string.Format("SftpDeliveryFile-{0:yyyyMMdd-HHmmss}.csv", DateTime.Now));
                _log.Info(m => m("Saving copy of SFTP upload file to '{0}'", filename));
                Source.ToFile(filename, Source.Length);
            }

            _log.Trace(m => m("Beginning SFTP communication."));
            using (Stream dataStream = new MemoryStream(Source))
            {
                _log.Trace(m => m("Getting SFTP client object..."));
                _sftp = GetClient();

                using (_sftp)
                {
                    try
                    {
                        _log.Debug(m => m("Connecting to SFTP server ({0}@{1})...", Username, Hostname));
                        _sftp.Connect();

                        _log.Trace(m => m("Does file exist at destination (and Overwrite is disabled)?"));
                        if (DeliveryWriteMode.Overwrite != writeMode && _sftp.Exists(Destination))
                        {
                            if (DeliveryWriteMode.Exception == writeMode)
                            {
                                _log.Info(m => m("Destination file exists and Overwrite flag has not been specified: '{0}'", Destination));
                                throw new SftpPermissionDeniedException("File already exists and Overwrite is not enabled.");
                            }
                            // DeliverWriteMode.Ignore
                            _log.Info(m => m("Destination file exists and flag is set to Ignore. Skipping.\n{0}", Destination));
                            return false;
                        }

                        _log.Info(m => m("Uploading file via SFTP ({0}@{1}:{2}) WriteMode: '{3}'", Username, Hostname, Destination, writeMode.ToString("F")));
                        _sftp.UploadFile(dataStream, Destination, (DeliveryWriteMode.Overwrite == writeMode));
                        // if nothing blew up we succeeded (?)
                        return true;
                    }
                    catch (SshConnectionException ex)
                    {
                        _log.Warn(m => m("Unable to establish an SFTP connection to '{0}@{1}'\n{2}", Username, Hostname, ex));
                        throw;
                    }
                    catch(SftpPermissionDeniedException ex)
                    {
                        _log.Warn(m => m("Failed to upload file to '{0}@{1}:{2}'\n{3}", Username, Hostname, Destination, ex));
                        throw;
                    }
                    catch(SshException ex)
                    {
                        _log.Warn(m => m("SSH server returned the following error '{0}'\n{1}", ex.Message, ex));
                        throw;
                    }
                    finally
                    {
                        _log.Debug(m => m("Disconnecting from SFTP server..."));
                        if (_sftp != null && _sftp.IsConnected)
                        {
                            _sftp.Disconnect();
                        }
                    }
                }
            }
            return false;
        }