123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- package op
- import (
- "regexp"
- "strings"
- "github.com/IceWhaleTech/CasaOS-Common/utils/logger"
- "github.com/IceWhaleTech/CasaOS/internal/conf"
- "github.com/IceWhaleTech/CasaOS/internal/driver"
- "github.com/IceWhaleTech/CasaOS/model"
- jsoniter "github.com/json-iterator/go"
- "github.com/pkg/errors"
- "go.uber.org/zap"
- )
- // Obj
- type ObjsUpdateHook = func(parent string, objs []model.Obj)
- var (
- ObjsUpdateHooks = make([]ObjsUpdateHook, 0)
- )
- func RegisterObjsUpdateHook(hook ObjsUpdateHook) {
- ObjsUpdateHooks = append(ObjsUpdateHooks, hook)
- }
- func HandleObjsUpdateHook(parent string, objs []model.Obj) {
- for _, hook := range ObjsUpdateHooks {
- hook(parent, objs)
- }
- }
- // Setting
- type SettingItemHook func(item *model.SettingItem) error
- var settingItemHooks = map[string]SettingItemHook{
- conf.VideoTypes: func(item *model.SettingItem) error {
- conf.SlicesMap[conf.VideoTypes] = strings.Split(item.Value, ",")
- return nil
- },
- conf.AudioTypes: func(item *model.SettingItem) error {
- conf.SlicesMap[conf.AudioTypes] = strings.Split(item.Value, ",")
- return nil
- },
- conf.ImageTypes: func(item *model.SettingItem) error {
- conf.SlicesMap[conf.ImageTypes] = strings.Split(item.Value, ",")
- return nil
- },
- conf.TextTypes: func(item *model.SettingItem) error {
- conf.SlicesMap[conf.TextTypes] = strings.Split(item.Value, ",")
- return nil
- },
- conf.ProxyTypes: func(item *model.SettingItem) error {
- conf.SlicesMap[conf.ProxyTypes] = strings.Split(item.Value, ",")
- return nil
- },
- conf.ProxyIgnoreHeaders: func(item *model.SettingItem) error {
- conf.SlicesMap[conf.ProxyIgnoreHeaders] = strings.Split(item.Value, ",")
- return nil
- },
- 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)
- if err != nil {
- return errors.WithStack(err)
- }
- regs = append(regs, reg)
- }
- conf.PrivacyReg = regs
- return nil
- },
- conf.FilenameCharMapping: func(item *model.SettingItem) error {
- var json = jsoniter.ConfigCompatibleWithStandardLibrary
- err := json.UnmarshalFromString(item.Value, &conf.FilenameCharMap)
- if err != nil {
- return err
- }
- logger.Info("filename char mapping", zap.Any("FilenameCharMap", conf.FilenameCharMap))
- return nil
- },
- }
- func RegisterSettingItemHook(key string, hook SettingItemHook) {
- settingItemHooks[key] = hook
- }
- func HandleSettingItemHook(item *model.SettingItem) (hasHook bool, err error) {
- if hook, ok := settingItemHooks[item.Key]; ok {
- return true, hook(item)
- }
- return false, nil
- }
- // Storage
- type StorageHook func(typ string, storage driver.Driver)
- var storageHooks = make([]StorageHook, 0)
- func CallStorageHooks(typ string, storage driver.Driver) {
- for _, hook := range storageHooks {
- hook(typ, storage)
- }
- }
- func RegisterStorageHook(hook StorageHook) {
- storageHooks = append(storageHooks, hook)
- }
|