From 5d14ea1da09968c6036590c3af80cdefc2cc8104 Mon Sep 17 00:00:00 2001 From: zhengkunwang223 Date: Tue, 14 Feb 2023 11:17:02 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/dto/response/app.go | 13 +++-- backend/app/service/app.go | 48 ++++++++++------ backend/app/service/app_utils.go | 20 ++----- backend/configs/system.go | 1 + backend/utils/git/git.go | 63 +++++++++++++++++++++ frontend/src/api/interface/app.ts | 3 - frontend/src/views/app-store/apps/index.vue | 1 - 7 files changed, 108 insertions(+), 41 deletions(-) diff --git a/backend/app/dto/response/app.go b/backend/app/dto/response/app.go index 3426a482f..b6a92520a 100644 --- a/backend/app/dto/response/app.go +++ b/backend/app/dto/response/app.go @@ -6,11 +6,14 @@ import ( ) type AppRes struct { - Version string `json:"version"` - CanUpdate bool `json:"canUpdate"` - Items []*AppDTO `json:"items"` - Tags []model.Tag `json:"tags"` - Total int64 `json:"total"` + Items []*AppDTO `json:"items"` + Total int64 `json:"total"` +} + +type AppUpdateRes struct { + Version string `json:"version"` + CanUpdate bool `json:"canUpdate"` + DownloadPath string `json:"downloadPath"` } type AppDTO struct { diff --git a/backend/app/service/app.go b/backend/app/service/app.go index cc466326f..cc7453132 100644 --- a/backend/app/service/app.go +++ b/backend/app/service/app.go @@ -98,23 +98,7 @@ func (a AppService) PageApp(req request.AppSearch) (interface{}, error) { } res.Items = appDTOs res.Total = total - tags, err := tagRepo.All() - if err != nil { - return nil, err - } - res.Tags = tags - setting, err := NewISettingService().GetSettingInfo() - if err != nil { - return nil, err - } - repoInfo, err := git.CheckAndGetInfo(global.CONF.System.AppRepoOwner, global.CONF.System.AppRepoName) - if err != nil { - return nil, err - } - if common.CompareVersion(repoInfo.Version, setting.AppStoreVersion) { - res.CanUpdate = true - } return res, nil } @@ -351,8 +335,38 @@ func (a AppService) SyncInstalled(installId uint) error { return appInstallRepo.Save(&appInstall) } +func (a AppService) GetAppUpdate() (*response.AppUpdateRes, error) { + res := &response.AppUpdateRes{} + setting, err := NewISettingService().GetSettingInfo() + if err != nil { + return nil, err + } + contentRes, err := git.CheckAndGetContent(global.CONF.System.AppRepoOwner, global.CONF.System.AppRepoName, setting.SystemVersion, "apps/list.json") + if err != nil { + return nil, err + } + list := &dto.AppList{} + if err = json.Unmarshal(contentRes.Content, list); err != nil { + return nil, err + } + res.Version = list.Version + if common.CompareVersion(list.Version, setting.AppStoreVersion) { + res.CanUpdate = true + res.DownloadPath = contentRes.DownloadPath + return res, err + } + return nil, err +} + func (a AppService) SyncAppList() error { - if err := getAppFromRepo(); err != nil { + updateRes, err := a.GetAppUpdate() + if err != nil { + return err + } + if !updateRes.CanUpdate { + return nil + } + if err := getAppFromRepo(updateRes.DownloadPath, updateRes.Version); err != nil { global.LOG.Errorf("get app from oss error: %s", err.Error()) return err } diff --git a/backend/app/service/app_utils.go b/backend/app/service/app_utils.go index 7f9099622..ab26a4cb3 100644 --- a/backend/app/service/app_utils.go +++ b/backend/app/service/app_utils.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "github.com/1Panel-dev/1Panel/backend/utils/git" "math" "os" "path" @@ -499,20 +498,11 @@ func handleErr(install model.AppInstall, err error, out string) error { return reErr } -func getAppFromRepo() error { - repoInfo, err := git.CheckAndGetInfo(global.CONF.System.AppRepoOwner, global.CONF.System.AppRepoName) - if err != nil { - return err - } +func getAppFromRepo(downloadPath, version string) error { + downloadUrl := fmt.Sprintf("%s/%s/apps-%s.tar.gz", downloadPath, version, version) appDir := constant.AppResourceDir - setting, err := NewISettingService().GetSettingInfo() - if err != nil { - return err - } - if !common.CompareVersion(repoInfo.Version, setting.AppStoreVersion) { - return nil - } - downloadUrl := fmt.Sprintf("%sapps-%s.tar.gz", repoInfo.DownloadPath, repoInfo.Version) + + global.LOG.Infof("download file from %s", downloadUrl) fileOp := files.NewFileOp() if _, err := fileOp.CopyAndBackup(appDir); err != nil { return err @@ -524,7 +514,7 @@ func getAppFromRepo() error { if err := fileOp.Decompress(packagePath, constant.ResourceDir, files.TarGz); err != nil { return err } - _ = NewISettingService().Update("AppStoreVersion", repoInfo.Version) + _ = NewISettingService().Update("AppStoreVersion", version) defer func() { _ = fileOp.DeleteFile(packagePath) }() diff --git a/backend/configs/system.go b/backend/configs/system.go index df968ceb1..2780e1b9e 100644 --- a/backend/configs/system.go +++ b/backend/configs/system.go @@ -12,4 +12,5 @@ type System struct { AppRepoName string `mapstructure:"app_repo_name"` EncryptKey string `mapstructure:"encrypt_key"` BaseDir string `mapstructure:"base_dir"` + Mode string `mapstructure:"mode"` } diff --git a/backend/utils/git/git.go b/backend/utils/git/git.go index 5b982484e..e5dfdfe4c 100644 --- a/backend/utils/git/git.go +++ b/backend/utils/git/git.go @@ -3,10 +3,12 @@ package git import ( "context" "crypto/tls" + "encoding/base64" "errors" "fmt" "gitee.com/openeuler/go-gitee/gitee" "github.com/1Panel-dev/1Panel/backend/global" + "github.com/antihax/optional" "github.com/google/go-github/github" "net/http" "time" @@ -20,6 +22,12 @@ type RepoInfo struct { DownloadPath string } +type RepoContent struct { + RepoType string + Content []byte + DownloadPath string +} + var gitRepoTypes = []string{"gitee", "github"} func CheckAndGetInfo(owner, repoName string) (*RepoInfo, error) { @@ -39,6 +47,23 @@ func CheckAndGetInfo(owner, repoName string) (*RepoInfo, error) { return nil, errors.New("all remote repo get failed") } +func CheckAndGetContent(owner, repoName, branch, path string) (*RepoContent, error) { + for _, repoType := range gitRepoTypes { + url := fmt.Sprintf("https://%s.com/%s/%s", repoType, owner, repoName) + if checkValid(url) { + res, err := getContentFromBranch(repoType, owner, repoName, branch, path) + if err == nil { + return res, nil + } else { + global.LOG.Errorf("get %s last version failed %s", repoType, err.Error()) + } + } else { + global.LOG.Errorf("check %s valid [%s] failed", repoType, url) + } + } + return nil, errors.New("all repo get failed") +} + func checkValid(addr string) bool { timeout := 2 * time.Second tr := &http.Transport{ @@ -82,3 +107,41 @@ func getLatestRepoInfo(repoType, owner, repoName string) (*RepoInfo, error) { } return &repoInfo, nil } + +func getContentFromBranch(repoType, owner, repoName, branch, path string) (*RepoContent, error) { + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + var repoContent RepoContent + repoContent.RepoType = repoType + if repoType == "gitee" { + client := gitee.NewAPIClient(gitee.NewConfiguration()) + content, res, err := client.RepositoriesApi.GetV5ReposOwnerRepoContentsPath(ctx, owner, repoName, path, &gitee.GetV5ReposOwnerRepoContentsPathOpts{ + Ref: optional.NewString(branch), + }) + if res.StatusCode != 200 || err != nil { + return nil, err + } + bs64Bytes, err := base64.StdEncoding.DecodeString(content.Content) + if err != nil { + return nil, err + } + repoContent.Content = bs64Bytes + repoContent.DownloadPath = fmt.Sprintf("https://gitee.com/%s/%s/releases/download/", owner, repoName) + return &repoContent, nil + } else { + client := github.NewClient(nil) + content, _, res, err := client.Repositories.GetContents(ctx, owner, repoName, path, &github.RepositoryContentGetOptions{ + Ref: branch, + }) + if res.StatusCode != 200 || err != nil { + return nil, err + } + bs64Bytes, err := base64.StdEncoding.DecodeString(*content.Content) + if err != nil { + return nil, err + } + repoContent.Content = bs64Bytes + repoContent.DownloadPath = fmt.Sprintf("https://github.com/%s/%s/releases/download", owner, repoName) + return &repoContent, nil + } +} diff --git a/frontend/src/api/interface/app.ts b/frontend/src/api/interface/app.ts index 843328c78..c5b3527d2 100644 --- a/frontend/src/api/interface/app.ts +++ b/frontend/src/api/interface/app.ts @@ -23,10 +23,7 @@ export namespace App { export interface AppResPage { total: number; - canUpdate: boolean; - version: string; items: App.App[]; - tags: App.Tag[]; } export interface AppDetail extends CommonModel { diff --git a/frontend/src/views/app-store/apps/index.vue b/frontend/src/views/app-store/apps/index.vue index 35da0da9c..a1a4084c7 100644 --- a/frontend/src/views/app-store/apps/index.vue +++ b/frontend/src/views/app-store/apps/index.vue @@ -126,7 +126,6 @@ const search = async (req: App.AppReq) => { await SearchApp(req) .then((res) => { apps.value = res.data.items; - canUpdate.value = res.data.canUpdate; }) .finally(() => { loading.value = false;