public byte[] Get(string url, bool returnstream = true, bool redownload = false) { var uri = new Uri(url.ToLower()); string cleanname = uri.AbsolutePath; if (redownload || !File.Exists(cacheDir + cleanname)) { try { if (!Directory.Exists(cacheDir + cleanname)) { Directory.CreateDirectory(Path.GetDirectoryName(cacheDir + cleanname)); } Console.Write("\nDownloading " + cleanname); using (HttpResponseMessage response = client.GetAsync(uri).Result) { if (response.IsSuccessStatusCode) { using (MemoryStream mstream = new MemoryStream()) using (HttpContent res = response.Content) { res.CopyToAsync(mstream); if (isEncrypted) { var cleaned = Path.GetFileNameWithoutExtension(cleanname); var decrypted = BLTE.DecryptFile(cleaned, mstream.ToArray(), decryptionKeyName); File.WriteAllBytes(cacheDir + cleanname, decrypted); return(decrypted); } else { File.WriteAllBytes(cacheDir + cleanname, mstream.ToArray()); } } } else { throw new Exception("Error retrieving file: HTTP status code " + response.StatusCode + " on URL " + url); } } } catch (Exception e) { Console.WriteLine(e.Message); } } if (returnstream) { return(File.ReadAllBytes(cacheDir + cleanname)); } else { return(new byte[0]); } }
public byte[] Get(string path, bool returnstream = true, bool redownload = false) { path = path.ToLower(); if (redownload || !File.Exists(Path.Combine(cacheDir, path))) { var found = false; foreach (var cdn in cdnList) { if (found) { continue; } var url = "http://" + cdn + "/" + path.ToLower(); var uri = new Uri(url); string cleanname = uri.AbsolutePath; try { if (!Directory.Exists(cacheDir + cleanname)) { Directory.CreateDirectory(Path.GetDirectoryName(cacheDir + cleanname)); } using (HttpResponseMessage response = client.GetAsync(uri).Result) { if (response.IsSuccessStatusCode) { using (MemoryStream mstream = new MemoryStream()) using (HttpContent res = response.Content) { res.CopyToAsync(mstream); found = true; if (isEncrypted) { var cleaned = Path.GetFileNameWithoutExtension(cleanname); var decrypted = BLTE.DecryptFile(cleaned, mstream.ToArray(), decryptionKeyName); File.WriteAllBytes(cacheDir + cleanname, decrypted); return(decrypted); } else { File.WriteAllBytes(cacheDir + cleanname, mstream.ToArray()); } } } else if (response.StatusCode == System.Net.HttpStatusCode.NotFound) { Logger.WriteLine("File not found on CDN " + cdn + " trying next CDN (if available).."); } else { throw new FileNotFoundException("Error retrieving file: HTTP status code " + response.StatusCode + " on URL " + url); } } } catch (TaskCanceledException e) { if (!e.CancellationToken.IsCancellationRequested) { Logger.WriteLine("!!! Timeout while retrieving file " + url); } } catch (Exception e) { Logger.WriteLine("!!! Error retrieving file " + url + ": " + e.Message); } } if (!found) { Logger.WriteLine("Exhausted all CDNs looking for file " + Path.GetFileNameWithoutExtension(path) + ", cannot retrieve it!", true); } } if (returnstream) { return(File.ReadAllBytes(Path.Combine(cacheDir, path))); } else { return(new byte[0]); } }
public byte[] Get(string url, bool returnstream = true, bool redownload = false) { url = url.ToLower(); var uri = new Uri(url); string cleanname = uri.AbsolutePath; if (redownload || !File.Exists(cacheDir + cleanname)) { try { if (!Directory.Exists(cacheDir + cleanname)) { Directory.CreateDirectory(Path.GetDirectoryName(cacheDir + cleanname)); } Console.Write("\nDownloading " + cleanname); using (HttpResponseMessage response = client.GetAsync(uri).Result) { if (response.IsSuccessStatusCode) { using (MemoryStream mstream = new MemoryStream()) using (HttpContent res = response.Content) { res.CopyToAsync(mstream); if (url.StartsWith("http://188.165.192.135/1")) { cleanname = cleanname.Substring(2); cleanname = "/tpr/wow/data/" + cleanname; } if (isEncrypted) { var cleaned = Path.GetFileNameWithoutExtension(cleanname); var decrypted = BLTE.DecryptFile(cleaned, mstream.ToArray(), decryptionKeyName); File.WriteAllBytes(cacheDir + cleanname, decrypted); return(decrypted); } else { File.WriteAllBytes(cacheDir + cleanname, mstream.ToArray()); } } } else if (response.StatusCode == System.Net.HttpStatusCode.NotFound && !url.StartsWith("http://188.165.192.135/")) { Console.WriteLine("\nNot found on primary mirror, retrying on secondary mirror..."); return(Get("http://188.165.192.135" + cleanname, returnstream, redownload)); } else if (response.StatusCode == System.Net.HttpStatusCode.NotFound && url.StartsWith("http://188.165.192.135") && !url.StartsWith("http://188.165.192.135/1")) { Console.WriteLine("\nNot found on secondary mirror, retrying on last mirror..."); string ncleanname = cleanname.Substring(14); return(Get("http://188.165.192.135/1/" + ncleanname, returnstream, redownload)); } else { throw new FileNotFoundException("\nError retrieving file: HTTP status code " + response.StatusCode + " on URL " + url); } } } catch (Exception e) { Console.WriteLine("\n!!! Error retrieving file " + url + ": " + e.Message); File.AppendAllText("failedfiles.txt", url + "\n"); } } if (returnstream) { return(File.ReadAllBytes(cacheDir + cleanname)); } else { return(new byte[0]); } }