package trigger import ( "strings" . "github.com/mickael-kerjean/filestash/server/common" ) var ( fileaction_event = make(chan ITriggerEvent, 1) fileaction_name = "event" ) func init() { Hooks.Register.WorkflowTrigger(&FileEventTrigger{}) Hooks.Register.AuthorisationMiddleware(hookAuthorisation{}) } type hookAuthorisation struct{} func (this hookAuthorisation) Ls(ctx *App, path string) error { processFileAction(ctx, map[string]string{"event": "ls", "path": path}) return nil } func (this hookAuthorisation) Cat(ctx *App, path string) error { processFileAction(ctx, map[string]string{"event": "cat", "path": path}) return nil } func (this hookAuthorisation) Mkdir(ctx *App, path string) error { processFileAction(ctx, map[string]string{"event": "mkdir", "path": path}) return nil } func (this hookAuthorisation) Rm(ctx *App, path string) error { processFileAction(ctx, map[string]string{"event": "rm", "path": path}) return nil } func (this hookAuthorisation) Mv(ctx *App, from string, to string) error { processFileAction(ctx, map[string]string{"event": "mv", "path": from + ", " + to}) return nil } func (this hookAuthorisation) Save(ctx *App, path string) error { processFileAction(ctx, map[string]string{"event": "save", "path": path}) return nil } func (this hookAuthorisation) Touch(ctx *App, path string) error { processFileAction(ctx, map[string]string{"event": "touch", "path": path}) return nil } type FileEventTrigger struct{} func (this *FileEventTrigger) Manifest() WorkflowSpecs { return WorkflowSpecs{ Name: fileaction_name, Title: "When Something Happen", Icon: ``, Specs: Form{ Elmnts: []FormElement{ { Name: "event", Type: "text", Datalist: []string{"ls", "cat", "mkdir", "mv", "rm", "touch"}, MultiValue: true, }, { Name: "path", Type: "text", }, }, }, Order: 3, } } func (this *FileEventTrigger) Init() (chan ITriggerEvent, error) { return fileaction_event, nil } func processFileAction(ctx *App, params map[string]string) { if ctx.Context.Value("AUDIT") == false { return } if err := TriggerEvents(fileaction_event, fileaction_name, fileactionCallback(params)); err != nil { Log.Error("[workflow] trigger=event step=triggerEvents err=%s", err.Error()) } } func fileactionCallback(out map[string]string) func(map[string]string) (map[string]string, bool) { return func(params map[string]string) (map[string]string, bool) { if !matchEvent(params["event"], out["event"]) { return out, false } else if !matchPath(params["path"], out["path"]) { return out, false } return out, true } } func matchEvent(paramValue string, eventValue string) bool { if paramValue == "" { return true } for _, pvalue := range strings.Split(paramValue, ",") { if strings.TrimSpace(pvalue) == eventValue { return true } } return false } func matchPath(paramValue string, eventValue string) bool { if paramValue == "" { return true } for _, epath := range strings.Split(eventValue, ",") { if GlobMatch(paramValue, strings.TrimSpace(epath)) { return true } } return false }