Skip to content

Commit d36cd80

Browse files
Merge pull request #732 from TheTrackerCouncil/auto-download-updates
Download Windows setup files
2 parents 6a98eea + aa3c806 commit d36cd80

File tree

2 files changed

+87
-4
lines changed

2 files changed

+87
-4
lines changed

src/TrackerCouncil.Smz3.UI/Services/MainWindowService.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
2+
using System.Diagnostics;
23
using System.IO;
34
using System.Linq;
5+
using System.Net.Http;
46
using System.Threading.Tasks;
57
using AppImageManager;
68
using Avalonia.Threading;
@@ -212,6 +214,70 @@ await Dispatcher.UIThread.InvokeAsync(async () =>
212214
trackerSpriteService.LoadSprites();
213215
}
214216

217+
public async Task<string?> InstallWindowsUpdate(string url)
218+
{
219+
var filename = Path.GetFileName(new Uri(url).AbsolutePath);
220+
var localPath = Path.Combine(Path.GetTempPath(), filename);
221+
222+
logger.LogInformation("Downloading {Url} to {LocalPath}", url, localPath);
223+
224+
var response = await DownloadFileAsyncAttempt(url, localPath);
225+
226+
if (!response.Item1)
227+
{
228+
logger.LogInformation("Download failed: {Error}", response.Item2);
229+
return response.Item2;
230+
}
231+
232+
try
233+
{
234+
logger.LogInformation("Launching setup file");
235+
236+
var psi = new ProcessStartInfo
237+
{
238+
FileName = localPath,
239+
UseShellExecute = true,
240+
RedirectStandardOutput = false,
241+
RedirectStandardError = false,
242+
RedirectStandardInput = false,
243+
CreateNoWindow = true
244+
};
245+
246+
Process.Start(psi);
247+
return null;
248+
}
249+
catch (Exception e)
250+
{
251+
return "Failed to start setup file";
252+
}
253+
}
254+
255+
private static async Task<(bool, string?)> DownloadFileAsyncAttempt(string url, string target, int attemptNumber = 0, int totalAttempts = 3)
256+
{
257+
258+
using var httpClient = new HttpClient();
259+
260+
try
261+
{
262+
await using var downloadStream = await httpClient.GetStreamAsync(url);
263+
await using var fileStream = new FileStream(target, FileMode.Create);
264+
await downloadStream.CopyToAsync(fileStream);
265+
return (true, null);
266+
}
267+
catch (Exception ex)
268+
{
269+
if (attemptNumber < totalAttempts)
270+
{
271+
await Task.Delay(TimeSpan.FromSeconds(attemptNumber));
272+
return await DownloadFileAsyncAttempt(url, target, attemptNumber + 1, totalAttempts);
273+
}
274+
else
275+
{
276+
return (false, $"Download failed: {ex.Message}");
277+
}
278+
}
279+
}
280+
215281
private async Task CheckForUpdates()
216282
{
217283
if (!_options.GeneralOptions.CheckForUpdatesOnStartup)
@@ -235,6 +301,12 @@ private async Task CheckForUpdates()
235301
.FirstOrDefault(x => x.Url.ToLower().EndsWith(".appimage"))?.Url;
236302
_model.DisplayDownloadLink = !string.IsNullOrEmpty(_model.NewVersionDownloadUrl);
237303
}
304+
else if (OperatingSystem.IsWindows())
305+
{
306+
_model.NewVersionDownloadUrl = gitHubRelease.Asset
307+
.FirstOrDefault(x => x.Url.ToLower().EndsWith(".exe"))?.Url;
308+
_model.DisplayDownloadLink = !string.IsNullOrEmpty(_model.NewVersionDownloadUrl);
309+
}
238310
}
239311
}
240312
catch (Exception ex)

src/TrackerCouncil.Smz3.UI/Views/MainWindow.axaml.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private async void DownloadReleaseButton_OnClick(object? sender, RoutedEventArgs
137137
{
138138
try
139139
{
140-
if (string.IsNullOrEmpty(_model.NewVersionDownloadUrl))
140+
if (string.IsNullOrEmpty(_model.NewVersionDownloadUrl) || _service == null)
141141
{
142142
return;
143143
}
@@ -162,11 +162,22 @@ private async void DownloadReleaseButton_OnClick(object? sender, RoutedEventArgs
162162
await MessageWindow.ShowErrorDialog("Failed downloading AppImage");
163163
}
164164
}
165+
else
166+
{
167+
var result = await _service.InstallWindowsUpdate(_model.NewVersionDownloadUrl);
168+
if (!string.IsNullOrEmpty(result))
169+
{
170+
await MessageWindow.ShowErrorDialog(result);
171+
}
172+
else
173+
{
174+
Close();
175+
}
176+
}
165177
}
166-
catch (Exception exception)
178+
catch
167179
{
168-
Console.WriteLine(exception);
169-
throw;
180+
await MessageWindow.ShowErrorDialog("Failed downloading update");
170181
}
171182
}
172183
}

0 commit comments

Comments
 (0)