protected override async Task PerformIO() { bool hadToConnect = _client.ConnectIfNeeded(); _client.CreateDirectoriesIfNeeded(Folder); string fullFilePath = ODFileUtils.CombinePaths(Folder, FileName, '/'); using (MemoryStream uploadStream = new MemoryStream(FileContent)) { SftpUploadAsyncResult res = (SftpUploadAsyncResult)_client.BeginUploadFile(uploadStream, fullFilePath); while (!res.AsyncWaitHandle.WaitOne(100)) { if (DoCancel) { res.IsUploadCanceled = true; } OnProgress((double)res.UploadedBytes / (double)1024 / (double)1024, "?currentVal MB of ?maxVal MB uploaded", (double)FileContent.Length / (double)1024 / (double)1024, ""); } _client.EndUploadFile(res); if (res.IsUploadCanceled) { TaskStateDelete state = new Delete() { _client = this._client, Path = fullFilePath }; state.Execute(false); } } _client.DisconnectIfNeeded(hadToConnect); await Task.Run(() => { }); //Gets rid of a compiler warning and does nothing. }
protected override async Task PerformIO() { bool hadToConnect = _client.ConnectIfNeeded(); _client.CreateDirectoriesIfNeeded(FolderPath); List <SftpFile> listFiles = (await Task.Factory.FromAsync(_client.BeginListDirectory(FolderPath, null, null), _client.EndListDirectory)).ToList(); listFiles = listFiles.FindAll(x => !x.FullName.EndsWith(".")); //Sftp has 2 "extra" files that are "." and "..". I think it's for explorer navigation. ListFolderPathLower = listFiles.Select(x => x.FullName.ToLower()).ToList(); ListFolderPathsDisplay = listFiles.Select(x => x.FullName).ToList(); _client.DisconnectIfNeeded(hadToConnect); }
protected override async Task PerformIO() { bool hadToConnect = _client.ConnectIfNeeded(); List <string> listFilePaths; bool isDirectory = _client.GetAttributes(FromPath).IsDirectory; if (isDirectory) { if (DoCancel) { return; } //Only include files (not sub-directories) in the list listFilePaths = (await Task.Factory.FromAsync(_client.BeginListDirectory(FromPath, null, null), _client.EndListDirectory)) .Where(x => !x.IsDirectory) .Select(x => x.FullName).ToList(); } else //Copying a file { listFilePaths = new List <string> { FromPath }; } CountTotal = listFilePaths.Count; for (int i = 0; i < CountTotal; i++) { if (DoCancel) { _client.DisconnectIfNeeded(hadToConnect); return; } string path = listFilePaths[i]; try { string fileName = Path.GetFileName(path); string toPathFull = ToPath; if (isDirectory) { toPathFull = ODFileUtils.CombinePaths(ToPath, fileName, '/'); } if (FileExists(_client, toPathFull)) { throw new Exception(); //Throw so that we can iterate CountFailed } string fromPathFull = FromPath; if (isDirectory) { fromPathFull = ODFileUtils.CombinePaths(FromPath, fileName, '/'); } if (IsCopy) { //Throws if fails. await Task.Run(() => { TaskStateDownload stateDown = new Download(_host, _user, _pass) { Folder = Path.GetDirectoryName(fromPathFull).Replace('\\', '/'), FileName = Path.GetFileName(fromPathFull), ProgressHandler = ProgressHandler, HasExceptions = HasExceptions }; stateDown.Execute(ProgressHandler != null); while (!stateDown.IsDone) { stateDown.DoCancel = DoCancel; } if (DoCancel) { _client.DisconnectIfNeeded(hadToConnect); return; } TaskStateUpload stateUp = new Upload(_host, _user, _pass) { Folder = Path.GetDirectoryName(toPathFull).Replace('\\', '/'), FileName = Path.GetFileName(toPathFull), FileContent = stateDown.FileContent, ProgressHandler = ProgressHandler, HasExceptions = HasExceptions }; stateUp.Execute(ProgressHandler != null); while (!stateUp.IsDone) { stateUp.DoCancel = DoCancel; } if (DoCancel) { _client.DisconnectIfNeeded(hadToConnect); return; } }); } else //Moving { await Task.Run(() => { SftpFile file = _client.Get(path); _client.CreateDirectoriesIfNeeded(ToPath); file.MoveTo(toPathFull); }); } CountSuccess++; } catch (Exception) { CountFailed++; } finally { if (IsCopy) { OnProgress(i + 1, "?currentVal files of ?maxVal files copied", CountTotal, ""); } else { OnProgress(i + 1, "?currentVal files of ?maxVal files moved", CountTotal, ""); } } } _client.DisconnectIfNeeded(hadToConnect); }