improve:自动更新
This commit is contained in:
@@ -525,8 +525,8 @@ namespace Ink_Canvas.Helpers
|
|||||||
if (totalSize <= 0)
|
if (totalSize <= 0)
|
||||||
{
|
{
|
||||||
progressCallback?.Invoke(0, "无法获取文件大小,取消下载");
|
progressCallback?.Invoke(0, "无法获取文件大小,取消下载");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
int blockSize = (int)Math.Ceiling((double)totalSize / threadCount);
|
int blockSize = (int)Math.Ceiling((double)totalSize / threadCount);
|
||||||
long[] blockDownloaded = new long[threadCount];
|
long[] blockDownloaded = new long[threadCount];
|
||||||
var tasks = new List<Task>();
|
var tasks = new List<Task>();
|
||||||
@@ -600,8 +600,8 @@ namespace Ink_Canvas.Helpers
|
|||||||
string tempPath = destinationPath + $".part{i}";
|
string tempPath = destinationPath + $".part{i}";
|
||||||
if (File.Exists(tempPath)) File.Delete(tempPath);
|
if (File.Exists(tempPath)) File.Delete(tempPath);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// 3. 合并所有块
|
// 3. 合并所有块
|
||||||
using (var output = new FileStream(destinationPath, FileMode.Create, FileAccess.Write, FileShare.None))
|
using (var output = new FileStream(destinationPath, FileMode.Create, FileAccess.Write, FileShare.None))
|
||||||
{
|
{
|
||||||
@@ -616,7 +616,7 @@ namespace Ink_Canvas.Helpers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
progressCallback?.Invoke(100, "多线程下载完成");
|
progressCallback?.Invoke(100, "多线程下载完成");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取文件总大小
|
// 获取文件总大小
|
||||||
@@ -630,9 +630,9 @@ namespace Ink_Canvas.Helpers
|
|||||||
var resp = await client.SendAsync(req);
|
var resp = await client.SendAsync(req);
|
||||||
if (resp.IsSuccessStatusCode && resp.Content.Headers.ContentLength.HasValue)
|
if (resp.IsSuccessStatusCode && resp.Content.Headers.ContentLength.HasValue)
|
||||||
return resp.Content.Headers.ContentLength.Value;
|
return resp.Content.Headers.ContentLength.Value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch { }
|
catch { }
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1072,6 +1072,36 @@ namespace Ink_Canvas.Helpers
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 启动手动指定版本的多线路多线程下载并自动安装(用于历史版本回滚等场景)
|
||||||
|
/// </summary>
|
||||||
|
public static async Task<bool> StartManualDownloadAndInstall(string version, UpdateChannel channel, Action<double, string> 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
|
internal class AutoUpdateWithSilenceTimeComboBox
|
||||||
|
|||||||
@@ -95,53 +95,13 @@ namespace Ink_Canvas
|
|||||||
DownloadProgressPanel.Visibility = Visibility.Visible;
|
DownloadProgressPanel.Visibility = Visibility.Visible;
|
||||||
DownloadProgressBar.Value = 0;
|
DownloadProgressBar.Value = 0;
|
||||||
DownloadProgressText.Text = "正在准备下载...";
|
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<bool> 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;
|
bool downloadSuccess = false;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
downloadSuccess = await AutoUpdateHelper.DownloadFile(
|
downloadSuccess = await AutoUpdateHelper.StartManualDownloadAndInstall(
|
||||||
downloadUrl,
|
selectedItem.Version,
|
||||||
zipFilePath,
|
channel,
|
||||||
(percent, text) =>
|
(percent, text) =>
|
||||||
{
|
{
|
||||||
Dispatcher.Invoke(() => {
|
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)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
LogHelper.WriteLogToFile($"HistoryRollback | 下载或安装异常: {ex.Message}", LogHelper.LogType.Error);
|
DownloadProgressText.Text = $"下载失败: {ex.Message}";
|
||||||
Dispatcher.Invoke(() => {
|
LogHelper.WriteLogToFile($"HistoryRollback | 下载异常: {ex.Message}", LogHelper.LogType.Error);
|
||||||
DownloadProgressText.Text = $"下载异常: {ex.Message}";
|
}
|
||||||
});
|
|
||||||
return false;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user