diff --git a/internal/logic/p2p/client.go b/internal/logic/p2p/client.go index 388d747..c960cd2 100644 --- a/internal/logic/p2p/client.go +++ b/internal/logic/p2p/client.go @@ -456,7 +456,7 @@ func (s *sP2P) receiveGatewayMessages(ctx context.Context) { g.Log().Info(ctx, "文件接收完成") // 开始覆盖文件与重启 - err = service.System().Update(ctx) + err = service.System().Update(ctx, "") //// 调用不同系统的更新服务 //service.OS().Update(msgData.Version, msgData.Server) diff --git a/internal/logic/system/update.go b/internal/logic/system/update.go index 8f156b2..95d8db8 100644 --- a/internal/logic/system/update.go +++ b/internal/logic/system/update.go @@ -48,15 +48,23 @@ type GitHubRelease struct { Body string `json:"body"` } -func (s *sSystem) Update(ctx context.Context) (err error) { +func (s *sSystem) Update(ctx context.Context, gzFile string) (err error) { //拼接操作系统和架构(格式:OS_ARCH) platform := fmt.Sprintf("%s_%s", runtime.GOOS, runtime.GOARCH) runFile := gcmd.GetArg(0).String() oldFile, err := service.System().RenameRunningFile(runFile) g.Log().Debugf(ctx, "执行文件改名为%v", oldFile) - gz := path.Join("download", platform+".gz") - err = gcompress.UnGzipFile(gz, runFile) + if gzFile == "" { + gzFile = path.Join("download", platform+".gz") + } + + ext := gfile.Ext(gzFile) + if ext == "zip" { + err = gcompress.UnZipFile(gzFile, runFile) + } else { + err = gcompress.UnGzipFile(gzFile, runFile) + } go func() { log.Println("5秒后开始重启...") @@ -186,13 +194,18 @@ func (s *sSystem) CheckUpdate() { if strings.Contains(fmt.Sprintf("_%s.", asset.Name), platform) { fmt.Printf("- %s\n", asset.BrowserDownloadUrl) + // 下载更新文件 fileDownload, err2 := g.Client().Get(ctx, asset.BrowserDownloadUrl) if err2 != nil { return } - //filename := gfile.Name() - err = gfile.PutBytes(path.Join("download", asset.Name), fileDownload.ReadAll()) + updateFile := path.Join("download", asset.Name) + err = gfile.PutBytes(updateFile, fileDownload.ReadAll()) + err = s.Update(ctx, updateFile) + if err != nil { + return + } // 保存最新版本号到文件 gfile.PutContents("download/version.txt", latestVersion) break diff --git a/internal/service/system.go b/internal/service/system.go index 4b9fb3f..9a2cea4 100644 --- a/internal/service/system.go +++ b/internal/service/system.go @@ -12,7 +12,7 @@ import ( type ( ISystem interface { Init() - Update(ctx context.Context) (err error) + Update(ctx context.Context, gzFile string) (err error) // RestartSelf 实现 Windows 平台下的程序自重启 RestartSelf() error // RenameRunningFile 重命名正在运行的程序文件(如 message.exe → message.exe~)