Pass info object as JSON blob to hook

This commit is contained in:
Marius 2016-02-22 13:59:28 +01:00
parent a949cb6b81
commit 0070379a91
2 changed files with 10 additions and 6 deletions

View File

@ -1,4 +1,4 @@
#!/bin/bash #!/bin/bash
echo "Upload finished" echo "Upload $TUS_ID ($TUS_SIZE bytes) finished"
printenv cat /dev/stdin | jq .

View File

@ -1,6 +1,8 @@
package main package main
import ( import (
"bytes"
"encoding/json"
"flag" "flag"
"fmt" "fmt"
"log" "log"
@ -10,7 +12,6 @@ import (
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/tus/tusd" "github.com/tus/tusd"
@ -151,11 +152,14 @@ func invokeHook(info tusd.FileInfo) {
env = append(env, "TUS_ID="+info.ID) env = append(env, "TUS_ID="+info.ID)
env = append(env, "TUS_SIZE="+strconv.FormatInt(info.Size, 10)) env = append(env, "TUS_SIZE="+strconv.FormatInt(info.Size, 10))
for k, v := range info.MetaData { jsonInfo, err := json.Marshal(info)
k = strings.ToUpper(k) if err != nil {
env = append(env, "TUS_METADATA_"+k+"="+v) stderr.Printf("Error encoding JSON for hook: %s", err)
} }
reader := bytes.NewReader(jsonInfo)
cmd.Stdin = reader
cmd.Env = env cmd.Env = env
cmd.Dir = hooksDir cmd.Dir = hooksDir
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout