@@ -142,37 +142,15 @@ internal async Task InstallOrUpdateAsync(UserPlugin plugin)
142142
143143 var filePath = Path . Combine ( Path . GetTempPath ( ) , downloadFilename ) ;
144144
145- var exceptionHappened = false ;
146145 try
147146 {
148147 using var cts = new CancellationTokenSource ( ) ;
149148
150149 if ( ! plugin . IsFromLocalInstallPath )
151150 {
152- if ( File . Exists ( filePath ) )
153- File . Delete ( filePath ) ;
154-
155- var prgBoxTitle = $ "{ Context . API . GetTranslation ( "plugin_pluginsmanager_downloading_plugin" ) } { plugin . Name } ";
156- await Context . API . ShowProgressBoxAsync ( prgBoxTitle ,
157- async ( reportProgress ) =>
158- {
159- if ( reportProgress == null )
160- {
161- // when reportProgress is null, it means there is expcetion with the progress box
162- // so we record it with exceptionHappened and return so that progress box will close instantly
163- exceptionHappened = true ;
164- return ;
165- }
166- else
167- {
168- await Http . DownloadAsync ( plugin . UrlDownload , filePath , reportProgress , cts . Token ) . ConfigureAwait ( false ) ;
169- }
170- } , cts . Cancel ) ;
171-
172- // if exception happened while downloading and user does not cancel downloading,
173- // we need to redownload the plugin
174- if ( exceptionHappened && ( ! cts . IsCancellationRequested ) )
175- await Http . DownloadAsync ( plugin . UrlDownload , filePath , null , cts . Token ) . ConfigureAwait ( false ) ;
151+ await DeleteFileAndDownloadMsgBoxAsync (
152+ $ "{ Context . API . GetTranslation ( "plugin_pluginsmanager_downloading_plugin" ) } { plugin . Name } ",
153+ plugin . UrlDownload , filePath , cts ) ;
176154 }
177155 else
178156 {
@@ -221,6 +199,34 @@ await Context.API.ShowProgressBoxAsync(prgBoxTitle,
221199 }
222200 }
223201
202+ private async Task DeleteFileAndDownloadMsgBoxAsync ( string prgBoxTitle , string downloadUrl , string filePath , CancellationTokenSource cts )
203+ {
204+ if ( File . Exists ( filePath ) )
205+ File . Delete ( filePath ) ;
206+
207+ var exceptionHappened = false ;
208+ await Context . API . ShowProgressBoxAsync ( prgBoxTitle ,
209+ async ( reportProgress ) =>
210+ {
211+ if ( reportProgress == null )
212+ {
213+ // when reportProgress is null, it means there is expcetion with the progress box
214+ // so we record it with exceptionHappened and return so that progress box will close instantly
215+ exceptionHappened = true ;
216+ return ;
217+ }
218+ else
219+ {
220+ await Context . API . HttpDownloadAsync ( downloadUrl , filePath , reportProgress , cts . Token ) . ConfigureAwait ( false ) ;
221+ }
222+ } , cts . Cancel ) ;
223+
224+ // if exception happened while downloading and user does not cancel downloading,
225+ // we need to redownload the plugin
226+ if ( exceptionHappened && ( ! cts . IsCancellationRequested ) )
227+ await Context . API . HttpDownloadAsync ( downloadUrl , filePath ) . ConfigureAwait ( false ) ;
228+ }
229+
224230 internal async ValueTask < List < Result > > RequestUpdateAsync ( string search , CancellationToken token ,
225231 bool usePrimaryUrlOnly = false )
226232 {
@@ -308,43 +314,48 @@ where string.Compare(existingPlugin.Metadata.Version, pluginUpdateSource.Version
308314
309315 _ = Task . Run ( async delegate
310316 {
317+ using var cts = new CancellationTokenSource ( ) ;
318+
311319 if ( ! x . PluginNewUserPlugin . IsFromLocalInstallPath )
312320 {
313- if ( File . Exists ( downloadToFilePath ) )
314- {
315- File . Delete ( downloadToFilePath ) ;
316- }
317-
318- await Http . DownloadAsync ( x . PluginNewUserPlugin . UrlDownload , downloadToFilePath )
319- . ConfigureAwait ( false ) ;
321+ await DeleteFileAndDownloadMsgBoxAsync (
322+ $ "{ Context . API . GetTranslation ( "plugin_pluginsmanager_downloading_plugin" ) } { x . PluginNewUserPlugin . Name } ",
323+ x . PluginNewUserPlugin . UrlDownload , downloadToFilePath , cts ) ;
320324 }
321325 else
322326 {
323327 downloadToFilePath = x . PluginNewUserPlugin . LocalInstallPath ;
324328 }
325329
326-
327- PluginManager . UpdatePlugin ( x . PluginExistingMetadata , x . PluginNewUserPlugin ,
328- downloadToFilePath ) ;
329-
330- if ( Settings . AutoRestartAfterChanging )
330+ // check if user cancelled download before installing plugin
331+ if ( cts . IsCancellationRequested )
331332 {
332- Context . API . ShowMsg (
333- Context . API . GetTranslation ( "plugin_pluginsmanager_update_title" ) ,
334- string . Format (
335- Context . API . GetTranslation (
336- "plugin_pluginsmanager_update_success_restart" ) ,
337- x . Name ) ) ;
338- Context . API . RestartApp ( ) ;
333+ return ;
339334 }
340335 else
341336 {
342- Context . API . ShowMsg (
343- Context . API . GetTranslation ( "plugin_pluginsmanager_update_title" ) ,
344- string . Format (
345- Context . API . GetTranslation (
346- "plugin_pluginsmanager_update_success_no_restart" ) ,
347- x . Name ) ) ;
337+ PluginManager . UpdatePlugin ( x . PluginExistingMetadata , x . PluginNewUserPlugin ,
338+ downloadToFilePath ) ;
339+
340+ if ( Settings . AutoRestartAfterChanging )
341+ {
342+ Context . API . ShowMsg (
343+ Context . API . GetTranslation ( "plugin_pluginsmanager_update_title" ) ,
344+ string . Format (
345+ Context . API . GetTranslation (
346+ "plugin_pluginsmanager_update_success_restart" ) ,
347+ x . Name ) ) ;
348+ Context . API . RestartApp ( ) ;
349+ }
350+ else
351+ {
352+ Context . API . ShowMsg (
353+ Context . API . GetTranslation ( "plugin_pluginsmanager_update_title" ) ,
354+ string . Format (
355+ Context . API . GetTranslation (
356+ "plugin_pluginsmanager_update_success_no_restart" ) ,
357+ x . Name ) ) ;
358+ }
348359 }
349360 } ) . ContinueWith ( t =>
350361 {
@@ -405,16 +416,18 @@ await Task.WhenAll(resultsForUpdate.Select(async plugin =>
405416
406417 try
407418 {
408- if ( File . Exists ( downloadToFilePath ) )
409- {
410- File . Delete ( downloadToFilePath ) ;
411- }
419+ using var cts = new CancellationTokenSource ( ) ;
412420
413- await Http . DownloadAsync ( plugin . PluginNewUserPlugin . UrlDownload , downloadToFilePath )
414- . ConfigureAwait ( false ) ;
421+ await DeleteFileAndDownloadMsgBoxAsync (
422+ $ "{ Context . API . GetTranslation ( "plugin_pluginsmanager_downloading_plugin" ) } { plugin . PluginNewUserPlugin . Name } ",
423+ plugin . PluginNewUserPlugin . UrlDownload , downloadToFilePath , cts ) ;
415424
416- PluginManager . UpdatePlugin ( plugin . PluginExistingMetadata , plugin . PluginNewUserPlugin ,
417- downloadToFilePath ) ;
425+ // check if user cancelled download before installing plugin
426+ if ( cts . IsCancellationRequested )
427+ return ;
428+ else
429+ PluginManager . UpdatePlugin ( plugin . PluginExistingMetadata , plugin . PluginNewUserPlugin ,
430+ downloadToFilePath ) ;
418431 }
419432 catch ( Exception ex )
420433 {
0 commit comments