|
1 | | -using DSharpPlus; |
| 1 | +using DSharpPlus; |
2 | 2 | using DSharpPlus.CommandsNext; |
3 | 3 | using DSharpPlus.CommandsNext.Attributes; |
4 | 4 | using DSharpPlus.Entities; |
|
16 | 16 | using System.Text; |
17 | 17 | using System.Text.RegularExpressions; |
18 | 18 | using System.Threading.Tasks; |
| 19 | +using System.Net.Http; |
| 20 | +using System.Net.Http.Headers; |
| 21 | +using System.Net.Mime; |
19 | 22 |
|
20 | 23 | namespace StoreBot |
21 | 24 | { |
22 | 25 | public class StoreCommands : BaseCommandModule |
23 | 26 | { |
| 27 | + public String BytesToString(long byteCount) |
| 28 | + { |
| 29 | + string[] suf = { "B", "KB", "MB", "GB", "TB", "PB", "EB" }; //Longs run out around EB |
| 30 | + if (byteCount == 0) |
| 31 | + return "0" + suf[0]; |
| 32 | + long bytes = Math.Abs(byteCount); |
| 33 | + int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024))); |
| 34 | + double num = Math.Round(bytes / Math.Pow(1024, place), 1); |
| 35 | + return (Math.Sign(byteCount) * num).ToString() + suf[place]; |
| 36 | + } |
| 37 | + private static readonly MSHttpClient _httpClient = new MSHttpClient(); |
| 38 | + |
24 | 39 | [Command("info"), Description("Returns info about the current StoreBot Instance")] |
25 | 40 | public async Task InfoAsync(CommandContext cct) |
26 | 41 | { |
@@ -128,8 +143,34 @@ public async Task PackagesAsync(CommandContext cct, [Description("Specify a prod |
128 | 143 | //iterate through all packages |
129 | 144 | foreach (PackageInstance package in packages) |
130 | 145 | { |
131 | | - //temporarily hold the value of the new package in a seperate var in order to check if the field will be to long |
132 | | - string packagelink = $"[{package.PackageMoniker}]({package.PackageUri})"; |
| 146 | + HttpRequestMessage httpRequest = new HttpRequestMessage(); |
| 147 | + httpRequest.RequestUri = package.PackageUri; |
| 148 | + //httpRequest.Method = HttpMethod.Get; |
| 149 | + httpRequest.Method = HttpMethod.Head; |
| 150 | + httpRequest.Headers.Add("Connection", "Keep-Alive"); |
| 151 | + httpRequest.Headers.Add("Accept", "*/*"); |
| 152 | + //httpRequest.Headers.Add("Range", "bytes=0-1"); |
| 153 | + httpRequest.Headers.Add("User-Agent", "Microsoft-Delivery-Optimization/10.0"); |
| 154 | + HttpResponseMessage httpResponse = await _httpClient.SendAsync(httpRequest, new System.Threading.CancellationToken()); |
| 155 | + HttpHeaders headers = httpResponse.Content.Headers; |
| 156 | + IEnumerable<string> values; |
| 157 | + string packagelink; |
| 158 | + if (headers.TryGetValues("Content-Disposition", out values)) |
| 159 | + { |
| 160 | + ContentDisposition contentDisposition = new ContentDisposition(values.First()); |
| 161 | + string filename = contentDisposition.FileName; |
| 162 | + packagelink = $"[{filename}]({package.PackageUri})"; |
| 163 | + } |
| 164 | + else |
| 165 | + { |
| 166 | + //temporarily hold the value of the new package in a seperate var in order to check if the field will be too long |
| 167 | + packagelink = $"[{package.PackageMoniker}]({package.PackageUri})"; |
| 168 | + } |
| 169 | + if (headers.TryGetValues("Content-Length", out values)) |
| 170 | + { |
| 171 | + string filesize = BytesToString(long.Parse(values.FirstOrDefault())); |
| 172 | + packagelink += $": {filesize}"; |
| 173 | + } |
133 | 174 | //check if the combined lengths of the package list and new package link will not exceed the maximum field length of 1024 characters |
134 | 175 | if ((packagelink.Length + packagelist.Length) >= 1024) |
135 | 176 | { |
@@ -301,8 +342,34 @@ public async Task PackagesAsync(CommandContext cct, [Description("Specify a prod |
301 | 342 | //iterate through all packages |
302 | 343 | foreach (PackageInstance package in packages) |
303 | 344 | { |
304 | | - //temporarily hold the value of the new package in a seperate var in order to check if the field will be to long |
305 | | - string packagelink = $"[{package.PackageMoniker}]({package.PackageUri})"; |
| 345 | + HttpRequestMessage httpRequest = new HttpRequestMessage(); |
| 346 | + httpRequest.RequestUri = package.PackageUri; |
| 347 | + //httpRequest.Method = HttpMethod.Get; |
| 348 | + httpRequest.Method = HttpMethod.Head; |
| 349 | + httpRequest.Headers.Add("Connection", "Keep-Alive"); |
| 350 | + httpRequest.Headers.Add("Accept", "*/*"); |
| 351 | + //httpRequest.Headers.Add("Range", "bytes=0-1"); |
| 352 | + httpRequest.Headers.Add("User-Agent", "Microsoft-Delivery-Optimization/10.0"); |
| 353 | + HttpResponseMessage httpResponse = await _httpClient.SendAsync(httpRequest, new System.Threading.CancellationToken()); |
| 354 | + HttpHeaders headers = httpResponse.Content.Headers; |
| 355 | + IEnumerable<string> values; |
| 356 | + string packagelink; |
| 357 | + if (headers.TryGetValues("Content-Disposition", out values)) |
| 358 | + { |
| 359 | + ContentDisposition contentDisposition = new ContentDisposition(values.First()); |
| 360 | + string filename = contentDisposition.FileName; |
| 361 | + packagelink = $"[{filename}]({package.PackageUri})"; |
| 362 | + } |
| 363 | + else |
| 364 | + { |
| 365 | + //temporarily hold the value of the new package in a seperate var in order to check if the field will be too long |
| 366 | + packagelink = $"[{package.PackageMoniker}]({package.PackageUri})"; |
| 367 | + } |
| 368 | + if (headers.TryGetValues("Content-Length", out values)) |
| 369 | + { |
| 370 | + string filesize = BytesToString(long.Parse(values.FirstOrDefault())); |
| 371 | + packagelink += $": {filesize}"; |
| 372 | + } |
306 | 373 | //check if the combined lengths of the package list and new package link will not exceed the maximum field length of 1024 characters |
307 | 374 | if ((packagelink.Length + packagelist.Length) >= 1024) |
308 | 375 | { |
|
0 commit comments