forked from Marlamin/BuildBackup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDN.cs
176 lines (157 loc) · 7 KB
/
CDN.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
namespace BuildBackup
{
public class CDN
{
public HttpClient client;
public string cacheDir;
public List<string> cdnList;
public async Task<uint> GetRemoteFileSize(string path)
{
path = path.ToLower();
var found = false;
foreach (var cdn in cdnList)
{
if (found) continue;
var uri = new Uri("http://" + cdn + "/" + path);
var cleanName = uri.AbsolutePath;
try
{
using (var response = await client.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead))
{
if (response.IsSuccessStatusCode)
{
found = true;
if (response.Content.Headers.ContentLength != null)
return (uint)response.Content.Headers.ContentLength;
}
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 " + uri.AbsoluteUri);
}
}
}
catch (Exception e)
{
Logger.WriteLine("!!! Error retrieving file size " + uri.AbsoluteUri + ": " + e.Message);
}
}
if (!found)
{
Logger.WriteLine("Exhausted all CDNs looking for file " + Path.GetFileNameWithoutExtension(path) + ", cannot retrieve filesize!", true);
}
return 0;
}
public async Task<byte[]> Get(string path, bool returnstream = true, bool redownload = false, uint expectedSize = 0, bool verbose = false)
{
path = path.ToLower();
var localPath = Path.Combine(cacheDir, path);
if (File.Exists(localPath) && expectedSize != 0)
{
var fileInfo = new FileInfo(localPath);
if (fileInfo.Length != expectedSize)
{
if (verbose)
Console.WriteLine("File size of " + path + " " + fileInfo.Length + " does not match expected size " + expectedSize + " redownloading..");
redownload = true;
}
}
if (redownload || !File.Exists(Path.Combine(cacheDir, path)))
{
var found = false;
foreach (var cdn in cdnList)
{
if (found) continue;
var uri = new Uri("http://" + cdn + "/" + path);
var cleanName = uri.AbsolutePath;
if (verbose)
Console.WriteLine("Downloading " + path + " from " + cdn);
try
{
if (!Directory.Exists(cacheDir + cleanName))
{
Directory.CreateDirectory(Path.GetDirectoryName(cacheDir + cleanName));
}
using (var response = await client.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead))
using (var responseStream = await response.Content.ReadAsStreamAsync())
{
if (response.IsSuccessStatusCode)
{
using (var file = File.Create(cacheDir + cleanName))
{
found = true;
var buffer = new byte[4096];
int read;
while ((read = await responseStream.ReadAsync(buffer, 0, buffer.Length)) != 0)
{
file.Write(buffer, 0, read);
}
}
}
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 " + uri.AbsoluteUri);
}
}
}
catch (TaskCanceledException e)
{
if (!e.CancellationToken.IsCancellationRequested)
{
Logger.WriteLine("!!! Timeout while retrieving file " + uri.AbsoluteUri);
}
}
catch (Exception e)
{
Logger.WriteLine("!!! Error retrieving file " + uri.AbsoluteUri + ": " + e.Message);
}
}
if (!found)
{
Logger.WriteLine("Exhausted all CDNs looking for file " + Path.GetFileNameWithoutExtension(path) + ", cannot retrieve it!", true);
}
else
{
if (verbose)
Console.WriteLine("Downloaded " + path);
}
}
if (returnstream)
{
if (path.Contains("tpr/wowdev"))
{
return await Task.FromResult(BLTE.DecryptFile(Path.GetFileNameWithoutExtension(path), await File.ReadAllBytesAsync(Path.Combine(cacheDir, path)), "wowdevalpha"));
}
else if (path.Contains("tpr/fenrisdev"))
{
return await Task.FromResult(BLTE.DecryptFile(Path.GetFileNameWithoutExtension(path), await File.ReadAllBytesAsync(Path.Combine(cacheDir, path)), "fenrisdev"));
}
else if (path.Contains("tpr/fenrisevent"))
{
return await Task.FromResult(BLTE.DecryptFile(Path.GetFileNameWithoutExtension(path), await File.ReadAllBytesAsync(Path.Combine(cacheDir, path)), "fenrise"));
}
else
{
return await File.ReadAllBytesAsync(Path.Combine(cacheDir, path));
}
}
else
{
return new byte[0];
}
}
}
}