Skip to content

Commit c1a45a8

Browse files
committed
Migrate PackageDetails.InstallerSize from double to long, allow downloading cargo crates
1 parent 6c4cf84 commit c1a45a8

15 files changed

Lines changed: 23 additions & 39 deletions

File tree

src/UniGetUI.Core.Tools.Tests/ToolsTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ public void TestRandomStringGenerator(int length)
109109
[InlineData("https://www.marticliment.com/unigetui/wingetui_size_test.txt", 460)]
110110
public async Task TestFileSizeLoader(string uri, long expectedSize)
111111
{
112-
double size = await CoreTools.GetFileSizeAsync(uri != "" ? new Uri(uri) : null);
113-
Assert.Equal(expectedSize / 1048576, size);
112+
long size = await CoreTools.GetFileSizeAsLongAsync(uri != "" ? new Uri(uri) : null);
113+
Assert.Equal(expectedSize, size);
114114
}
115115

116116
[Theory]

src/UniGetUI.Core.Tools/Tools.cs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -244,34 +244,18 @@ public static bool IsAdministrator()
244244
}
245245
}
246246

247-
/// <summary>
248-
/// Returns the size (in MB) of the file at the given URL
249-
/// </summary>
250-
/// <param name="url">a valid Uri object containing a URL to a file</param>
251-
/// <returns>a double representing the size in MBs, 0 if the process fails</returns>
252-
public static async Task<double> GetFileSizeAsync(Uri? url)
253-
{
254-
return await GetFileSizeAsyncAsLong(url) / 1048576d;
255-
}
247+
public static Task<long> GetFileSizeAsLongAsync(Uri? url)
248+
=> Task.Run(() => GetFileSizeAsLong(url));
256249

257-
/// <summary>
258-
/// Returns the size (in MB) of the file at the given URL
259-
/// </summary>
260-
/// <param name="url">a valid Uri object containing a URL to a file</param>
261-
/// <returns>a double representing the size in MBs, 0 if the process fails</returns>
262-
public static double GetFileSize(Uri? url)
263-
{
264-
return GetFileSizeAsyncAsLong(url).GetAwaiter().GetResult() / 1048576d;
265-
}
266250

267-
public static async Task<long> GetFileSizeAsyncAsLong(Uri? url)
251+
public static long GetFileSizeAsLong(Uri? url)
268252
{
269253
if (url is null) return 0;
270254

271255
try
272256
{
273257
using HttpClient client = new(CoreTools.GenericHttpClientParameters);
274-
HttpResponseMessage response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Head, url));
258+
HttpResponseMessage response = client.Send(new HttpRequestMessage(HttpMethod.Head, url));
275259
return response.Content.Headers.ContentLength ?? 0;
276260
}
277261
catch (Exception e)

src/UniGetUI.PAckageEngine.Interfaces/IPackageDetails.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ public interface IPackageDetails
5959
public string? InstallerType { get; set; }
6060

6161
/// <summary>
62-
/// The size, in **MEGABYTES**, of the installer
62+
/// The size, in **BYTES**, of the installer
6363
/// </summary>
64-
public double InstallerSize { get; set; }
64+
public long InstallerSize { get; set; }
6565

6666
/// <summary>
6767
/// A URL pointing to the Manifest File of the package

src/UniGetUI.PackageEngine.Managers.Cargo/Cargo.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public Cargo()
4949
CanSkipIntegrityChecks = true,
5050
SupportsCustomVersions = true,
5151
SupportsCustomLocations = true,
52+
CanDownloadInstaller = true,
5253
SupportsProxy = ProxySupport.Partially,
5354
SupportsProxyAuth = true
5455
};

src/UniGetUI.PackageEngine.Managers.Cargo/CratesIOClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ internal record CargoManifestVersion
3838
{
3939
public string[]? bin_names { get; init; }
4040
public required string checksum { get; init; }
41-
public double? crate_size { get; init; }
41+
public long? crate_size { get; init; }
4242
public string? created_at { get; init; }
4343
public required string dl_path { get; init; }
4444
public string? license { get; init; }

src/UniGetUI.PackageEngine.Managers.Cargo/Helpers/CargoPkgDetailsHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected override void GetDetails_UnSafe(IPackageDetails details)
4242

4343
details.Author = versionData.published_by?.name;
4444
details.License = versionData.license;
45-
details.InstallerUrl = new Uri(CratesIOClient.ApiUrl + versionData.dl_path);
45+
details.InstallerUrl = new Uri((CratesIOClient.ApiUrl + versionData.dl_path).Replace("/api/v1/api/v1", "/api/v1"));
4646
details.InstallerSize = versionData.crate_size ?? 0;
4747
details.InstallerHash = versionData.checksum;
4848
details.Publisher = versionData.published_by?.name;

src/UniGetUI.PackageEngine.Managers.Generic.NuGet/BaseNuGetDetailsHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ protected override void GetDetails_UnSafe(IPackageDetails details)
4949
{
5050
try
5151
{
52-
details.InstallerSize = long.Parse(match.Groups[3].Value) / 1000000.0;
52+
details.InstallerSize = long.Parse(match.Groups[3].Value);
5353
break;
5454
}
5555
catch (Exception ex)

src/UniGetUI.PackageEngine.Managers.Npm/Helpers/NpmPkgDetailsHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected override void GetDetails_UnSafe(IPackageDetails details)
5757
details.InstallerUrl = installerUrl;
5858

5959
if (int.TryParse(contents?["dist"]?["unpackedSize"]?.ToString() ?? "", NumberStyles.Any, CultureInfo.InvariantCulture, out int installerSize))
60-
details.InstallerSize = installerSize / 1048576d;
60+
details.InstallerSize = installerSize;
6161

6262
details.InstallerHash = contents?["dist"]?["integrity"]?.ToString();
6363

src/UniGetUI.PackageEngine.Managers.Pip/Helpers/PipPkgDetailsHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ protected override void GetDetails_UnSafe(IPackageDetails details)
6969
{
7070
details.InstallerType = url["url"]?.ToString().Split('.')[^1].Replace("whl", "Wheel");
7171
details.InstallerUrl = installerUrl;
72-
details.InstallerSize = CoreTools.GetFileSize(installerUrl);
72+
details.InstallerSize = CoreTools.GetFileSizeAsLong(installerUrl);
7373
}
7474

7575
details.UpdateDate = url["upload_time"]?.ToString();

src/UniGetUI.PackageEngine.Managers.Scoop/Helpers/ScoopPkgDetailsHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ protected override void GetDetails_UnSafe(IPackageDetails details)
150150
}
151151

152152
if (details.InstallerUrl is not null)
153-
details.InstallerSize = CoreTools.GetFileSize(details.InstallerUrl);
153+
details.InstallerSize = CoreTools.GetFileSizeAsLong(details.InstallerUrl);
154154

155155
// Load release notes URL
156156
if (contents?["checkver"] is JsonObject checkver && Uri.TryCreate(checkver["url"]?.ToString(), UriKind.RelativeOrAbsolute,

0 commit comments

Comments
 (0)