Exemplo n.º 1
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.º 2
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));
                }
            }
        }