private IEnumerable <Tuple <string, string> > GetLocalListing(string localPath, string remotePath, bool recursive)
        {
            if (string.IsNullOrWhiteSpace(localPath))
            {
                throw new ArgumentNullException(nameof(localPath));
            }
            if (string.IsNullOrWhiteSpace(remotePath))
            {
                throw new ArgumentNullException(nameof(remotePath));
            }

            List <Tuple <string, string> > listing = new List <Tuple <string, string> >();
            DirectoryInfo currentDirectory         = new DirectoryInfo(localPath);
            string        nextRemotePath           = FtpConfiguration.CombinePaths(remotePath, currentDirectory.Name);

            foreach (FileInfo fileInfo in currentDirectory.EnumerateFiles())
            {
                listing.Add(new Tuple <string, string>(fileInfo.FullName, FtpConfiguration.CombinePaths(nextRemotePath, fileInfo.Name)));
            }

            if (recursive)
            {
                foreach (DirectoryInfo directoryInfo in currentDirectory.EnumerateDirectories())
                {
                    listing.AddRange(GetLocalListing(directoryInfo.FullName, nextRemotePath, recursive));
                }
            }

            return(listing);
        }
Example #2
0
        public FtpSession(FtpConfiguration ftpConfiguration, FtpsMode ftpsMode)
        {
            if (ftpConfiguration == null)
            {
                throw new ArgumentNullException(nameof(ftpConfiguration));
            }

            _ftpClient = new FtpClient(ftpConfiguration.Host);

            if (ftpConfiguration.Port != null)
            {
                _ftpClient.Port = ftpConfiguration.Port.Value;
            }
            if (ftpConfiguration.UseAnonymousLogin == false)
            {
                _ftpClient.Credentials = new NetworkCredential(ftpConfiguration.Username, ftpConfiguration.Password);
            }

            switch (ftpsMode)
            {
            case FtpsMode.Explicit:
                _ftpClient.EncryptionMode = FtpEncryptionMode.Explicit;
                break;

            case FtpsMode.Implicit:
                _ftpClient.EncryptionMode = FtpEncryptionMode.Implicit;
                break;

            case FtpsMode.None:
                _ftpClient.EncryptionMode = FtpEncryptionMode.None;
                break;

            default:
                throw new InvalidOperationException(Resources.UnknownFTPSModeException);
            }

            if (ftpsMode != FtpsMode.None)
            {
                _ftpClient.ValidateCertificate += (control, e) => _ftpClient_ValidateCertificate(control, e, ftpConfiguration.AcceptAllCertificates);

                if (string.IsNullOrWhiteSpace(ftpConfiguration.ClientCertificatePath) == false)
                {
                    X509Certificate2 clientCertificate;
                    if (ftpConfiguration.ClientCertificatePassword == null)
                    {
                        clientCertificate = new X509Certificate2(ftpConfiguration.ClientCertificatePath);
                    }
                    else
                    {
                        clientCertificate = new X509Certificate2(ftpConfiguration.ClientCertificatePath, ftpConfiguration.ClientCertificatePassword);
                    }

                    _ftpClient.ClientCertificates.Add(clientCertificate);
                }
            }
        }
        async Task IFtpSession.UploadAsync(string localPath, string remotePath, bool overwrite, bool recursive, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(localPath))
            {
                throw new ArgumentNullException(nameof(localPath));
            }
            if (string.IsNullOrWhiteSpace(remotePath))
            {
                throw new ArgumentNullException(nameof(remotePath));
            }
            if (cancellationToken == null)
            {
                throw new ArgumentNullException(nameof(cancellationToken));
            }

            if (Directory.Exists(localPath))
            {
                IEnumerable <Tuple <string, string> > listing = GetLocalListing(localPath, remotePath, recursive);

                foreach (Tuple <string, string> pair in listing)
                {
                    cancellationToken.ThrowIfCancellationRequested();

                    string directoryPath = FtpConfiguration.GetDirectoryPath(pair.Item2);
                    if (!_sftpClient.Exists(directoryPath))
                    {
                        _sftpClient.CreateDirectory(directoryPath);
                    }

                    using (Stream fileStream = File.OpenRead(pair.Item1))
                    {
                        await Task.Factory.FromAsync(_sftpClient.BeginUploadFile(fileStream, pair.Item2, overwrite, null, null), _sftpClient.EndUploadFile);
                    }
                }
            }
            else
            {
                if (File.Exists(localPath))
                {
                    if (_sftpClient.Exists(remotePath) && !overwrite)
                    {
                        throw new IOException(Resources.FileExistsException);
                    }

                    using (Stream fileStream = File.OpenRead(localPath))
                    {
                        await Task.Factory.FromAsync(_sftpClient.BeginUploadFile(fileStream, remotePath, overwrite, null, null), _sftpClient.EndUploadFile);
                    }
                }
                else
                {
                    throw new ArgumentException(string.Format(Resources.PathNotFoundException, localPath), nameof(localPath));
                }
            }
        }
        void IFtpSession.Upload(string localPath, string remotePath, bool overwrite, bool recursive)
        {
            if (string.IsNullOrWhiteSpace(localPath))
            {
                throw new ArgumentNullException(nameof(localPath));
            }
            if (string.IsNullOrWhiteSpace(remotePath))
            {
                throw new ArgumentNullException(nameof(remotePath));
            }

            if (Directory.Exists(localPath))
            {
                IEnumerable <Tuple <string, string> > listing = GetLocalListing(localPath, remotePath, recursive);

                foreach (Tuple <string, string> pair in listing)
                {
                    string directoryPath = FtpConfiguration.GetDirectoryPath(pair.Item2);
                    if (!_sftpClient.Exists(directoryPath))
                    {
                        _sftpClient.CreateDirectory(directoryPath);
                    }

                    using (Stream fileStream = File.OpenRead(pair.Item1))
                    {
                        _sftpClient.UploadFile(fileStream, pair.Item2, overwrite);
                    }
                }
            }
            else
            {
                if (File.Exists(localPath))
                {
                    if (_sftpClient.Exists(remotePath) && !overwrite)
                    {
                        throw new IOException(Resources.FileExistsException);
                    }

                    using (Stream fileStream = File.OpenRead(localPath))
                    {
                        _sftpClient.UploadFile(fileStream, remotePath, overwrite);
                    }
                }
                else
                {
                    throw new ArgumentException(string.Format(Resources.PathNotFoundException, localPath), nameof(localPath));
                }
            }
        }
        public SftpSession(FtpConfiguration ftpConfiguration)
        {
            if (ftpConfiguration == null)
            {
                throw new ArgumentNullException(nameof(ftpConfiguration));
            }

            ConnectionInfo connectionInfo = null;

            if (ftpConfiguration.Port == null)
            {
                connectionInfo = new ConnectionInfo(ftpConfiguration.Host, ftpConfiguration.Username, new PasswordAuthenticationMethod(ftpConfiguration.Username, ftpConfiguration.Password));
            }
            else
            {
                connectionInfo = new ConnectionInfo(ftpConfiguration.Host, ftpConfiguration.Port.Value, ftpConfiguration.Username, new PasswordAuthenticationMethod(ftpConfiguration.Username, ftpConfiguration.Password));
            }

            _sftpClient = new SftpClient(connectionInfo);
        }