Пример #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);
        }
Пример #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("UnknownFTPSModeException");
            }


            if (ftpsMode != FtpsMode.None)
            {
                ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate;

                _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);
                }
            }
        }
Пример #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 (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("FileExistsException");
                    }

                    using (Stream fileStream = File.OpenRead(localPath))
                    {
                        await Task.Factory.FromAsync(_sftpClient.BeginUploadFile(fileStream, remotePath, overwrite, null, null), _sftpClient.EndUploadFile);
                    }
                }
                else
                {
                    throw new ArgumentException("PathNotFoundException", (localPath));
                }
            }
        }
Пример #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("FileExistsException");
                    }

                    using (Stream fileStream = File.OpenRead(localPath))
                    {
                        _sftpClient.UploadFile(fileStream, remotePath, overwrite);
                    }
                }
                else
                {
                    throw new ArgumentException("PathNotFoundException", (localPath));
                }
            }
        }
Пример #5
0
        protected override async Task <Action <NativeActivityContext> > ExecuteAsync(NativeActivityContext context, CancellationToken cancellationToken)
        {
            IFtpSession      ftpSession       = null;
            FtpConfiguration ftpConfiguration = new FtpConfiguration(Host.Get(context));

            ftpConfiguration.Port = Port.Expression == null ? null : (int?)Port.Get(context);
            ftpConfiguration.UseAnonymousLogin         = UseAnonymousLogin;
            ftpConfiguration.ClientCertificatePath     = ClientCertificatePath.Get(context);
            ftpConfiguration.ClientCertificatePassword = ClientCertificatePassword.Get(context);
            ftpConfiguration.AcceptAllCertificates     = AcceptAllCertificates;

            if (ftpConfiguration.UseAnonymousLogin == false)
            {
                ftpConfiguration.Username = Username.Get(context);
                ftpConfiguration.Password = Password.Get(context);

                if (string.IsNullOrWhiteSpace(ftpConfiguration.Username))
                {
                    throw new ArgumentNullException("EmptyUsernameException");
                }
            }

            if (UseSftp)
            {
                ftpSession = new SftpSession(ftpConfiguration);
            }
            else
            {
                ftpSession = new FtpSession(ftpConfiguration, FtpsMode);
            }

            await ftpSession.OpenAsync(cancellationToken);

            return((nativeActivityContext) =>
            {
                if (Body != null)
                {
                    _ftpSession = ftpSession;
                    nativeActivityContext.ScheduleAction(Body, ftpSession, OnCompleted, OnFaulted);
                }
            });
        }
Пример #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);
        }
Пример #7
0
        protected override async Task <Action <AsyncCodeActivityContext> > ExecuteAsync(AsyncCodeActivityContext context, CancellationToken cancellationToken)
        {
            PropertyDescriptor ftpSessionProperty = context.DataContext.GetProperties()[WithFtpSession.FtpSessionPropertyName];
            IFtpSession        ftpSession         = ftpSessionProperty?.GetValue(context.DataContext) as IFtpSession;

            if (ftpSession == null)
            {
                throw new InvalidOperationException("FTPSessionNotFoundException");
            }

            string localPath  = LocalPath.Get(context);
            string remotePath = RemotePath.Get(context);

            if (Directory.Exists(localPath))
            {
                if (string.IsNullOrWhiteSpace(Path.GetExtension(remotePath)))
                {
                    if (!(await ftpSession.DirectoryExistsAsync(remotePath, cancellationToken)))
                    {
                        if (Create)
                        {
                            await ftpSession.CreateDirectoryAsync(remotePath, cancellationToken);
                        }
                        else
                        {
                            throw new ArgumentException("PathNotFoundException", remotePath);
                        }
                    }
                }
                else
                {
                    throw new InvalidOperationException("IncompatiblePathsException");
                }
            }
            else
            {
                if (File.Exists(localPath))
                {
                    if (string.IsNullOrWhiteSpace(Path.GetExtension(remotePath)))
                    {
                        remotePath = FtpConfiguration.CombinePaths(remotePath, Path.GetFileName(localPath));
                    }

                    string directoryPath = FtpConfiguration.GetDirectoryPath(remotePath);

                    if (!(await ftpSession.DirectoryExistsAsync(directoryPath, cancellationToken)))
                    {
                        if (Create)
                        {
                            await ftpSession.CreateDirectoryAsync(directoryPath, cancellationToken);
                        }
                        else
                        {
                            throw new InvalidOperationException(directoryPath);
                        }
                    }
                }
                else
                {
                    throw new ArgumentException("PathNotFoundException", localPath);
                }
            }

            await ftpSession.UploadAsync(localPath, remotePath, Overwrite, Recursive, cancellationToken);

            return((asyncCodeActivityContext) =>
            {
            });
        }