Example #1
0
        public static bool DownloadFile(this BosClient bosClient, BOSDownloadRequest bosDownloadRequest, IProgress <int> onProgressPercentChanged)
        {
            Debug.Assert(bosDownloadRequest.RequestInfo.DownloadSize > 0);
            Debug.Assert(bosDownloadRequest.RequestInfo.DownloadedBytes % bosDownloadRequest.RequestInfo.DownloadSize == 0);

            var filePath = bosDownloadRequest.RequestInfo.FilePath;

            var getObjectRequest = new GetObjectRequest
            {
                BucketName = bosDownloadRequest.RequestInfo.Bucket,
                Key        = bosDownloadRequest.RequestInfo.ObjectKey,
            };

            var objectMetadata = bosClient.GetObjectMetadata(getObjectRequest);
            var fileLength     = objectMetadata.ContentLength;

            Debug.Assert(bosDownloadRequest.RequestInfo.DownloadedBytes < fileLength);

            using (var stream = File.OpenWrite(filePath))
            {
                if (bosDownloadRequest.RequestInfo.DownloadedBytes == 0)
                {
                    // This doesn't work:
                    //stream.Seek(fileLength, SeekOrigin.Begin);
                    stream.SetLength(fileLength);
                }

                stream.Seek(bosDownloadRequest.RequestInfo.DownloadedBytes, SeekOrigin.Begin);

                int parts = (int)(fileLength / bosDownloadRequest.RequestInfo.DownloadSize);
                if (fileLength % bosDownloadRequest.RequestInfo.DownloadSize != 0)
                {
                    parts += 1;
                }


                for (int i = (int)(bosDownloadRequest.RequestInfo.DownloadedBytes / bosDownloadRequest.RequestInfo.DownloadSize);
                     i < parts;
                     ++i)
                {
                    if (bosDownloadRequest.PauseCancellationToken.IsCancellationRequested)
                    {
                        return(false);
                    }

                    if (bosDownloadRequest.AbortCancellationToken.IsCancellationRequested)
                    {
                        stream.Close();
                        return(false);
                    }

                    var startBytes = bosDownloadRequest.RequestInfo.DownloadSize * i;
                    var endBytes   = startBytes + bosDownloadRequest.RequestInfo.DownloadSize - 1;
                    if (endBytes > fileLength)
                    {
                        endBytes = fileLength;
                    }

                    onProgressPercentChanged?.Report((int)(((double)startBytes / fileLength) * 100));

                    getObjectRequest.SetRange(startBytes, endBytes);
                    var bosObject = bosClient.GetObject(getObjectRequest);
                    bosObject.ObjectContent.CopyTo(stream);

                    bosDownloadRequest.RequestInfo.DownloadedBytes += endBytes + 1 - startBytes;
                }

                onProgressPercentChanged?.Report(100);
            }

            return(true);
        }
        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;
        }
Example #3
0
 public static Task <bool> DownloadFileAsync(this BosClient bosClient, BOSDownloadRequest bosDownloadRequest, IProgress <int> onProgressPercentChanged)
 {
     return(Task.Run(() => bosClient.DownloadFile(bosDownloadRequest, onProgressPercentChanged)));
 }