Skip to content

Commit

Permalink
Prevent re-downloads from same InstallerUrl (#381)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdanish-kh authored Jun 8, 2023
1 parent aee36b4 commit 19b4bdc
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/WingetCreateCLI/Commands/BaseCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public abstract class BaseCommand
/// </summary>
protected const string ProgramApplicationAlias = "wingetcreate.exe";

/// <summary>
/// Dictionary to store paths to downloaded installers from a given URL.
/// </summary>
private static readonly Dictionary<string, string> DownloadedInstallers = new();

/// <summary>
/// Gets or sets the GitHub token used to submit a pull request on behalf of the user.
/// </summary>
Expand Down Expand Up @@ -279,11 +284,28 @@ protected static async Task<string> DownloadPackageFile(string installerUrl)
{
Logger.InfoLocalized(nameof(Resources.DownloadInstaller_Message), installerUrl);

// Do not download if we have already seen this URL.
if (DownloadedInstallers.ContainsKey(installerUrl))
{
string packageFilePath = DownloadedInstallers[installerUrl];

// Check if file has not been deleted mid-execution.
if (File.Exists(packageFilePath))
{
return packageFilePath;
}
else
{
DownloadedInstallers.Remove(installerUrl);
}
}

try
{
string packageFile = await PackageParser.DownloadFileAsync(installerUrl);
string packageFilePath = await PackageParser.DownloadFileAsync(installerUrl);
TelemetryManager.Log.WriteEvent(new DownloadInstallerEvent { IsSuccessful = true });
return packageFile;
DownloadedInstallers.Add(installerUrl, packageFilePath);
return packageFilePath;
}
catch (Exception e)
{
Expand Down

0 comments on commit 19b4bdc

Please sign in to comment.