mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-01-19 16:29:17 +08:00
feat: 1Panel 重启增加同步应用商店 (#2479)
This commit is contained in:
parent
256be27f92
commit
f61345c18d
@ -50,11 +50,8 @@ func (b *BaseApi) SyncApp(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
go func() {
|
go func() {
|
||||||
global.LOG.Infof("sync app list start ...")
|
|
||||||
if err := appService.SyncAppListFromRemote(); err != nil {
|
if err := appService.SyncAppListFromRemote(); err != nil {
|
||||||
global.LOG.Errorf("sync app list error [%s]", err.Error())
|
global.LOG.Errorf("Synchronization with the App Store failed [%s]", err.Error())
|
||||||
} else {
|
|
||||||
global.LOG.Infof("sync app list success!")
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
helper.SuccessWithData(c, "")
|
helper.SuccessWithData(c, "")
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package response
|
package response
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
||||||
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
|
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -15,6 +16,7 @@ type AppRes struct {
|
|||||||
type AppUpdateRes struct {
|
type AppUpdateRes struct {
|
||||||
CanUpdate bool `json:"canUpdate"`
|
CanUpdate bool `json:"canUpdate"`
|
||||||
AppStoreLastModified int `json:"appStoreLastModified"`
|
AppStoreLastModified int `json:"appStoreLastModified"`
|
||||||
|
AppList *dto.AppList `json:"appList"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type AppDTO struct {
|
type AppDTO struct {
|
||||||
|
@ -456,11 +456,11 @@ func (a AppService) SyncAppListFromLocal() {
|
|||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.LOG.Errorf("sync app failed %v", err)
|
global.LOG.Errorf("Sync local app failed %v", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
global.LOG.Infof("start sync local apps...")
|
global.LOG.Infof("Starting local application synchronization ...")
|
||||||
dirEntries, err = os.ReadDir(localAppDir)
|
dirEntries, err = os.ReadDir(localAppDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
@ -666,17 +666,14 @@ func (a AppService) SyncAppListFromLocal() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
tx.Commit()
|
tx.Commit()
|
||||||
global.LOG.Infof("sync local apps success")
|
global.LOG.Infof("Synchronization of local applications completed")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a AppService) GetAppUpdate() (*response.AppUpdateRes, error) {
|
func (a AppService) GetAppUpdate() (*response.AppUpdateRes, error) {
|
||||||
res := &response.AppUpdateRes{
|
res := &response.AppUpdateRes{
|
||||||
CanUpdate: false,
|
CanUpdate: false,
|
||||||
}
|
}
|
||||||
setting, err := NewISettingService().GetSettingInfo()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
versionUrl := fmt.Sprintf("%s/%s/1panel.json.version.txt", global.CONF.System.AppRepo, global.CONF.System.Mode)
|
versionUrl := fmt.Sprintf("%s/%s/1panel.json.version.txt", global.CONF.System.AppRepo, global.CONF.System.Mode)
|
||||||
versionRes, err := http2.GetHttpRes(versionUrl)
|
versionRes, err := http2.GetHttpRes(versionUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -688,38 +685,36 @@ func (a AppService) GetAppUpdate() (*response.AppUpdateRes, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
lastModifiedStr := string(body)
|
lastModifiedStr := string(body)
|
||||||
|
|
||||||
lastModified, err := strconv.Atoi(lastModifiedStr)
|
lastModified, err := strconv.Atoi(lastModifiedStr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
setting, err := NewISettingService().GetSettingInfo()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
appStoreLastModified, _ := strconv.Atoi(setting.AppStoreLastModified)
|
appStoreLastModified, _ := strconv.Atoi(setting.AppStoreLastModified)
|
||||||
res.AppStoreLastModified = appStoreLastModified
|
res.AppStoreLastModified = appStoreLastModified
|
||||||
if setting.AppStoreLastModified == "" || lastModified != appStoreLastModified {
|
if setting.AppStoreLastModified == "" || lastModified != appStoreLastModified {
|
||||||
res.CanUpdate = true
|
res.CanUpdate = true
|
||||||
return res, err
|
return res, err
|
||||||
}
|
}
|
||||||
if err = getAppFromRepo(fmt.Sprintf("%s/%s/1panel.json.zip", global.CONF.System.AppRepo, global.CONF.System.Mode)); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
listFile := path.Join(constant.ResourceDir, "1panel.json")
|
|
||||||
content, err := os.ReadFile(listFile)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
list := &dto.AppList{}
|
list := &dto.AppList{}
|
||||||
if err = json.Unmarshal(content, list); err != nil {
|
list, err = getAppList()
|
||||||
return nil, err
|
if err != nil {
|
||||||
|
return res, err
|
||||||
}
|
}
|
||||||
if list.Extra.Version != "" && setting.SystemVersion != list.Extra.Version && !common.CompareVersion(setting.SystemVersion, list.Extra.Version) {
|
if list.Extra.Version != "" && setting.SystemVersion != list.Extra.Version && !common.CompareVersion(setting.SystemVersion, list.Extra.Version) {
|
||||||
|
global.LOG.Errorf("The current version is too low to synchronize with the App Store. The minimum required version is %s", list.Extra.Version)
|
||||||
return nil, buserr.New("ErrVersionTooLow")
|
return nil, buserr.New("ErrVersionTooLow")
|
||||||
}
|
}
|
||||||
|
res.AppList = list
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAppFromRepo(downloadPath string) error {
|
func getAppFromRepo(downloadPath string) error {
|
||||||
downloadUrl := downloadPath
|
downloadUrl := downloadPath
|
||||||
global.LOG.Infof("download file from %s", downloadUrl)
|
global.LOG.Infof("[AppStore] download file from %s", downloadUrl)
|
||||||
fileOp := files.NewFileOp()
|
fileOp := files.NewFileOp()
|
||||||
packagePath := path.Join(constant.ResourceDir, path.Base(downloadUrl))
|
packagePath := path.Join(constant.ResourceDir, path.Base(downloadUrl))
|
||||||
if err := fileOp.DownloadFile(downloadUrl, packagePath); err != nil {
|
if err := fileOp.DownloadFile(downloadUrl, packagePath); err != nil {
|
||||||
@ -734,26 +729,41 @@ func getAppFromRepo(downloadPath string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getAppList() (*dto.AppList, error) {
|
||||||
|
list := &dto.AppList{}
|
||||||
|
if err := getAppFromRepo(fmt.Sprintf("%s/%s/1panel.json.zip", global.CONF.System.AppRepo, global.CONF.System.Mode)); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
listFile := path.Join(constant.ResourceDir, "1panel.json")
|
||||||
|
content, err := os.ReadFile(listFile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if err = json.Unmarshal(content, list); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return list, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (a AppService) SyncAppListFromRemote() (err error) {
|
func (a AppService) SyncAppListFromRemote() (err error) {
|
||||||
|
global.LOG.Infof("Starting synchronization with App Store...")
|
||||||
updateRes, err := a.GetAppUpdate()
|
updateRes, err := a.GetAppUpdate()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !updateRes.CanUpdate {
|
if !updateRes.CanUpdate {
|
||||||
|
global.LOG.Infof("The App Store is at the latest version")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err = getAppFromRepo(fmt.Sprintf("%s/%s/1panel.json.zip", global.CONF.System.AppRepo, global.CONF.System.Mode)); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
listFile := path.Join(constant.ResourceDir, "1panel.json")
|
|
||||||
content, err := os.ReadFile(listFile)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
list := &dto.AppList{}
|
list := &dto.AppList{}
|
||||||
if err = json.Unmarshal(content, list); err != nil {
|
if updateRes.AppList == nil {
|
||||||
|
list, err = getAppList()
|
||||||
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
list = updateRes.AppList
|
||||||
|
}
|
||||||
|
|
||||||
if err = NewISettingService().Update("AppStoreLastModified", strconv.Itoa(list.LastModified)); err != nil {
|
if err = NewISettingService().Update("AppStoreLastModified", strconv.Itoa(list.LastModified)); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -964,5 +974,6 @@ func (a AppService) SyncAppListFromRemote() (err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
tx.Commit()
|
tx.Commit()
|
||||||
|
global.LOG.Infof("Synchronization with the App Store was successful!")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -6,26 +6,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Init() {
|
func Init() {
|
||||||
syncApp()
|
go syncApp()
|
||||||
syncInstalledApp()
|
go syncInstalledApp()
|
||||||
}
|
}
|
||||||
|
|
||||||
func syncApp() {
|
func syncApp() {
|
||||||
setting, err := service.NewISettingService().GetSettingInfo()
|
|
||||||
if err != nil {
|
|
||||||
global.LOG.Errorf("sync app error: %s", err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if setting.AppStoreLastModified != "0" {
|
|
||||||
global.LOG.Info("no need to sync")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
global.LOG.Info("sync app start...")
|
|
||||||
if err := service.NewIAppService().SyncAppListFromRemote(); err != nil {
|
if err := service.NewIAppService().SyncAppListFromRemote(); err != nil {
|
||||||
global.LOG.Errorf("sync app error")
|
global.LOG.Errorf("App Store synchronization failed")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
global.LOG.Info("sync app successful")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func syncInstalledApp() {
|
func syncInstalledApp() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user