Esempio n. 1
0
        /// <summary>
        /// Request a Download action on the targeted file, returning an array of bytes.
        /// </summary>
        /// <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>The full array of bytes of the targeted file</returns>
        public async Task <byte[]> DownloadData(CancellationToken cancelToken, ProgressMonitorBase progress = null, FtpOptions options = null)
        {
            using (var mem = new MemoryStream()) {
                await Download(mem, cancelToken, progress, options);

                return(mem.ToArray());
            }
        }
Esempio n. 2
0
        /// <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();
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Request a Download action on the targeted file, returning a string.
        /// </summary>
        /// <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>The full string of the targeted file</returns>
        public async Task <string> DownloadString(CancellationToken cancelToken, ProgressMonitorBase progress = null, FtpOptions options = null)
        {
            using (var mem = new MemoryStream()) {
                await Download(mem, cancelToken, progress, options);

                using (var reader = new StreamReader(mem))
                    return(await reader.ReadToEndAsync());
            }
        }
Esempio n. 4
0
 /// <summary>
 /// Request an Append action on the targeted file.
 /// </summary>
 /// <param name="stream">The stream that will be read from</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 Append(Stream stream, CancellationToken cancelToken, ProgressMonitorBase progress = null, FtpOptions options = null)
 {
     await InternalUpload(UploadMode.Append, stream, cancelToken, progress, options);
 }
Esempio n. 5
0
 /// <summary>
 /// Request an Append action on the targeted file.
 /// </summary>
 /// <param name="filename">The local file that will be read from</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 Append(string filename, CancellationToken cancelToken, ProgressMonitorBase progress = null, FtpOptions options = null)
 {
     using (var filestream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
         await InternalUpload(UploadMode.Append, filestream, cancelToken, progress, options);
 }
Esempio n. 6
0
 /// <summary>
 /// Request a Upload action on the targeted directory, with a unique, randomly generated name.
 /// </summary>
 /// <param name="stream">The stream that will be read from</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>The filename of the generated file</returns>
 public async Task <string> UploadWithUniqueName(Stream stream, CancellationToken cancelToken, ProgressMonitorBase progress = null, FtpOptions options = null)
 {
     return(await InternalUpload(UploadMode.UploadAsUnique, stream, cancelToken, progress, options));
 }
Esempio n. 7
0
 /// <summary>
 /// Request a Upload action on the targeted directory, with a unique, randomly generated name.
 /// </summary>
 /// <param name="filename">The local file that will be read from</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>The filename of the generated file</returns>
 public async Task <string> UploadWithUniqueName(string filename, CancellationToken cancelToken, ProgressMonitorBase progress = null, FtpOptions options = null)
 {
     using (var filestream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read))
         return(await InternalUpload(UploadMode.UploadAsUnique, filestream, cancelToken, progress, options));
 }
Esempio n. 8
0
        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);
        }
Esempio n. 9
0
 /// <summary>
 /// Request a Upload action on the targeted file.
 /// </summary>
 /// <param name="stream">The stream that will be read from</param>
 /// <param name="progress">The progress monitor</param>
 /// <param name="options">The query-specific options</param>
 /// <returns>A Task</returns>
 public async Task Upload(Stream stream, ProgressMonitorBase progress = null, FtpOptions options = null)
 {
     await InternalUpload(UploadMode.Upload, stream, CancellationToken.None, progress, options);
 }
Esempio n. 10
0
 /// <summary>
 /// Request a Download action on the targeted file.
 /// </summary>
 /// <param name="stream">The stream that will be written to</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, ProgressMonitorBase progress = null, FtpOptions options = null)
 {
     await Download(stream, CancellationToken.None, progress, options);
 }
Esempio n. 11
0
 /// <summary>
 /// Request a Download action on the targeted file.
 /// </summary>
 /// <param name="filename">The local file 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(string filename, CancellationToken cancelToken, ProgressMonitorBase progress = null, FtpOptions options = null)
 {
     using (var filestream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None))
         await Download(filestream, cancelToken, progress, options);
 }
Esempio n. 12
0
 /// <summary>
 /// Request a Download action on the targeted file, returning a string.
 /// </summary>
 /// <param name="encode">The encoding to use to reclaim the data into a string</param>
 /// <param name="progress">The progress monitor</param>
 /// <param name="options">The query-specific options</param>
 /// <returns>The full string of the targeted file</returns>
 public async Task <string> DownloadString(Encoding encode, ProgressMonitorBase progress = null, FtpOptions options = null)
 {
     return(await DownloadString(encode, CancellationToken.None, progress, options));
 }
Esempio n. 13
0
 /// <summary>
 /// Request a Download action on the targeted file, returning an array of bytes.
 /// </summary>
 /// <param name="progress">The progress monitor</param>
 /// <param name="options">The query-specific options</param>
 /// <returns>The full array of bytes of the targeted file</returns>
 public async Task <byte[]> DownloadData(ProgressMonitorBase progress = null, FtpOptions options = null)
 {
     return(await DownloadData(CancellationToken.None, progress, options));
 }