From 2a17d0c2cd09a05c5066e7a7676acddaf8625c0a Mon Sep 17 00:00:00 2001 From: Andy Hsu Date: Tue, 5 Mar 2024 16:29:26 +0800 Subject: [PATCH] fix: settings reset to default after restart if set to empty (close #6143) --- internal/bootstrap/data/setting.go | 7 +++++-- internal/model/setting.go | 16 ++++++++-------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/internal/bootstrap/data/setting.go b/internal/bootstrap/data/setting.go index b12973d7a2f..f85903c09dd 100644 --- a/internal/bootstrap/data/setting.go +++ b/internal/bootstrap/data/setting.go @@ -34,6 +34,9 @@ func initSettings() { // create or save setting for i := range initialSettingItems { item := &initialSettingItems[i] + if item.PreDefault == "" { + item.PreDefault = item.Value + } // err stored, err := op.GetSettingItemByKey(item.Key) if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { @@ -41,7 +44,7 @@ func initSettings() { continue } // save - if stored != nil && item.Key != conf.VERSION && stored.Value != item.DeprecatedValue { + if stored != nil && item.Key != conf.VERSION && stored.Value != item.PreDefault { item.Value = stored.Value } if stored == nil || *item != *stored { @@ -128,7 +131,7 @@ func InitialSettings() []model.SettingItem { // global settings {Key: conf.HideFiles, Value: "/\\/README.md/i", Type: conf.TypeText, Group: model.GLOBAL}, {Key: "package_download", Value: "true", Type: conf.TypeBool, Group: model.GLOBAL}, - {Key: conf.CustomizeHead, DeprecatedValue: ``, Type: conf.TypeText, Group: model.GLOBAL, Flag: model.PRIVATE}, + {Key: conf.CustomizeHead, PreDefault: ``, Type: conf.TypeText, Group: model.GLOBAL, Flag: model.PRIVATE}, {Key: conf.CustomizeBody, Type: conf.TypeText, Group: model.GLOBAL, Flag: model.PRIVATE}, {Key: conf.LinkExpiration, Value: "0", Type: conf.TypeNumber, Group: model.GLOBAL, Flag: model.PRIVATE}, {Key: conf.SignAll, Value: "true", Type: conf.TypeBool, Group: model.GLOBAL, Flag: model.PRIVATE}, diff --git a/internal/model/setting.go b/internal/model/setting.go index 9ef6fdf7d78..9893124890e 100644 --- a/internal/model/setting.go +++ b/internal/model/setting.go @@ -21,14 +21,14 @@ const ( ) type SettingItem struct { - Key string `json:"key" gorm:"primaryKey" binding:"required"` // unique key - Value string `json:"value"` // value - DeprecatedValue string `json:"deprecated_value" gorm:"-:all"` // deprecated value - Help string `json:"help"` // help message - Type string `json:"type"` // string, number, bool, select - Options string `json:"options"` // values for select - Group int `json:"group"` // use to group setting in frontend - Flag int `json:"flag"` // 0 = public, 1 = private, 2 = readonly, 3 = deprecated, etc. + Key string `json:"key" gorm:"primaryKey" binding:"required"` // unique key + Value string `json:"value"` // value + PreDefault string `json:"-" gorm:"-:all"` // deprecated value + Help string `json:"help"` // help message + Type string `json:"type"` // string, number, bool, select + Options string `json:"options"` // values for select + Group int `json:"group"` // use to group setting in frontend + Flag int `json:"flag"` // 0 = public, 1 = private, 2 = readonly, 3 = deprecated, etc. } func (s SettingItem) IsDeprecated() bool {