feat: custom hide error message by regexp (close #1468)
This commit is contained in:
84
internal/db/settinghooks.go
Normal file
84
internal/db/settinghooks.go
Normal file
@ -0,0 +1,84 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/alist-org/alist/v3/internal/conf"
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type SettingItemHook struct {
|
||||
Hook func(item *model.SettingItem) error
|
||||
}
|
||||
|
||||
var SettingItemHooks = map[string]SettingItemHook{
|
||||
conf.VideoTypes: {
|
||||
Hook: func(item *model.SettingItem) error {
|
||||
conf.TypesMap[conf.VideoTypes] = strings.Split(item.Value, ",")
|
||||
return nil
|
||||
},
|
||||
},
|
||||
conf.AudioTypes: {
|
||||
Hook: func(item *model.SettingItem) error {
|
||||
conf.TypesMap[conf.AudioTypes] = strings.Split(item.Value, ",")
|
||||
return nil
|
||||
},
|
||||
},
|
||||
conf.ImageTypes: {
|
||||
Hook: func(item *model.SettingItem) error {
|
||||
conf.TypesMap[conf.ImageTypes] = strings.Split(item.Value, ",")
|
||||
return nil
|
||||
},
|
||||
},
|
||||
conf.TextTypes: {
|
||||
Hook: func(item *model.SettingItem) error {
|
||||
conf.TypesMap[conf.TextTypes] = strings.Split(item.Value, ",")
|
||||
return nil
|
||||
},
|
||||
},
|
||||
conf.OfficeTypes: {
|
||||
Hook: func(item *model.SettingItem) error {
|
||||
conf.TypesMap[conf.OfficeTypes] = strings.Split(item.Value, ",")
|
||||
return nil
|
||||
},
|
||||
},
|
||||
conf.ProxyTypes: {
|
||||
func(item *model.SettingItem) error {
|
||||
conf.TypesMap[conf.ProxyTypes] = strings.Split(item.Value, ",")
|
||||
return nil
|
||||
},
|
||||
},
|
||||
conf.PrivacyRegs: {
|
||||
Hook: func(item *model.SettingItem) error {
|
||||
regStrs := strings.Split(item.Value, "\n")
|
||||
regs := make([]*regexp.Regexp, 0, len(regStrs))
|
||||
for _, regStr := range regStrs {
|
||||
reg, err := regexp.Compile(regStr)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
regs = append(regs, reg)
|
||||
}
|
||||
conf.PrivacyReg = regs
|
||||
return nil
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func HandleSettingItem(item *model.SettingItem) (bool, error) {
|
||||
if hook, ok := SettingItemHooks[item.Key]; ok {
|
||||
return true, hook.Hook(item)
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// func HandleSettingItems(items []model.SettingItem) error {
|
||||
// for i := range items {
|
||||
// if err := HandleSettingItem(&items[i]); err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
// return nil
|
||||
// }
|
@ -2,9 +2,7 @@ package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/alist-org/alist/v3/internal/conf"
|
||||
"github.com/alist-org/alist/v3/internal/model"
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
@ -13,19 +11,9 @@ import (
|
||||
var settingsMap map[string]string
|
||||
var publicSettingsMap map[string]string
|
||||
|
||||
func ResetTypeMap() {
|
||||
settingsMap := GetSettingsMap()
|
||||
conf.TypesMap[conf.AudioTypes] = strings.Split(settingsMap[conf.AudioTypes], ",")
|
||||
conf.TypesMap[conf.VideoTypes] = strings.Split(settingsMap[conf.VideoTypes], ",")
|
||||
conf.TypesMap[conf.ImageTypes] = strings.Split(settingsMap[conf.ImageTypes], ",")
|
||||
conf.TypesMap[conf.TextTypes] = strings.Split(settingsMap[conf.TextTypes], ",")
|
||||
conf.TypesMap[conf.OfficeTypes] = strings.Split(settingsMap[conf.OfficeTypes], ",")
|
||||
}
|
||||
|
||||
func settingsUpdate() {
|
||||
settingsMap = nil
|
||||
publicSettingsMap = nil
|
||||
ResetTypeMap()
|
||||
}
|
||||
|
||||
func GetPublicSettingsMap() map[string]string {
|
||||
@ -105,13 +93,38 @@ func GetSettingItemsInGroups(groups []int) ([]model.SettingItem, error) {
|
||||
}
|
||||
|
||||
func SaveSettingItems(items []model.SettingItem) error {
|
||||
settingsUpdate()
|
||||
return errors.WithStack(db.Save(items).Error)
|
||||
others := make([]model.SettingItem, 0)
|
||||
for i := range items {
|
||||
if ok, err := HandleSettingItem(&items[i]); ok {
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
err = db.Save(items[i]).Error
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
others = append(others, items[i])
|
||||
}
|
||||
}
|
||||
err := db.Save(others).Error
|
||||
if err == nil {
|
||||
settingsUpdate()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func SaveSettingItem(item model.SettingItem) error {
|
||||
settingsUpdate()
|
||||
return errors.WithStack(db.Save(item).Error)
|
||||
_, err := HandleSettingItem(&item)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = db.Save(item).Error
|
||||
if err == nil {
|
||||
settingsUpdate()
|
||||
}
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
func DeleteSettingItemByKey(key string) error {
|
||||
|
Reference in New Issue
Block a user