hook.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package op
  2. import (
  3. "regexp"
  4. "strings"
  5. "github.com/IceWhaleTech/CasaOS-Common/utils/logger"
  6. "github.com/IceWhaleTech/CasaOS/internal/conf"
  7. "github.com/IceWhaleTech/CasaOS/internal/driver"
  8. "github.com/IceWhaleTech/CasaOS/model"
  9. jsoniter "github.com/json-iterator/go"
  10. "github.com/pkg/errors"
  11. "go.uber.org/zap"
  12. )
  13. // Obj
  14. type ObjsUpdateHook = func(parent string, objs []model.Obj)
  15. var (
  16. ObjsUpdateHooks = make([]ObjsUpdateHook, 0)
  17. )
  18. func RegisterObjsUpdateHook(hook ObjsUpdateHook) {
  19. ObjsUpdateHooks = append(ObjsUpdateHooks, hook)
  20. }
  21. func HandleObjsUpdateHook(parent string, objs []model.Obj) {
  22. for _, hook := range ObjsUpdateHooks {
  23. hook(parent, objs)
  24. }
  25. }
  26. // Setting
  27. type SettingItemHook func(item *model.SettingItem) error
  28. var settingItemHooks = map[string]SettingItemHook{
  29. conf.VideoTypes: func(item *model.SettingItem) error {
  30. conf.SlicesMap[conf.VideoTypes] = strings.Split(item.Value, ",")
  31. return nil
  32. },
  33. conf.AudioTypes: func(item *model.SettingItem) error {
  34. conf.SlicesMap[conf.AudioTypes] = strings.Split(item.Value, ",")
  35. return nil
  36. },
  37. conf.ImageTypes: func(item *model.SettingItem) error {
  38. conf.SlicesMap[conf.ImageTypes] = strings.Split(item.Value, ",")
  39. return nil
  40. },
  41. conf.TextTypes: func(item *model.SettingItem) error {
  42. conf.SlicesMap[conf.TextTypes] = strings.Split(item.Value, ",")
  43. return nil
  44. },
  45. conf.ProxyTypes: func(item *model.SettingItem) error {
  46. conf.SlicesMap[conf.ProxyTypes] = strings.Split(item.Value, ",")
  47. return nil
  48. },
  49. conf.ProxyIgnoreHeaders: func(item *model.SettingItem) error {
  50. conf.SlicesMap[conf.ProxyIgnoreHeaders] = strings.Split(item.Value, ",")
  51. return nil
  52. },
  53. conf.PrivacyRegs: func(item *model.SettingItem) error {
  54. regStrs := strings.Split(item.Value, "\n")
  55. regs := make([]*regexp.Regexp, 0, len(regStrs))
  56. for _, regStr := range regStrs {
  57. reg, err := regexp.Compile(regStr)
  58. if err != nil {
  59. return errors.WithStack(err)
  60. }
  61. regs = append(regs, reg)
  62. }
  63. conf.PrivacyReg = regs
  64. return nil
  65. },
  66. conf.FilenameCharMapping: func(item *model.SettingItem) error {
  67. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  68. err := json.UnmarshalFromString(item.Value, &conf.FilenameCharMap)
  69. if err != nil {
  70. return err
  71. }
  72. logger.Info("filename char mapping", zap.Any("FilenameCharMap", conf.FilenameCharMap))
  73. return nil
  74. },
  75. }
  76. func RegisterSettingItemHook(key string, hook SettingItemHook) {
  77. settingItemHooks[key] = hook
  78. }
  79. func HandleSettingItemHook(item *model.SettingItem) (hasHook bool, err error) {
  80. if hook, ok := settingItemHooks[item.Key]; ok {
  81. return true, hook(item)
  82. }
  83. return false, nil
  84. }
  85. // Storage
  86. type StorageHook func(typ string, storage driver.Driver)
  87. var storageHooks = make([]StorageHook, 0)
  88. func CallStorageHooks(typ string, storage driver.Driver) {
  89. for _, hook := range storageHooks {
  90. hook(typ, storage)
  91. }
  92. }
  93. func RegisterStorageHook(hook StorageHook) {
  94. storageHooks = append(storageHooks, hook)
  95. }