Ejemplo n.º 1
0
        private async void downloadToolStripButton__Click(object sender, EventArgs e)
        {
            var objectListView = bucketTabControl_.SelectedTab.Controls[0] as BOSObjectListView;

            Debug.Assert(objectListView != null);

            var objectKey = objectListView.SelectedItems[0].SubItems[0].Text;

            string filePath;

            using (var saveFileDialog = new SaveFileDialog())
            {
                var filters = new string[]
                {
                    "All Files (*.*)|*.*",
                };

                saveFileDialog.FileName        = Path.GetFileName(objectKey);
                saveFileDialog.Filter          = string.Join("|", filters);
                saveFileDialog.OverwritePrompt = true;
                saveFileDialog.CheckPathExists = true;
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    filePath = saveFileDialog.FileName;
                }
                else
                {
                    return;
                }
            }

            toolStripProgressBar_.Value      = 0;
            uploadToolStripButton_.Enabled   = false;
            downloadToolStripButton_.Enabled = false;
            settingsToolStripButton_.Enabled = false;

            toolStripProgressBar_.Visible = true;
            pauseToolStripButton_.Visible = true;
            abortToolStripButton_.Visible = true;

            pauseCancellationTokenSource_ = new CancellationTokenSource();
            abortCancellationTokenSource_ = new CancellationTokenSource();


            BOSDownloadRequestInfo bosDownloadRequestInfo;

            var downloadInfoFile    = filePath + BOSDownloadRequestInfo.DownloadRequestInfoFileExtension;
            var downloadingFilePath = filePath + BOSDownloadRequestInfo.DownloadingFileExtension;

            if (File.Exists(downloadInfoFile))
            {
                bosDownloadRequestInfo = JsonHelper.Load <BOSDownloadRequestInfo>(downloadInfoFile);
            }
            else
            {
                bosDownloadRequestInfo = new BOSDownloadRequestInfo
                {
                    FilePath     = downloadingFilePath,
                    Bucket       = CurrentBucket_,
                    ObjectKey    = objectKey,
                    DownloadSize = settings_.DownloadSize > 0 ? settings_.DownloadSize : 1024 * 256,
                };
            }

            var bosDownloadRequest = new BOSDownloadRequest
            {
                RequestInfo            = bosDownloadRequestInfo,
                PauseCancellationToken = pauseCancellationTokenSource_.Token,
                AbortCancellationToken = abortCancellationTokenSource_.Token,
            };

            bool ok = false;

            try
            {
                ok = await bosClient_.DownloadFileAsync(bosDownloadRequest,
                                                        new ProgressBarReporter(toolStripProgressBar_.ProgressBar, SynchronizationContext.Current));
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    this,
                    "Failed to download object:\n" + ex.Message,
                    "Error:",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }

            if (ok)
            {
                if (File.Exists(downloadingFilePath))
                {
                    File.Move(downloadingFilePath, filePath);
                }

                if (File.Exists(downloadInfoFile))
                {
                    File.Delete(downloadInfoFile);
                }
            }
            else if (pauseCancellationTokenSource_.IsCancellationRequested)
            {
                JsonHelper.Store(downloadInfoFile, bosDownloadRequestInfo);
            }
            else if (abortCancellationTokenSource_.IsCancellationRequested)
            {
                if (File.Exists(downloadingFilePath))
                {
                    File.Delete(downloadingFilePath);
                }
            }

            uploadToolStripButton_.Enabled   = true;
            downloadToolStripButton_.Enabled = true;
            settingsToolStripButton_.Enabled = true;

            toolStripProgressBar_.Visible = false;
            pauseToolStripButton_.Visible = false;
            abortToolStripButton_.Visible = false;

            pauseCancellationTokenSource_ = null;
            abortCancellationTokenSource_ = null;
        }
Ejemplo n.º 2
0
 public static void Store(Settings settings)
 {
     JsonHelper.Store(SettingsFilePath, settings);
 }
Ejemplo n.º 3
0
        private async void uploadToolStripButton__Click(object sender, EventArgs e)
        {
            string filePath;

            using (var openFileDialog = new OpenFileDialog())
            {
                var filters = new string[]
                {
                    "All Files (*.*)|*.*",
                };

                openFileDialog.Filter = string.Join("|", filters);
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    filePath = openFileDialog.FileName;
                }
                else
                {
                    return;
                }
            }

            toolStripProgressBar_.Value      = 0;
            uploadToolStripButton_.Enabled   = false;
            downloadToolStripButton_.Enabled = false;
            settingsToolStripButton_.Enabled = false;

            toolStripProgressBar_.Visible = true;
            pauseToolStripButton_.Visible = true;
            abortToolStripButton_.Visible = true;

            var objectKey = settings_.UseFileFullPathAsObjectKey ? filePath : Path.GetFileName(filePath);

            pauseCancellationTokenSource_ = new CancellationTokenSource();
            abortCancellationTokenSource_ = new CancellationTokenSource();

            BOSMultipartUploadRequestInfo bosMultipartUploadRequestInfo;

            var multipartUploadInfoFile = filePath + BOSMultipartUploadRequestInfo.UploadRequestInfoFileExtension;

            if (File.Exists(multipartUploadInfoFile))
            {
                bosMultipartUploadRequestInfo = JsonHelper.Load <BOSMultipartUploadRequestInfo>(multipartUploadInfoFile);
            }
            else
            {
                bosMultipartUploadRequestInfo = new BOSMultipartUploadRequestInfo
                {
                    FilePath  = filePath,
                    Bucket    = CurrentBucket_,
                    ObjectKey = objectKey,
                    PartSize  = settings_.UploadPartSize > 0 ? settings_.UploadPartSize : 1024 * 1024 * 1,
                };
            }

            var bosMultipartUploadRequest = new BOSMultipartUploadRequest
            {
                RequestInfo            = bosMultipartUploadRequestInfo,
                PauseCancellationToken = pauseCancellationTokenSource_.Token,
                AbortCancellationToken = abortCancellationTokenSource_.Token,
            };

            bool ok = false;

            try
            {
                ok = await bosClient_.UploadFileAsync(bosMultipartUploadRequest,
                                                      new ProgressBarReporter(toolStripProgressBar_.ProgressBar, SynchronizationContext.Current));
            }
            catch (Exception ex)
            {
                MessageBox.Show(
                    this,
                    "Failed to upload object:\n" + ex.Message,
                    "Error:",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }

            if (ok)
            {
                UpdateObjectList();

                if (File.Exists(multipartUploadInfoFile))
                {
                    File.Delete(multipartUploadInfoFile);
                }
            }
            else if (pauseCancellationTokenSource_.IsCancellationRequested)
            {
                JsonHelper.Store(multipartUploadInfoFile, bosMultipartUploadRequestInfo);
            }

            uploadToolStripButton_.Enabled   = true;
            downloadToolStripButton_.Enabled = true;
            settingsToolStripButton_.Enabled = true;

            toolStripProgressBar_.Visible = false;
            pauseToolStripButton_.Visible = false;
            abortToolStripButton_.Visible = false;

            pauseCancellationTokenSource_ = null;
            abortCancellationTokenSource_ = null;
        }