diff --git a/Ink Canvas/Helpers/AutoUpdateHelper.cs b/Ink Canvas/Helpers/AutoUpdateHelper.cs index 1b35cd53..7724f9e6 100644 --- a/Ink Canvas/Helpers/AutoUpdateHelper.cs +++ b/Ink Canvas/Helpers/AutoUpdateHelper.cs @@ -480,9 +480,9 @@ namespace Ink_Canvas.Helpers { string url = string.Format(group.DownloadUrlFormat, version); LogHelper.WriteLogToFile($"AutoUpdate | 尝试从线路组 {group.GroupName} 下载: {url}"); - + bool downloadSuccess = await DownloadFile(url, zipFilePath, progressCallback); - + if (downloadSuccess) { SaveDownloadStatus(true); @@ -495,7 +495,7 @@ namespace Ink_Canvas.Helpers LogHelper.WriteLogToFile($"AutoUpdate | 线路组 {group.GroupName} 下载失败,尝试下一个线路组", LogHelper.LogType.Warning); } } - + LogHelper.WriteLogToFile("AutoUpdate | 所有线路组下载均失败", LogHelper.LogType.Error); progressCallback?.Invoke(0, "所有线路组下载均失败"); return false; @@ -525,8 +525,8 @@ namespace Ink_Canvas.Helpers if (totalSize <= 0) { progressCallback?.Invoke(0, "无法获取文件大小,取消下载"); - return false; - } + return false; + } int blockSize = (int)Math.Ceiling((double)totalSize / threadCount); long[] blockDownloaded = new long[threadCount]; var tasks = new List(); @@ -600,8 +600,8 @@ namespace Ink_Canvas.Helpers string tempPath = destinationPath + $".part{i}"; if (File.Exists(tempPath)) File.Delete(tempPath); } - return false; - } + return false; + } // 3. 合并所有块 using (var output = new FileStream(destinationPath, FileMode.Create, FileAccess.Write, FileShare.None)) { @@ -616,7 +616,7 @@ namespace Ink_Canvas.Helpers } } progressCallback?.Invoke(100, "多线程下载完成"); - return true; + return true; } // 获取文件总大小 @@ -630,9 +630,9 @@ namespace Ink_Canvas.Helpers var resp = await client.SendAsync(req); if (resp.IsSuccessStatusCode && resp.Content.Headers.ContentLength.HasValue) return resp.Content.Headers.ContentLength.Value; - } - } - catch { } + } + } + catch { } return -1; } @@ -1072,6 +1072,36 @@ namespace Ink_Canvas.Helpers return false; } } + + /// + /// 启动手动指定版本的多线路多线程下载并自动安装(用于历史版本回滚等场景) + /// + public static async Task StartManualDownloadAndInstall(string version, UpdateChannel channel, Action progressCallback = null) + { + try + { + var groups = ChannelLineGroups[channel]; + bool downloadSuccess = await DownloadSetupFileWithFallback(version, groups, progressCallback); + if (!downloadSuccess) + { + LogHelper.WriteLogToFile($"AutoUpdate | 手动下载版本{version}失败"); + return false; + } + LogHelper.WriteLogToFile($"AutoUpdate | 手动安装版本: {version}"); + InstallNewVersionApp(version, false); + App.IsAppExitByUser = true; + Application.Current.Dispatcher.Invoke(() => { + Application.Current.Shutdown(); + }); + return true; + } + catch (Exception ex) + { + LogHelper.WriteLogToFile($"AutoUpdate | 手动下载或安装异常: {ex.Message}", LogHelper.LogType.Error); + progressCallback?.Invoke(0, $"下载异常: {ex.Message}"); + return false; + } + } } internal class AutoUpdateWithSilenceTimeComboBox diff --git a/Ink Canvas/Windows/HistoryRollbackWindow.xaml.cs b/Ink Canvas/Windows/HistoryRollbackWindow.xaml.cs index 13f69c55..88621545 100644 --- a/Ink Canvas/Windows/HistoryRollbackWindow.xaml.cs +++ b/Ink Canvas/Windows/HistoryRollbackWindow.xaml.cs @@ -95,53 +95,13 @@ namespace Ink_Canvas DownloadProgressPanel.Visibility = Visibility.Visible; DownloadProgressBar.Value = 0; DownloadProgressText.Text = "正在准备下载..."; - bool downloadSuccess = false; - downloadCts = new CancellationTokenSource(); - try - { - downloadSuccess = await DownloadAndInstallVersion(selectedItem.Version, selectedItem.DownloadUrl, downloadCts.Token); - } - catch (OperationCanceledException) - { - DownloadProgressText.Text = "下载已取消。"; - LogHelper.WriteLogToFile($"HistoryRollback | 用户取消下载", LogHelper.LogType.Info); - } - catch (Exception ex) - { - DownloadProgressText.Text = $"下载失败: {ex.Message}"; - LogHelper.WriteLogToFile($"HistoryRollback | 下载异常: {ex.Message}", LogHelper.LogType.Error); - } - if (downloadSuccess) - { - DownloadProgressBar.Value = 100; - DownloadProgressText.Text = "下载完成,准备安装..."; - await Task.Delay(800); - LogHelper.WriteLogToFile($"HistoryRollback | 版本 {selectedItem.Version} 下载并准备安装成功"); - this.DialogResult = true; - this.Close(); - } - else if (!downloadCts.IsCancellationRequested) - { - DownloadProgressText.Text = "下载失败,请检查网络后重试。"; - LogHelper.WriteLogToFile($"HistoryRollback | 版本 {selectedItem?.Version} 下载失败", LogHelper.LogType.Error); - RollbackButton.IsEnabled = true; - VersionComboBox.IsEnabled = true; - } - } - private async Task DownloadAndInstallVersion(string version, string downloadUrl, CancellationToken token) - { - LogHelper.WriteLogToFile($"HistoryRollback | 开始下载版本: {version}, url: {downloadUrl}"); - string updatesFolderPath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "AutoUpdate"); - if (!Directory.Exists(updatesFolderPath)) - Directory.CreateDirectory(updatesFolderPath); - string zipFilePath = Path.Combine(updatesFolderPath, $"InkCanvasForClass.CE.{version}.zip"); bool downloadSuccess = false; try { - downloadSuccess = await AutoUpdateHelper.DownloadFile( - downloadUrl, - zipFilePath, + downloadSuccess = await AutoUpdateHelper.StartManualDownloadAndInstall( + selectedItem.Version, + channel, (percent, text) => { Dispatcher.Invoke(() => { @@ -150,32 +110,26 @@ namespace Ink_Canvas }); } ); - if (!downloadSuccess) - { - LogHelper.WriteLogToFile($"HistoryRollback | 多线程下载失败"); - return false; - } - // 下载完成后,调用现有安装流程 - LogHelper.WriteLogToFile($"HistoryRollback | 开始安装版本: {version}"); - AutoUpdateHelper.InstallNewVersionApp(version, false); - App.IsAppExitByUser = true; - Application.Current.Dispatcher.Invoke(() => { - Application.Current.Shutdown(); - }); - return true; - } - catch (OperationCanceledException) - { - LogHelper.WriteLogToFile($"HistoryRollback | 用户取消下载", LogHelper.LogType.Info); - return false; } catch (Exception ex) { - LogHelper.WriteLogToFile($"HistoryRollback | 下载或安装异常: {ex.Message}", LogHelper.LogType.Error); - Dispatcher.Invoke(() => { - DownloadProgressText.Text = $"下载异常: {ex.Message}"; - }); - return false; + DownloadProgressText.Text = $"下载失败: {ex.Message}"; + LogHelper.WriteLogToFile($"HistoryRollback | 下载异常: {ex.Message}", LogHelper.LogType.Error); + } + + if (downloadSuccess) + { + DownloadProgressBar.Value = 100; + DownloadProgressText.Text = "下载完成,准备安装..."; + await Task.Delay(800); + this.DialogResult = true; + this.Close(); + } + else + { + DownloadProgressText.Text = "下载失败,请检查网络后重试。"; + RollbackButton.IsEnabled = true; + VersionComboBox.IsEnabled = true; } }