From 0a5f39311cdd861e0b01a7e284f4fbe8ff81f3f5 Mon Sep 17 00:00:00 2001 From: Maxime Daniel Date: Thu, 7 Sep 2017 13:56:57 +0200 Subject: [PATCH] Add copy/rename trigger and more environment variables --- filemanager.go | 15 +++++++++++++-- resource.go | 16 ++++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/filemanager.go b/filemanager.go index 6878dfb5..ad964cfd 100644 --- a/filemanager.go +++ b/filemanager.go @@ -55,6 +55,7 @@ package filemanager import ( "errors" + "fmt" "log" "net/http" "os" @@ -249,6 +250,8 @@ func New(database string, base User) (*FileManager, error) { "after_save": {}, "before_publish": {}, "after_publish": {}, + "after_copy": {}, + "after_rename": {}, } err = db.Set("config", "commands", m.Commands) } @@ -471,7 +474,7 @@ func (r *Regexp) MatchString(s string) bool { } // Runner runs the commands for a certain event type. -func (m FileManager) Runner(event string, path string) error { +func (m FileManager) Runner(event string, path string, destination string, user *User) error { commands := []string{} // Get the commands from the File Manager instance itself. @@ -496,7 +499,15 @@ func (m FileManager) Runner(event string, path string) error { } cmd := exec.Command(command, args...) - cmd.Env = append(os.Environ(), "file="+path) + cmd.Env = append(os.Environ(), fmt.Sprintf("FILE=%s", path)) + cmd.Env = append(cmd.Env, fmt.Sprintf("ROOT=%s", user.FileSystem)) + cmd.Env = append(cmd.Env, fmt.Sprintf("TRIGGER=%s", event)) + cmd.Env = append(cmd.Env, fmt.Sprintf("USERNAME=%s", user.Username)) + + if destination != "" { + cmd.Env = append(cmd.Env, fmt.Sprintf("DESTINATION=%s", destination)) + } + cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr diff --git a/resource.go b/resource.go index 4aef9b78..bc4bb7d6 100644 --- a/resource.go +++ b/resource.go @@ -37,7 +37,7 @@ func resourceHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) case http.MethodPut: // Before save command handler. path := filepath.Join(string(c.User.FileSystem), r.URL.Path) - if err := c.Runner("before_save", path); err != nil { + if err := c.Runner("before_save", path, "", c.User); err != nil { return http.StatusInternalServerError, err } @@ -47,7 +47,7 @@ func resourceHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) } // After save command handler. - if err := c.Runner("after_save", path); err != nil { + if err := c.Runner("after_save", path, "", c.User); err != nil { return http.StatusInternalServerError, err } @@ -255,7 +255,7 @@ func resourcePublish(c *RequestContext, w http.ResponseWriter, r *http.Request) path := filepath.Join(string(c.User.FileSystem), r.URL.Path) // Before save command handler. - if err := c.Runner("before_publish", path); err != nil { + if err := c.Runner("before_publish", path, "", c.User); err != nil { return http.StatusInternalServerError, err } @@ -265,7 +265,7 @@ func resourcePublish(c *RequestContext, w http.ResponseWriter, r *http.Request) } // Executed the before publish command. - if err := c.Runner("before_publish", path); err != nil { + if err := c.Runner("before_publish", path, "", c.User); err != nil { return http.StatusInternalServerError, err } @@ -293,8 +293,16 @@ func resourcePatchHandler(c *RequestContext, w http.ResponseWriter, r *http.Requ if action == "copy" { err = c.User.FileSystem.Copy(src, dst) + + if err := c.Runner("after_copy", src, dst, c.User); err != nil { + return http.StatusInternalServerError, err + } } else { err = c.User.FileSystem.Rename(src, dst) + + if err := c.Runner("after_rename", src, dst, c.User); err != nil { + return http.StatusInternalServerError, err + } } return errorToHTTP(err, true), err