Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/internal/op/hook.go
1560 views
1
package op
2
3
import (
4
"regexp"
5
"strconv"
6
"strings"
7
8
"github.com/alist-org/alist/v3/internal/conf"
9
"github.com/alist-org/alist/v3/internal/driver"
10
"github.com/alist-org/alist/v3/internal/model"
11
"github.com/alist-org/alist/v3/pkg/utils"
12
"github.com/pkg/errors"
13
log "github.com/sirupsen/logrus"
14
)
15
16
// Obj
17
type ObjsUpdateHook = func(parent string, objs []model.Obj)
18
19
var (
20
objsUpdateHooks = make([]ObjsUpdateHook, 0)
21
)
22
23
func RegisterObjsUpdateHook(hook ObjsUpdateHook) {
24
objsUpdateHooks = append(objsUpdateHooks, hook)
25
}
26
27
func HandleObjsUpdateHook(parent string, objs []model.Obj) {
28
for _, hook := range objsUpdateHooks {
29
hook(parent, objs)
30
}
31
}
32
33
// Setting
34
type SettingItemHook func(item *model.SettingItem) error
35
36
var settingItemHooks = map[string]SettingItemHook{
37
conf.VideoTypes: func(item *model.SettingItem) error {
38
conf.SlicesMap[conf.VideoTypes] = strings.Split(item.Value, ",")
39
return nil
40
},
41
conf.AudioTypes: func(item *model.SettingItem) error {
42
conf.SlicesMap[conf.AudioTypes] = strings.Split(item.Value, ",")
43
return nil
44
},
45
conf.ImageTypes: func(item *model.SettingItem) error {
46
conf.SlicesMap[conf.ImageTypes] = strings.Split(item.Value, ",")
47
return nil
48
},
49
conf.TextTypes: func(item *model.SettingItem) error {
50
conf.SlicesMap[conf.TextTypes] = strings.Split(item.Value, ",")
51
return nil
52
},
53
conf.ProxyTypes: func(item *model.SettingItem) error {
54
conf.SlicesMap[conf.ProxyTypes] = strings.Split(item.Value, ",")
55
return nil
56
},
57
conf.ProxyIgnoreHeaders: func(item *model.SettingItem) error {
58
conf.SlicesMap[conf.ProxyIgnoreHeaders] = strings.Split(item.Value, ",")
59
return nil
60
},
61
conf.PrivacyRegs: func(item *model.SettingItem) error {
62
regStrs := strings.Split(item.Value, "\n")
63
regs := make([]*regexp.Regexp, 0, len(regStrs))
64
for _, regStr := range regStrs {
65
reg, err := regexp.Compile(regStr)
66
if err != nil {
67
return errors.WithStack(err)
68
}
69
regs = append(regs, reg)
70
}
71
conf.PrivacyReg = regs
72
return nil
73
},
74
conf.FilenameCharMapping: func(item *model.SettingItem) error {
75
err := utils.Json.UnmarshalFromString(item.Value, &conf.FilenameCharMap)
76
if err != nil {
77
return err
78
}
79
log.Debugf("filename char mapping: %+v", conf.FilenameCharMap)
80
return nil
81
},
82
conf.IgnoreDirectLinkParams: func(item *model.SettingItem) error {
83
conf.SlicesMap[conf.IgnoreDirectLinkParams] = strings.Split(item.Value, ",")
84
return nil
85
},
86
conf.DefaultRole: func(item *model.SettingItem) error {
87
v := strings.TrimSpace(item.Value)
88
if v == "" {
89
return nil
90
}
91
id, err := strconv.Atoi(v)
92
if err != nil {
93
return errors.WithStack(err)
94
}
95
_, err = GetRole(uint(id))
96
return err
97
},
98
}
99
100
func RegisterSettingItemHook(key string, hook SettingItemHook) {
101
settingItemHooks[key] = hook
102
}
103
104
func HandleSettingItemHook(item *model.SettingItem) (hasHook bool, err error) {
105
if hook, ok := settingItemHooks[item.Key]; ok {
106
return true, hook(item)
107
}
108
return false, nil
109
}
110
111
// Storage
112
type StorageHook func(typ string, storage driver.Driver)
113
114
var storageHooks = make([]StorageHook, 0)
115
116
func callStorageHooks(typ string, storage driver.Driver) {
117
for _, hook := range storageHooks {
118
hook(typ, storage)
119
}
120
}
121
122
func RegisterStorageHook(hook StorageHook) {
123
storageHooks = append(storageHooks, hook)
124
}
125
126