/// <summary> /// Request a Download action on the targeted file. /// </summary> /// <param name="stream">The stream that will be written to</param> /// <param name="cancelToken">The token to cancel the query</param> /// <param name="progress">The progress monitor</param> /// <param name="options">The query-specific options</param> /// <returns>A Task</returns> public async Task Download(Stream stream, CancellationToken cancelToken, ProgressMonitorBase progress = null, FtpOptions options = null) { cancelToken.ThrowIfCancellationRequested(); options = options ?? Handler.Options; FtpWebResponse response = null; FtpWebRequest request = null; if (progress != null) { long size = -1; if (progress.AskForSize) { try { size = await GetFileSize(cancelToken); } catch { } } progress.Progress = new FtpProgress(size); progress.OnInit(); } if (cancelToken.IsCancellationRequested) { cancelToken.ThrowIfCancellationRequested(); } request = Handler.MakeRequest(cancelToken, options, Target, WebRequestMethods.Ftp.DownloadFile); response = (FtpWebResponse)await request.GetResponseAsync(); cancelToken.ThrowIfCancellationRequested(); byte[] buffer = new byte[options.BufferSize]; int readCount = 0; try { using (var responseStream = response.GetResponseStream()) { if (progress != null) { progress.Progress.StartRateTimer(); } do { readCount = await responseStream.ReadAsync(buffer, 0, options.BufferSize, cancelToken); await stream.WriteAsync(buffer, 0, readCount, cancelToken); cancelToken.ThrowIfCancellationRequested(); if (progress != null) { progress.Progress.CurrentCount += readCount; progress.Progress.AddToRate(readCount); progress.Progressed(); } } while (readCount > 0); } } finally { if (progress != null) { progress.Progress.StopRateTimer(); } } }
private async Task <string> InternalUpload(UploadMode mode, Stream stream, CancellationToken cancelToken, ProgressMonitorBase progress, FtpOptions options = null) { cancelToken.ThrowIfCancellationRequested(); options = options ?? Handler.Options; long size = -1; if (progress != null) { if (progress.AskForSize) { try { size = stream.Length; } catch (NotSupportedException) { } } progress.Progress = new FtpProgress(size); progress.OnInit(); } byte[] buffer = new byte[options.BufferSize]; int readCount = 0; var protocol = string.Empty; switch (mode) { case UploadMode.Upload: protocol = WebRequestMethods.Ftp.UploadFile; break; case UploadMode.UploadAsUnique: protocol = WebRequestMethods.Ftp.UploadFileWithUniqueName; break; case UploadMode.Append: protocol = WebRequestMethods.Ftp.AppendFile; break; } ; FtpWebRequest request = Handler.MakeRequest(cancelToken, options, Target, protocol); if (size != -1) { request.ContentLength = size; } using (var requestStream = await request.GetRequestStreamAsync()) { cancelToken.ThrowIfCancellationRequested(); do { readCount = await stream.ReadAsync(buffer, 0, options.BufferSize, cancelToken); await requestStream.WriteAsync(buffer, 0, readCount, cancelToken); cancelToken.ThrowIfCancellationRequested(); if (progress != null) { progress.Progress.CurrentCount += readCount; progress.Progressed(); } } while (readCount > 0); } if (mode == UploadMode.UploadAsUnique) { using (var response = (FtpWebResponse)await request.GetResponseAsync()) { cancelToken.ThrowIfCancellationRequested(); return(Path.GetFileName(response.ResponseUri.ToString())); } } return(null); }