public static FilePath DownloadFile(this ICakeContext context, Uri address, DownloadFileSettings settings) { if (context == null) { throw new ArgumentNullException(nameof(context)); } var tempFolder = context.Environment.GetSpecialPath(SpecialPath.LocalTemp); var tempFilename = tempFolder.CombineWithFilePath(new FilePath(System.IO.Path.GetRandomFileName())); DownloadFile(context, address, tempFilename, settings); return(tempFilename); }
public static FilePath DownloadFile(this ICakeContext context, string address, DownloadFileSettings settings) { return(DownloadFile(context, new Uri(address), settings)); }
public static void DownloadFile(this ICakeContext context, Uri address, FilePath outputPath, DownloadFileSettings settings) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (address == null) { throw new ArgumentNullException(nameof(address)); } if (outputPath == null) { throw new ArgumentNullException(nameof(outputPath)); } context.Log.Verbose("Downloading file: {0}", address); // We track the last posted value since the event seems to fire many times for the same value. var percentComplete = 0; using (var http = GetHttpClient(context, settings.UseDefaultCredentials)) { if (!settings.UseDefaultCredentials) { if (!string.IsNullOrWhiteSpace(settings.Username) && !string.IsNullOrWhiteSpace(settings.Password)) { var byteArray = Encoding.ASCII.GetBytes(string.Concat(settings.Username, ":", settings.Password)); http.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray)); } } var progress = new Progress <int>(progressPercentage => { // Only write to log if the value changed and only ever 5%. if (percentComplete != progressPercentage && progressPercentage % 5 == 0) { percentComplete = progressPercentage; context.Log.Verbose("Downloading file: {0}%", progressPercentage); } }); // Download file async so we get the progress events, but block for it to complete anyway. http.DownloadFileAsync(address, outputPath.FullPath, progress).Wait(); } context.Log.Verbose("Download complete, saved to: {0}", outputPath.FullPath); }
public static void DownloadFile(this ICakeContext context, string address, FilePath outputPath, DownloadFileSettings settings) { DownloadFile(context, new Uri(address), outputPath, settings); }