mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-01-19 08:19:15 +08:00
feat: 修改应用同步逻辑
This commit is contained in:
parent
d1dc8c6a50
commit
5d14ea1da0
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}()
|
||||
|
@ -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"`
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user