Add copy/rename trigger and more environment variables

This commit is contained in:
Maxime Daniel 2017-09-07 13:56:57 +02:00
parent ec190d28a8
commit 0a5f39311c
2 changed files with 25 additions and 6 deletions

View File

@ -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

View File

@ -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