예제 #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 bool DirectoryExists(string path)
 {
     return(_sftpClient.Exists(path));
 }
예제 #3
0
 public void Can_DetermineIfDirectoryExists()
 {
     Assert.True(_fileSystem.DirectoryExists(_filesPath));
     A.CallTo(() => _fakeSftpClient.Exists(_filesPath)).MustHaveHappened(Repeated.Exactly.Once);
 }
예제 #4
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;
        }