public static string GetFile(Log log, string url) { var dst = GetFileName(url); // 1) Early out if the file exists locally if (File.Exists(dst)) { Disk.TouchFile(log, dst); return(dst); } Disk.CreateDirectory(log, StuffDirectory); try { // Use a set to avoid repeating errors var errors = new HashSet <string>(); for (int tries = 0;; tries++) { // 2) Download the file from URL log.WriteLine("Downloading " + url + ( tries > 0 ? " (retry " + tries + "...)" : null ), ConsoleColor.Blue); try { using (var client = new StuffWebClient()) client.DownloadFile(url, dst); return(dst); } catch (Exception e) { // Fail the 10th time, or unless WebException if (tries >= 10 || !(e is WebException)) { // Print previous errors, before rethrowing foreach (var s in errors) { log.Error(s); } throw; } errors.Add(e.Message); Thread.Sleep(500); } } } catch { Disk.DeleteFile(log, dst); throw; } }
public static void UpdateTimestamp(Log log, string url) { Disk.TouchFile(log, GetFileName(url)); }