11using System ;
2+ using System . Diagnostics ;
23using System . IO ;
34using System . Linq ;
5+ using System . Net . Http ;
46using System . Threading . Tasks ;
57using AppImageManager ;
68using 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 )
0 commit comments