@@ -142,37 +142,15 @@ internal async Task InstallOrUpdateAsync(UserPlugin plugin)
142
142
143
143
var filePath = Path . Combine ( Path . GetTempPath ( ) , downloadFilename ) ;
144
144
145
- var exceptionHappened = false ;
146
145
try
147
146
{
148
147
using var cts = new CancellationTokenSource ( ) ;
149
148
150
149
if ( ! plugin . IsFromLocalInstallPath )
151
150
{
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 ) ;
176
154
}
177
155
else
178
156
{
@@ -221,6 +199,34 @@ await Context.API.ShowProgressBoxAsync(prgBoxTitle,
221
199
}
222
200
}
223
201
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
+
224
230
internal async ValueTask < List < Result > > RequestUpdateAsync ( string search , CancellationToken token ,
225
231
bool usePrimaryUrlOnly = false )
226
232
{
@@ -308,43 +314,48 @@ where string.Compare(existingPlugin.Metadata.Version, pluginUpdateSource.Version
308
314
309
315
_ = Task . Run ( async delegate
310
316
{
317
+ using var cts = new CancellationTokenSource ( ) ;
318
+
311
319
if ( ! x . PluginNewUserPlugin . IsFromLocalInstallPath )
312
320
{
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 ) ;
320
324
}
321
325
else
322
326
{
323
327
downloadToFilePath = x . PluginNewUserPlugin . LocalInstallPath ;
324
328
}
325
329
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 )
331
332
{
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 ;
339
334
}
340
335
else
341
336
{
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
+ }
348
359
}
349
360
} ) . ContinueWith ( t =>
350
361
{
@@ -405,16 +416,18 @@ await Task.WhenAll(resultsForUpdate.Select(async plugin =>
405
416
406
417
try
407
418
{
408
- if ( File . Exists ( downloadToFilePath ) )
409
- {
410
- File . Delete ( downloadToFilePath ) ;
411
- }
419
+ using var cts = new CancellationTokenSource ( ) ;
412
420
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 ) ;
415
424
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 ) ;
418
431
}
419
432
catch ( Exception ex )
420
433
{
0 commit comments