/// <summary>
/// Downloads the file asynchronously .
/// </summary>
/// <param name="destinationPath">The path to download the file to.</param>
/// <param name="blobTransferClient">The <see cref="BlobTransferClient"/> which is used to download files.</param>
/// <param name="locator">An asset <see cref="ILocator"/> which defines permissions associated with the Asset.</param>
/// <param name="retryPolicy">The retry policy.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A function delegate that returns the future result to be available through the Task.</returns>
internal Task DownloadToFileAsync(string destinationPath, BlobTransferClient blobTransferClient, ILocator locator, IRetryPolicy retryPolicy, CancellationToken cancellationToken)
{
FileEncryption fileEncryption = this.GetFileEncryption();
cancellationToken.Register(() => this.Cleanup(null, fileEncryption, null, null));
return(Task.Factory.StartNew(() =>
{
cancellationToken.ThrowIfCancellationRequested(() => this.Cleanup(null, fileEncryption, null, null));
ulong iv = Convert.ToUInt64(this.InitializationVector, CultureInfo.InvariantCulture);
UriBuilder uriBuilder = new UriBuilder(locator.BaseUri);
uriBuilder.Path += String.Concat("/", Name);
blobTransferClient.TransferProgressChanged += this.OnDownloadBlobTransferProgressChanged;
blobTransferClient.DownloadBlob(
uriBuilder.Uri,
destinationPath,
fileEncryption,
iv,
cancellationToken,
retryPolicy,
() => locator.ContentAccessComponent)
.Wait(cancellationToken);
cancellationToken.ThrowIfCancellationRequested(() => this.Cleanup(null, fileEncryption, null, null));
},
cancellationToken)
.ContinueWith(
t =>
{
t.ThrowIfFaulted(() => this.Cleanup(null, fileEncryption, null, null));
cancellationToken.ThrowIfCancellationRequested(() => this.Cleanup(null, fileEncryption, null, null));
this.Cleanup(null, fileEncryption, null, null);
},
cancellationToken));
}