Exemplo n.º 1
0
        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);
        }
Exemplo n.º 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.SslProtocols         = (SslProtocols)ftpConfiguration.SslProtocols;
                _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);
                }
            }
        }
Exemplo n.º 3
0
        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));
                }
            }
        }
Exemplo n.º 4
0
        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));
                }
            }
        }
Exemplo n.º 5
0
        public SftpSession(FtpConfiguration ftpConfiguration)
        {
            if (ftpConfiguration == null)
            {
                throw new ArgumentNullException(nameof(ftpConfiguration));
            }

            ConnectionInfo connectionInfo = null;

            var authMethods = new List <AuthenticationMethod>();

            //Add password authentication method if password is provided
            if (!String.IsNullOrEmpty(ftpConfiguration.Password))
            {
                authMethods.Add(new PasswordAuthenticationMethod(ftpConfiguration.Username, ftpConfiguration.Password));
            }

            //Add private key authentication method if private key is provided
            if (!String.IsNullOrEmpty(ftpConfiguration.ClientCertificatePath))
            {
                PrivateKeyFile keyFile  = new PrivateKeyFile(ftpConfiguration.ClientCertificatePath, ftpConfiguration.ClientCertificatePassword);
                var            keyFiles = new[] { keyFile };
                authMethods.Add(new PrivateKeyAuthenticationMethod(ftpConfiguration.Username, keyFiles));
            }

            //Throw an error if we ended up with no authentication method
            if (authMethods.Count == 0)
            {
                throw new ArgumentNullException(Resources.NoValidAuthenticationMethod);
            }

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

            _sftpClient = new SftpClient(connectionInfo);
        }
Exemplo n.º 6
0
        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);
        }