コード例 #1
0
ファイル: ClientForm.cs プロジェクト: Wiles/soa_assign_4
        /// <summary>
        /// Downloads the folder.
        /// </summary>
        /// <param name="localPath">The local path.</param>
        /// <param name="remotePath">The remote path.</param>
        private void DownloadFolder(string localPath, string remotePath)
        {
            try
            {
                if (treeViewHelper.IsSelectedPathDirectory())
                {
                    var selectedFolder   = treeViewHelper.GetSelectedPath();
                    var files            = treeViewHelper.RecursiveListFiles(treeDirectory.SelectedNode);
                    var folders          = treeViewHelper.RecursiveListFolders(treeDirectory.SelectedNode);
                    var localDirectories = (from f in folders
                                            select Path.Combine(localPath, f.Value)).ToList();

                    // Create the selected node on the user's drive
                    var localSelectedFolder = Path.Combine(localPath, Path.GetFileNameWithoutExtension(selectedFolder));
                    if (!Directory.Exists(localSelectedFolder))
                    {
                        Directory.CreateDirectory(localSelectedFolder);
                    }

                    // Create the child directories
                    foreach (var directory in localDirectories)
                    {
                        if (!Directory.Exists(directory))
                        {
                            Directory.CreateDirectory(directory);
                        }
                    }

                    int fileDownloadIndex = 0;
                    foreach (var file in files)
                    {
                        // Remove the parent directories that the user has not selected
                        var fileLocalPath  = Path.Combine(localPath, file.Value);
                        var fileRemotePath = file.Key;

                        ProgressForm progress = new ProgressForm(String.Format(
                                                                     "Downloading {0}/{1}...", fileDownloadIndex, files.Count));
                        var details = this.Client.DownloadDetails(UserName, fileRemotePath);
                        progress.Filename = Path.GetFileName(fileLocalPath);
                        progress.Total    = (long)details.NumberOfChunks * (long)details.ChunkSize;

                        using (var bw = new BackgroundWorker())
                            using (var strm = new FileStream(fileLocalPath, FileMode.OpenOrCreate))
                                using (var writer = new BinaryWriter(strm))
                                {
                                    bw.DoWork += (sender, e) =>
                                    {
                                        try
                                        {
                                            for (int i = 0; i < details.NumberOfChunks; i++)
                                            {
                                                // Exit the loop if we're told to cancel
                                                if (bw.CancellationPending)
                                                {
                                                    e.Cancel = true;
                                                    break;
                                                }

                                                var data = this.Client.DownloadFile(UserName, fileRemotePath, i);
                                                writer.Write(data);
                                                bw.ReportProgress(details.ChunkSize);
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            MessageBox.Show("Failure to download folder, because: " + ex.Message, "Error");
                                            throw;
                                        }
                                    };

                                    bw.ProgressChanged += (sender, e) =>
                                    {
                                        progress.IncrementValue(e.ProgressPercentage);
                                    };

                                    bw.RunWorkerCompleted += (sender, e) =>
                                    {
                                        progress.InvokeOnUI(progress.Close);
                                    };

                                    progress.OnCancel += (sender, e) =>
                                    {
                                        bw.CancelAsync();
                                    };

                                    progress.StartPosition = FormStartPosition.CenterParent;

                                    bw.WorkerReportsProgress      = true;
                                    bw.WorkerSupportsCancellation = true;
                                    bw.RunWorkerAsync();

                                    progress.ShowDialog();
                                    if (progress.WasCancelled)
                                    {
                                        break;
                                    }

                                    fileDownloadIndex++;
                                }
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Failure to download folder");
            }
        }
コード例 #2
0
ファイル: ClientForm.cs プロジェクト: Wiles/soa_assign_4
        /// <summary>
        /// Uploads the file.
        /// </summary>
        /// <param name="localPath">The local path.</param>
        /// <param name="remotePath">The remote path.</param>
        private void UploadFile(string localPath, string remotePath)
        {
            try
            {
                ProgressForm progress = new ProgressForm("Uploading...");

                var length = new FileInfo(localPath).Length;

                // Create the file download chunks and get ready to update the
                // progress bar
                progress.Filename = Path.GetFileName(localPath);
                progress.Total    = length;
                var chunk = MIN_UPLOAD_CHUNK;
                try
                {
                    chunk = this.Client.GetMaxRequestLength() - UPLOAD_HEADER_SIZE;
                }
                finally
                {
                    if (chunk < MIN_UPLOAD_CHUNK)
                    {
                        chunk = MIN_UPLOAD_CHUNK;
                    }
                    else if (chunk > MAX_UPLOAD_CHUNK)
                    {
                        chunk = MAX_UPLOAD_CHUNK;
                    }
                }

                var count = (int)Math.Ceiling((double)length / (double)chunk);

                if (length < chunk)
                {
                    chunk = (int)length;
                }

                using (var bw = new BackgroundWorker())
                    using (var strm = new FileStream(localPath, FileMode.Open))
                        using (var reader = new BinaryReader(strm))
                        {
                            bw.WorkerReportsProgress      = true;
                            bw.WorkerSupportsCancellation = true;
                            bw.DoWork += (sender, e) =>
                            {
                                try
                                {
                                    for (int i = 0; i < count; i++)
                                    {
                                        // Check if the worker was cancelled
                                        if (bw.CancellationPending)
                                        {
                                            e.Cancel = true;
                                            try
                                            {
                                                // Attempt to delete the item on the remote path
                                                this.Client.DeleteItem(this.UserName, remotePath);
                                            }
                                            catch (Exception ex)
                                            {
                                                MessageBox.Show("Failure to delete remains of upload, because " + ex.Message);
                                            }

                                            break;
                                        }

                                        var  size   = chunk;
                                        var  buffer = reader.ReadBytes((int)size);
                                        bool appendToExistingFile = (i > 0);
                                        this.Client.UploadFile(
                                            this.UserName, remotePath, buffer, appendToExistingFile);
                                        bw.ReportProgress(chunk);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show("Failure during file download, because: " + ex.Message, "Error");
                                    throw;
                                }
                            };

                            bw.ProgressChanged += (sender, e) =>
                            {
                                progress.IncrementValue(e.ProgressPercentage);
                            };

                            bw.RunWorkerCompleted += (sender, e) =>
                            {
                                progress.InvokeOnUI(progress.Close);
                                if (!e.Cancelled)
                                {
                                    this.RefreshFileDirectory();
                                }
                            };

                            progress.OnCancel += (sender, e) =>
                            {
                                bw.CancelAsync();
                            };

                            bw.RunWorkerAsync();
                            progress.ShowDialog();
                        }
            }
            catch (Exception)
            {
                MessageBox.Show("Failure to upload file");
            }
        }
コード例 #3
0
ファイル: ClientForm.cs プロジェクト: Wiles/soa_assign_4
        /// <summary>
        /// Downloads the file.
        /// </summary>
        /// <param name="localPath">The local path.</param>
        /// <param name="remotePath">The remote path.</param>
        private void DownloadFile(string localPath, string remotePath)
        {
            try
            {
                ProgressForm progress = new ProgressForm("Downloading...");
                var          details  = this.Client.DownloadDetails(UserName, remotePath);
                progress.Filename = Path.GetFileName(localPath);
                progress.Total    = (long)details.NumberOfChunks * (long)details.ChunkSize;

                using (var bw = new BackgroundWorker())
                    using (var strm = new FileStream(localPath, FileMode.OpenOrCreate))
                        using (var writer = new BinaryWriter(strm))
                        {
                            bw.DoWork += (sender, e) =>
                            {
                                try
                                {
                                    for (int i = 0; i < details.NumberOfChunks; i++)
                                    {
                                        // Exit the loop if we're told to cancel
                                        if (bw.CancellationPending)
                                        {
                                            e.Cancel = true;
                                            break;
                                        }

                                        var data = this.Client.DownloadFile(UserName, remotePath, i);
                                        writer.Write(data);
                                        bw.ReportProgress(details.ChunkSize);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show("Failure to download file, because: " + ex.Message, "Error");
                                    throw;
                                }
                            };

                            bw.ProgressChanged += (sender, e) =>
                            {
                                progress.IncrementValue(e.ProgressPercentage);
                            };

                            bw.RunWorkerCompleted += (sender, e) =>
                            {
                                progress.InvokeOnUI(progress.Close);
                            };

                            progress.OnCancel += (sender, e) =>
                            {
                                bw.CancelAsync();
                            };

                            bw.WorkerReportsProgress      = true;
                            bw.WorkerSupportsCancellation = true;
                            bw.RunWorkerAsync();
                            progress.ShowDialog();
                        }
            }
            catch (Exception)
            {
                MessageBox.Show("Failure to download file");
            }
        }