1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-31 22:18:07 +08:00

fix: 解决应用同步可能出现的多个同名应用的问题 (#1827)

This commit is contained in:
zhengkunwang 2023-08-03 18:37:57 +08:00 committed by GitHub
parent 70319aca45
commit e30546102e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -663,6 +663,7 @@ func (a AppService) GetAppUpdate() (*response.AppUpdateRes, error) {
return nil, err return nil, err
} }
appStoreLastModified, _ := strconv.Atoi(setting.AppStoreLastModified) appStoreLastModified, _ := strconv.Atoi(setting.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
@ -687,13 +688,13 @@ func getAppFromRepo(downloadPath string) error {
return nil return nil
} }
func (a AppService) SyncAppListFromRemote() error { func (a AppService) SyncAppListFromRemote() (err error) {
updateRes, err := a.GetAppUpdate() updateRes, err := a.GetAppUpdate()
if err != nil { if err != nil {
return err return err
} }
if !updateRes.CanUpdate { if !updateRes.CanUpdate {
return nil return
} }
if err = getAppFromRepo(fmt.Sprintf("%s/%s/1panel.json.zip", global.CONF.System.AppRepo, global.CONF.System.Mode)); err != nil { if err = getAppFromRepo(fmt.Sprintf("%s/%s/1panel.json.zip", global.CONF.System.AppRepo, global.CONF.System.Mode)); err != nil {
return err return err
@ -704,10 +705,19 @@ func (a AppService) SyncAppListFromRemote() error {
return err return err
} }
list := &dto.AppList{} list := &dto.AppList{}
if err := json.Unmarshal(content, list); err != nil { if err = json.Unmarshal(content, list); err != nil {
return
}
if err = settingRepo().Update("AppStoreLastModified", strconv.Itoa(list.LastModified)); err != nil {
return err return err
} }
defer func() {
if err != nil {
_ = settingRepo().Update("AppStoreLastModified", strconv.Itoa(updateRes.AppStoreLastModified))
}
}()
var ( var (
tags []*model.Tag tags []*model.Tag
appTags []*model.AppTag appTags []*model.AppTag
@ -721,7 +731,7 @@ func (a AppService) SyncAppListFromRemote() error {
} }
oldApps, err := appRepo.GetBy(appRepo.WithResource(constant.AppResourceRemote)) oldApps, err := appRepo.GetBy(appRepo.WithResource(constant.AppResourceRemote))
if err != nil { if err != nil {
return err return
} }
for _, old := range oldApps { for _, old := range oldApps {
oldAppIds = append(oldAppIds, old.ID) oldAppIds = append(oldAppIds, old.ID)
@ -810,32 +820,32 @@ func (a AppService) SyncAppListFromRemote() error {
tx, ctx := getTxAndContext() tx, ctx := getTxAndContext()
defer tx.Rollback() defer tx.Rollback()
if len(addAppArray) > 0 { if len(addAppArray) > 0 {
if err := appRepo.BatchCreate(ctx, addAppArray); err != nil { if err = appRepo.BatchCreate(ctx, addAppArray); err != nil {
return err return
} }
} }
if len(deleteAppArray) > 0 { if len(deleteAppArray) > 0 {
if err := appRepo.BatchDelete(ctx, deleteAppArray); err != nil { if err = appRepo.BatchDelete(ctx, deleteAppArray); err != nil {
return err return
} }
if err := appDetailRepo.DeleteByAppIds(ctx, deleteIds); err != nil { if err = appDetailRepo.DeleteByAppIds(ctx, deleteIds); err != nil {
return err return
} }
} }
if err := tagRepo.DeleteAll(ctx); err != nil { if err = tagRepo.DeleteAll(ctx); err != nil {
return err return
} }
if len(tags) > 0 { if len(tags) > 0 {
if err := tagRepo.BatchCreate(ctx, tags); err != nil { if err = tagRepo.BatchCreate(ctx, tags); err != nil {
return err return
} }
for _, t := range tags { for _, t := range tags {
tagMap[t.Key] = t.ID tagMap[t.Key] = t.ID
} }
} }
for _, update := range updateAppArray { for _, update := range updateAppArray {
if err := appRepo.Save(ctx, &update); err != nil { if err = appRepo.Save(ctx, &update); err != nil {
return err return
} }
} }
apps := append(addAppArray, updateAppArray...) apps := append(addAppArray, updateAppArray...)
@ -879,35 +889,32 @@ func (a AppService) SyncAppListFromRemote() error {
} }
} }
if len(addDetails) > 0 { if len(addDetails) > 0 {
if err := appDetailRepo.BatchCreate(ctx, addDetails); err != nil { if err = appDetailRepo.BatchCreate(ctx, addDetails); err != nil {
return err return
} }
} }
if len(deleteDetails) > 0 { if len(deleteDetails) > 0 {
if err := appDetailRepo.BatchDelete(ctx, deleteDetails); err != nil { if err = appDetailRepo.BatchDelete(ctx, deleteDetails); err != nil {
return err return
} }
} }
for _, u := range updateDetails { for _, u := range updateDetails {
if err := appDetailRepo.Update(ctx, u); err != nil { if err = appDetailRepo.Update(ctx, u); err != nil {
return err return
} }
} }
if len(oldAppIds) > 0 { if len(oldAppIds) > 0 {
if err := appTagRepo.DeleteByAppIds(ctx, oldAppIds); err != nil { if err = appTagRepo.DeleteByAppIds(ctx, oldAppIds); err != nil {
return err return
} }
} }
if len(appTags) > 0 { if len(appTags) > 0 {
if err := appTagRepo.BatchCreate(ctx, appTags); err != nil { if err = appTagRepo.BatchCreate(ctx, appTags); err != nil {
return err return
} }
} }
tx.Commit() tx.Commit()
if err := NewISettingService().Update("AppStoreLastModified", strconv.Itoa(list.LastModified)); err != nil { return
return err
}
return nil
} }