using Microsoft.WindowsAzure.Storage.Blob; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Tango.Core.IO; namespace Tango.Transport.Web { public class StorageBlobDownloader : IWebFileDownloader { private bool _disposed; private FileStreamWrapper _stream; private long _fileSize; private String _fileName; public CloudBlockBlob Blob { get; private set; } public String Address { get; private set; } public event EventHandler Progress; public StorageBlobDownloader(CloudBlockBlob blob, String fileName) { Blob = blob; _fileName = fileName; } public StorageBlobDownloader(String blobAddress, String fileName) : this(new CloudBlockBlob(new Uri(blobAddress)), fileName) { Address = blobAddress; } private void OnProgress(long current) { Progress?.Invoke(this, new WebFileDownloaderProgressEventArgs() { Current = current, Total = _fileSize, }); } public async Task Download() { if (_disposed) { throw new ObjectDisposedException("The storage blob downloader can only be used once."); } await Blob.FetchAttributesAsync(); _fileSize = Blob.Properties.Length; _stream = new FileStreamWrapper(_fileName, FileMode.Create, OnProgress); await Blob.DownloadToStreamAsync(_stream); Dispose(); } public void Dispose() { if (!_disposed) { _disposed = true; if (_stream != null) { _stream.Dispose(); } } } public async Task GetFileSize() { await Blob.FetchAttributesAsync(); return Blob.Properties.Length; } } }