2022-08-08 04:52:54 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/alist-org/alist/v3/internal/conf"
|
|
|
|
"github.com/alist-org/alist/v3/internal/model"
|
2022-11-22 07:54:18 +00:00
|
|
|
"github.com/alist-org/alist/v3/pkg/utils"
|
2022-08-08 04:52:54 +00:00
|
|
|
"github.com/pkg/errors"
|
2022-11-22 07:54:18 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-08-08 04:52:54 +00:00
|
|
|
)
|
|
|
|
|
2022-11-28 05:45:25 +00:00
|
|
|
type SettingItemHook func(item *model.SettingItem) error
|
2022-08-08 04:52:54 +00:00
|
|
|
|
2022-11-28 05:45:25 +00:00
|
|
|
var settingItemHooks = map[string]SettingItemHook{
|
|
|
|
conf.VideoTypes: func(item *model.SettingItem) error {
|
|
|
|
conf.TypesMap[conf.VideoTypes] = strings.Split(item.Value, ",")
|
|
|
|
return nil
|
2022-08-08 04:52:54 +00:00
|
|
|
},
|
2022-11-28 05:45:25 +00:00
|
|
|
conf.AudioTypes: func(item *model.SettingItem) error {
|
|
|
|
conf.TypesMap[conf.AudioTypes] = strings.Split(item.Value, ",")
|
|
|
|
return nil
|
2022-08-08 04:52:54 +00:00
|
|
|
},
|
2022-11-28 05:45:25 +00:00
|
|
|
conf.ImageTypes: func(item *model.SettingItem) error {
|
|
|
|
conf.TypesMap[conf.ImageTypes] = strings.Split(item.Value, ",")
|
|
|
|
return nil
|
2022-08-08 04:52:54 +00:00
|
|
|
},
|
2022-11-28 05:45:25 +00:00
|
|
|
conf.TextTypes: func(item *model.SettingItem) error {
|
|
|
|
conf.TypesMap[conf.TextTypes] = strings.Split(item.Value, ",")
|
|
|
|
return nil
|
2022-08-08 04:52:54 +00:00
|
|
|
},
|
2022-11-28 05:45:25 +00:00
|
|
|
conf.ProxyTypes: func(item *model.SettingItem) error {
|
|
|
|
conf.TypesMap[conf.ProxyTypes] = strings.Split(item.Value, ",")
|
|
|
|
return nil
|
2022-08-08 04:52:54 +00:00
|
|
|
},
|
2022-11-28 05:45:25 +00:00
|
|
|
|
|
|
|
conf.PrivacyRegs: 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)
|
2022-11-22 07:54:18 +00:00
|
|
|
if err != nil {
|
2022-11-28 05:45:25 +00:00
|
|
|
return errors.WithStack(err)
|
2022-11-22 07:54:18 +00:00
|
|
|
}
|
2022-11-28 05:45:25 +00:00
|
|
|
regs = append(regs, reg)
|
|
|
|
}
|
|
|
|
conf.PrivacyReg = regs
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
conf.FilenameCharMapping: func(item *model.SettingItem) error {
|
|
|
|
err := utils.Json.UnmarshalFromString(item.Value, &conf.FilenameCharMap)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Debugf("filename char mapping: %+v", conf.FilenameCharMap)
|
|
|
|
return nil
|
2022-11-22 07:54:18 +00:00
|
|
|
},
|
2022-08-08 04:52:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func HandleSettingItem(item *model.SettingItem) (bool, error) {
|
2022-11-28 05:45:25 +00:00
|
|
|
if hook, ok := settingItemHooks[item.Key]; ok {
|
|
|
|
return true, hook(item)
|
2022-08-08 04:52:54 +00:00
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2022-11-28 05:45:25 +00:00
|
|
|
func RegisterSettingItemHook(key string, hook SettingItemHook) {
|
|
|
|
settingItemHooks[key] = hook
|
|
|
|
}
|
|
|
|
|
2022-08-08 04:52:54 +00:00
|
|
|
// func HandleSettingItems(items []model.SettingItem) error {
|
|
|
|
// for i := range items {
|
|
|
|
// if err := HandleSettingItem(&items[i]); err != nil {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return nil
|
|
|
|
// }
|