69 lines
1.6 KiB
Go
69 lines
1.6 KiB
Go
package torrent
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"os/exec"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"github.com/debridmediamanager/zurg/internal/config"
|
|
"github.com/debridmediamanager/zurg/pkg/logutil"
|
|
)
|
|
|
|
type ScriptExecutor struct {
|
|
Script string
|
|
Args []string
|
|
}
|
|
|
|
func (se *ScriptExecutor) Execute() (string, error) {
|
|
if se.Script == "" {
|
|
return "", nil
|
|
}
|
|
|
|
var cmd *exec.Cmd
|
|
if runtime.GOOS == "windows" {
|
|
// For Windows, using PowerShell
|
|
cmd = exec.Command("powershell", "-Command", se.Script)
|
|
} else {
|
|
// For Unix-like systems
|
|
cmd = exec.Command("/bin/sh", "-c", se.Script)
|
|
}
|
|
cmd.Args = append(cmd.Args, "zurg")
|
|
for _, arg := range se.Args {
|
|
// replace space with escaped space
|
|
cmd.Args = append(cmd.Args, strings.ReplaceAll(arg, " ", "\\ "))
|
|
}
|
|
var out bytes.Buffer
|
|
var stderr bytes.Buffer
|
|
cmd.Stdout = &out
|
|
cmd.Stderr = &stderr
|
|
err := cmd.Run()
|
|
if err != nil {
|
|
return "", fmt.Errorf("error executing script: %v; stderr: %s", err, stderr.String())
|
|
}
|
|
return out.String(), nil
|
|
}
|
|
|
|
func (t *TorrentManager) TriggerHookOnLibraryUpdate(updatedPaths []string) {
|
|
_ = t.workerPool.Submit(func() {
|
|
OnLibraryUpdateHook(updatedPaths, t.Config, t.log)
|
|
t.log.Debugf("Triggered hook on_library_update for %d path(s)", len(updatedPaths))
|
|
})
|
|
}
|
|
|
|
func OnLibraryUpdateHook(paths []string, config config.ConfigInterface, log *logutil.Logger) {
|
|
executor := &ScriptExecutor{
|
|
Script: config.GetOnLibraryUpdate(),
|
|
Args: paths,
|
|
}
|
|
output, err := executor.Execute()
|
|
if err != nil {
|
|
log.Errorf("Failed to execute hook on_library_update: %v", err)
|
|
return
|
|
}
|
|
if output != "" {
|
|
log.Debugf("Output of hook on_library_update:\n%s", output)
|
|
}
|
|
}
|