Skip to content
This repository was archived by the owner on Mar 26, 2025. It is now read-only.

Commit 63fc14d

Browse files
authored
Merge pull request #6 from StoreDev/filenameandfilesize
Add Filename and Filesize to response
2 parents 4b3995e + c3bb9c0 commit 63fc14d

File tree

1 file changed

+72
-5
lines changed

1 file changed

+72
-5
lines changed

StoreCommands.cs

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using DSharpPlus;
1+
using DSharpPlus;
22
using DSharpPlus.CommandsNext;
33
using DSharpPlus.CommandsNext.Attributes;
44
using DSharpPlus.Entities;
@@ -16,11 +16,26 @@
1616
using System.Text;
1717
using System.Text.RegularExpressions;
1818
using System.Threading.Tasks;
19+
using System.Net.Http;
20+
using System.Net.Http.Headers;
21+
using System.Net.Mime;
1922

2023
namespace StoreBot
2124
{
2225
public class StoreCommands : BaseCommandModule
2326
{
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+
2439
[Command("info"), Description("Returns info about the current StoreBot Instance")]
2540
public async Task InfoAsync(CommandContext cct)
2641
{
@@ -128,8 +143,34 @@ public async Task PackagesAsync(CommandContext cct, [Description("Specify a prod
128143
//iterate through all packages
129144
foreach (PackageInstance package in packages)
130145
{
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+
}
133174
//check if the combined lengths of the package list and new package link will not exceed the maximum field length of 1024 characters
134175
if ((packagelink.Length + packagelist.Length) >= 1024)
135176
{
@@ -301,8 +342,34 @@ public async Task PackagesAsync(CommandContext cct, [Description("Specify a prod
301342
//iterate through all packages
302343
foreach (PackageInstance package in packages)
303344
{
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+
}
306373
//check if the combined lengths of the package list and new package link will not exceed the maximum field length of 1024 characters
307374
if ((packagelink.Length + packagelist.Length) >= 1024)
308375
{

0 commit comments

Comments
 (0)