Example #1
0
        public override async Task StartAsync()
        {
            try
            {
                if (_bucket != null)
                {
                    _entries = new List <IEntry>();
                    var children = await _bucket.ListAllEntries(string.Empty, false);

                    foreach (var child in children)
                    {
                        _entries.Add(child);
                    }
                }

                foreach (var directory in _entries.Where(e => e.IsDirectory).ToArray())
                {
                    _entries.Remove(directory);
                    var children = await directory.ListAllBlobsAsync();

                    foreach (var child in children)
                    {
                        _entries.Add(child);
                    }
                }

                var blobs = _entries.OfType <IBlob>().ToArray();

                var    total = blobs.Sum(f => f.Length);
                double done  = 0;

                int count     = blobs.Length;
                int processed = 1;

                foreach (var blob in blobs)
                {
                    _tokenSource.Token.ThrowIfCancellationRequested();

                    Text = $"Downloading {count} blobs ({processed}: '{blob.Key}')...";

                    var progress = new Progress <long>();
                    progress.ProgressChanged += (s, transferred) =>
                    {
                        Progress = (done + transferred) / total;
                    };

                    var path = Path.Combine(_localFolderPath, PathHelper.GetParent(blob.Key));
                    if (Directory.Exists(path) == false)
                    {
                        Directory.CreateDirectory(path);
                    }

                    var fullPath = Path.Combine(_localFolderPath, blob.Key);

                    using (var output = File.OpenWrite(fullPath))
                    {
                        await blob.DownloadToStreamAsync(output, progress, _tokenSource.Token);
                    }

                    done += blob.Length;
                    processed++;
                    Progress = done / total;
                }

                OnSucceeded();
            }
            catch (Exception ex)
            {
                OnFailed(ex.Message);
            }
        }