Skip to content

Commit

Permalink
fix: settings reset to default after restart if set to empty (close #…
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Mar 5, 2024
1 parent 6f6a8e6 commit 2a17d0c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
7 changes: 5 additions & 2 deletions internal/bootstrap/data/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ 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) {
utils.Log.Fatalf("failed get setting: %+v", err)
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 {
Expand Down Expand Up @@ -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: `<script src="https://polyfill.io/v3/polyfill.min.js?features=String.prototype.replaceAll"></script>`, Type: conf.TypeText, Group: model.GLOBAL, Flag: model.PRIVATE},
{Key: conf.CustomizeHead, PreDefault: `<script src="https://polyfill.io/v3/polyfill.min.js?features=String.prototype.replaceAll"></script>`, 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},
Expand Down
16 changes: 8 additions & 8 deletions internal/model/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 2a17d0c

Please sign in to comment.