public FileInfo DownloadFileFromURLToPath(string url, string path, long fileSize) { if (comms.Cancel) { return(null); } if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(path)) { return(null); } downloadHandler.DownloadedFilePath = path; downloadHandler.DownloadedFilename = Path.GetFileName(path); downloadHandler.DownloadedFileSize = fileSize; if (comms.LogProgress) { downloadHandler.Progress = new DownloadProgress(downloadHandler); comms.SetProgress(downloadHandler.Progress); } if (IsGoogleDriveURL(url)) { url = PatchUtils.GetGoogleDriveDownloadLinkFromUrl(url); verifyDownloadSize = false; return(DownloadGoogleDriveFileFromURLToPath(url, path)); } else { verifyDownloadSize = true; return(DownloadFileFromURLToPathInternal(url, path)); } }
public string DownloadTextFromURL(string url) { if (string.IsNullOrEmpty(url)) { return(null); } if (IsGoogleDriveURL(url)) { url = PatchUtils.GetGoogleDriveDownloadLinkFromUrl(url); } for (int i = 0; i < PatchParameters.FailedDownloadsRetryLimit; i++) { if (comms.Cancel) { return(null); } try { var syncObject = new object(); lock ( syncObject ) { downloadHandler.DownloadString(url, syncObject); Monitor.Wait(syncObject); } if (comms.Cancel) { return(null); } if (downloadStringResult == null) { Thread.Sleep(PatchParameters.FailedDownloadsCooldownMillis); continue; } return(downloadStringResult); } catch (WebException e) { comms.LogToFile(e); Thread.Sleep(PatchParameters.FailedDownloadsCooldownMillis); } catch (UriFormatException e) { comms.LogToFile(e); return(null); } } return(null); }
// Credit: http://stackoverflow.com/questions/1460273/how-to-check-if-a-file-exists-on-an-webserver-by-its-url // Credit: http://stackoverflow.com/questions/3614034/system-net-webexception-http-status-code public bool FileExistsAtUrl(string url, out long fileSize) { if (string.IsNullOrEmpty(url)) { fileSize = 0L; return(false); } if (IsGoogleDriveURL(url)) { url = PatchUtils.GetGoogleDriveDownloadLinkFromUrl(url); } WebRequest request = WebRequest.Create(url); request.Method = "HEAD"; request.Timeout = PatchParameters.FileAvailabilityCheckTimeout; try { using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { fileSize = response.ContentLength; return(response.StatusCode == HttpStatusCode.OK); } } catch (WebException e) { fileSize = 0L; if (e.Response != null && e.Response is HttpWebResponse) { using (HttpWebResponse exResponse = (HttpWebResponse)e.Response) { if (exResponse.StatusCode == HttpStatusCode.ServiceUnavailable) { // Drive returns 503 error while requesting HEAD for valid download links if (IsGoogleDriveURL(url)) { return(true); } } } } comms.LogToFile(e); return(false); } }