2019-01-06 06:44:33 +08:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/filebrowser/filebrowser/v2/settings"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/users"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Runner is a commands runner.
|
|
|
|
type Runner struct {
|
2020-10-01 22:45:24 +08:00
|
|
|
Enabled bool
|
2019-01-06 06:44:33 +08:00
|
|
|
*settings.Settings
|
|
|
|
}
|
|
|
|
|
|
|
|
// RunHook runs the hooks for the before and after event.
|
|
|
|
func (r *Runner) RunHook(fn func() error, evt, path, dst string, user *users.User) error {
|
|
|
|
path = user.FullPath(path)
|
|
|
|
dst = user.FullPath(dst)
|
|
|
|
|
2020-10-01 22:45:24 +08:00
|
|
|
if r.Enabled {
|
|
|
|
if val, ok := r.Commands["before_"+evt]; ok {
|
|
|
|
for _, command := range val {
|
|
|
|
err := r.exec(command, "before_"+evt, path, dst, user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := fn()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-01 22:45:24 +08:00
|
|
|
if r.Enabled {
|
|
|
|
if val, ok := r.Commands["after_"+evt]; ok {
|
|
|
|
for _, command := range val {
|
|
|
|
err := r.exec(command, "after_"+evt, path, dst, user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runner) exec(raw, evt, path, dst string, user *users.User) error {
|
|
|
|
blocking := true
|
|
|
|
|
|
|
|
if strings.HasSuffix(raw, "&") {
|
|
|
|
blocking = false
|
|
|
|
raw = strings.TrimSpace(strings.TrimSuffix(raw, "&"))
|
|
|
|
}
|
|
|
|
|
|
|
|
command, err := ParseCommand(r.Settings, raw)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-01-20 00:02:46 +08:00
|
|
|
envMapping := func(key string) string {
|
|
|
|
switch key {
|
|
|
|
case "FILE":
|
|
|
|
return path
|
|
|
|
case "SCOPE":
|
|
|
|
return user.Scope
|
|
|
|
case "TRIGGER":
|
|
|
|
return evt
|
|
|
|
case "USERNAME":
|
|
|
|
return user.Username
|
|
|
|
case "DESTINATION":
|
|
|
|
return dst
|
|
|
|
default:
|
|
|
|
return os.Getenv(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for i, arg := range command {
|
|
|
|
if i == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
command[i] = os.Expand(arg, envMapping)
|
|
|
|
}
|
|
|
|
|
2020-06-01 07:12:36 +08:00
|
|
|
cmd := exec.Command(command[0], command[1:]...) //nolint:gosec
|
2019-01-06 06:44:33 +08:00
|
|
|
cmd.Env = append(os.Environ(), fmt.Sprintf("FILE=%s", path))
|
2020-06-01 07:12:36 +08:00
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("SCOPE=%s", user.Scope)) //nolint:gocritic
|
2019-01-06 06:44:33 +08:00
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("TRIGGER=%s", evt))
|
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("USERNAME=%s", user.Username))
|
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("DESTINATION=%s", dst))
|
|
|
|
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
if !blocking {
|
|
|
|
log.Printf("[INFO] Nonblocking Command: \"%s\"", strings.Join(command, " "))
|
2021-03-15 02:32:14 +08:00
|
|
|
defer func() {
|
|
|
|
go func() {
|
|
|
|
err := cmd.Wait()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("[INFO] Nonblocking Command \"%s\" failed: %s", strings.Join(command, " "), err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}()
|
2019-01-06 06:44:33 +08:00
|
|
|
return cmd.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[INFO] Blocking Command: \"%s\"", strings.Join(command, " "))
|
|
|
|
return cmd.Run()
|
|
|
|
}
|