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)); } } }
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)); } } }
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) => { }); }