DownloadFile.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright (c) .NET Foundation and contributors. All rights reserved.
  2. // Licensed under the MIT license. See LICENSE file in the project root for full license information.
  3. using System;
  4. using System.IO;
  5. using System.Net;
  6. using System.Net.Http;
  7. using System.Threading.Tasks;
  8. using System.Collections.Generic;
  9. using Microsoft.Build.Framework;
  10. using Microsoft.Build.Utilities;
  11. namespace RepoTasks
  12. {
  13. public class DownloadFile : Microsoft.Build.Utilities.Task
  14. {
  15. [Required]
  16. public string Uri { get; set; }
  17. /// <summary>
  18. /// If this field is set and the task fail to download the file from `Uri`, with a NotFound
  19. /// status, it will try to download the file from `PrivateUri`.
  20. /// </summary>
  21. public string PrivateUri { get; set; }
  22. /// <summary>
  23. /// Suffix for the private URI in base64 form (for SAS compatibility)
  24. /// </summary>
  25. public string PrivateUriSuffix { get; set; }
  26. public int MaxRetries { get; set; } = 5;
  27. [Required]
  28. public string DestinationPath { get; set; }
  29. public bool Overwrite { get; set; }
  30. public override bool Execute()
  31. {
  32. return ExecuteAsync().GetAwaiter().GetResult();
  33. }
  34. private async System.Threading.Tasks.Task<bool> ExecuteAsync()
  35. {
  36. string destinationDir = Path.GetDirectoryName(DestinationPath);
  37. if (!Directory.Exists(destinationDir))
  38. {
  39. Directory.CreateDirectory(destinationDir);
  40. }
  41. if (File.Exists(DestinationPath) && !Overwrite)
  42. {
  43. return true;
  44. }
  45. const string FileUriProtocol = "file://";
  46. if (Uri.StartsWith(FileUriProtocol, StringComparison.Ordinal))
  47. {
  48. var filePath = Uri.Substring(FileUriProtocol.Length);
  49. Log.LogMessage($"Copying '{filePath}' to '{DestinationPath}'");
  50. File.Copy(filePath, DestinationPath);
  51. return true;
  52. }
  53. List<string> errorMessages = new List<string>();
  54. bool? downloadStatus = await DownloadWithRetriesAsync(Uri, DestinationPath, errorMessages);
  55. if (downloadStatus == false && !string.IsNullOrEmpty(PrivateUri))
  56. {
  57. string uriSuffix = "";
  58. if (!string.IsNullOrEmpty(PrivateUriSuffix))
  59. {
  60. var uriSuffixBytes = System.Convert.FromBase64String(PrivateUriSuffix);
  61. uriSuffix = System.Text.Encoding.UTF8.GetString(uriSuffixBytes);
  62. }
  63. downloadStatus = await DownloadWithRetriesAsync($"{PrivateUri}{uriSuffix}", DestinationPath, errorMessages);
  64. }
  65. if (downloadStatus != true)
  66. {
  67. foreach (var error in errorMessages)
  68. {
  69. Log.LogError(error);
  70. }
  71. }
  72. return downloadStatus == true;
  73. }
  74. /// <summary>
  75. /// Attempt to download file from `source` with retries when response error is different of FileNotFound and Success.
  76. /// </summary>
  77. /// <param name="source">URL to the file to be downloaded.</param>
  78. /// <param name="target">Local path where to put the downloaded file.</param>
  79. /// <returns>true: Download Succeeded. false: Download failed with 404. null: Download failed but is retriable.</returns>
  80. private async Task<bool?> DownloadWithRetriesAsync(string source, string target, List<string> errorMessages)
  81. {
  82. Random rng = new Random();
  83. Log.LogMessage(MessageImportance.High, $"Attempting download '{source}' to '{target}'");
  84. using (var httpClient = new HttpClient())
  85. {
  86. for (int retryNumber = 0; retryNumber < MaxRetries; retryNumber++)
  87. {
  88. try
  89. {
  90. var httpResponse = await httpClient.GetAsync(source);
  91. Log.LogMessage(MessageImportance.High, $"{source} -> {httpResponse.StatusCode}");
  92. // The Azure Storage REST API returns '400 - Bad Request' in some cases
  93. // where the resource is not found on the storage.
  94. // https://docs.microsoft.com/en-us/rest/api/storageservices/common-rest-api-error-codes
  95. if (httpResponse.StatusCode == HttpStatusCode.NotFound ||
  96. httpResponse.ReasonPhrase.IndexOf("The requested URI does not represent any resource on the server.", StringComparison.OrdinalIgnoreCase) == 0)
  97. {
  98. errorMessages.Add($"Problems downloading file from '{source}'. Does the resource exist on the storage? {httpResponse.StatusCode} : {httpResponse.ReasonPhrase}");
  99. return false;
  100. }
  101. httpResponse.EnsureSuccessStatusCode();
  102. using (var outStream = File.Create(target))
  103. {
  104. await httpResponse.Content.CopyToAsync(outStream);
  105. }
  106. Log.LogMessage(MessageImportance.High, $"returning true {source} -> {httpResponse.StatusCode}");
  107. return true;
  108. }
  109. catch (Exception e)
  110. {
  111. Log.LogMessage(MessageImportance.High, $"returning error in {source} ");
  112. errorMessages.Add($"Problems downloading file from '{source}'. {e.Message} {e.StackTrace}");
  113. File.Delete(target);
  114. }
  115. await System.Threading.Tasks.Task.Delay(rng.Next(1000, 10000));
  116. }
  117. }
  118. Log.LogMessage(MessageImportance.High, $"giving up {source} ");
  119. errorMessages.Add($"Giving up downloading the file from '{source}' after {MaxRetries} retries.");
  120. return null;
  121. }
  122. }
  123. }