/// <summary> /// Downloads the file requested by the /// <see cref="GetFileAsync(string,System.Threading.CancellationToken)" /> method. /// </summary> /// <param name="file"> /// The file info received by calling /// <see cref="GetFileAsync(string,System.Threading.CancellationToken)" />. /// </param> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of /// cancellation. /// </param> /// <exception cref="ArgumentNullException">File cannot be null.</exception> /// <returns> /// Returns a task containing the downloaded file as a stream. /// </returns> public Task <Stream> DownloadFileAsync([NotNull] File file, CancellationToken cancellationToken = default(CancellationToken)) { Contracts.EnsureNotNull(file, nameof(file)); var url = file.GetDownloadUrl(this.ApiKey); return(this.Client.GetStreamAsync(url)); }
/// <summary> /// Downloads the file requested by the /// <see cref="GetFileAsync(string,System.Threading.CancellationToken)" /> method. /// </summary> /// <param name="file"> /// The file info received by calling /// <see cref="GetFileAsync(string,System.Threading.CancellationToken)" />. /// </param> /// <param name="fullPath"> /// The full directory and file name to the location where the downloaded file should be saved. /// </param> /// <param name="overwrite"> /// If set to <c>true</c> overwrites the file that exists in the <paramref name="fullPath" /> path. /// </param> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of /// cancellation. /// </param> /// <exception cref="InvalidOperationException"> /// File already exists in the destination path but overwrite parameter is set to <c>false</c>. /// </exception> /// <exception cref="ArgumentNullException">file cannot be null | fullPath cannot be null.</exception> /// <returns>A task that represents the asynchronous operation.</returns> public async Task DownloadFileAsync([NotNull] File file, [NotNull] string fullPath, bool overwrite = false, CancellationToken cancellationToken = default(CancellationToken)) { Contracts.EnsureNotNull(file, nameof(file)); Contracts.EnsureNotNull(fullPath, nameof(fullPath)); if (System.IO.File.Exists(fullPath)) { if (!overwrite) { throw new IOException($"File '{fullPath}' already exists."); } System.IO.File.Delete(fullPath); } using (var response = await this.DownloadFileAsync(file, cancellationToken).ConfigureAwait(false)) { using (var fileStream = new FileStream(fullPath, FileMode.Create, FileAccess.Write, FileShare.None)) { await response.CopyToAsync(fileStream, 81920, cancellationToken).ConfigureAwait(false); } } }