Skip to content

Commit

Permalink
Add pre-create hook for validation before upload creation
Browse files Browse the repository at this point in the history
  • Loading branch information
Acconut committed Jul 11, 2016
1 parent bad8173 commit 3530ffd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
8 changes: 8 additions & 0 deletions .hooks/pre-create
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

filename=$(cat /dev/stdin | jq .MetaData.filename)

if [ -n "$filename" ]; then
echo "Error: no filename provided"
exit 1
fi
47 changes: 37 additions & 10 deletions cmd/tusd/cli/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
"strconv"
Expand All @@ -15,9 +16,27 @@ type HookType string
const (
HookPostFinish HookType = "post-finish"
HookPostTerminate HookType = "post-terminate"
HookPreCreate HookType = "pre-create"
)

func SetupHooks(handler *tusd.Handler) {
type hookDataStore struct {
tusd.DataStore
}

func (store hookDataStore) NewUpload(info tusd.FileInfo) (id string, err error) {
if output, err := invokeHookSync(HookPreCreate, info, true); err != nil {
return "", fmt.Errorf("pre-create hook failed: %s\n%s", err, string(output))
}
return store.DataStore.NewUpload(info)
}

func SetupPreHooks(composer *tusd.StoreComposer) {
composer.UseCore(hookDataStore{
DataStore: composer.Core,
})
}

func SetupPostHooks(handler *tusd.Handler) {
go func() {
for {
select {
Expand All @@ -31,6 +50,15 @@ func SetupHooks(handler *tusd.Handler) {
}

func invokeHook(typ HookType, info tusd.FileInfo) {
go func() {
_, err := invokeHookSync(typ, info, false)
if err != nil {
stderr.Printf("Error running %s hook for %s: %s", string(typ), info.ID, err)
}
}()
}

func invokeHookSync(typ HookType, info tusd.FileInfo, captureOutput bool) ([]byte, error) {
switch typ {
case HookPostFinish:
stdout.Printf("Upload %s (%d bytes) finished\n", info.ID, info.Size)
Expand All @@ -39,7 +67,7 @@ func invokeHook(typ HookType, info tusd.FileInfo) {
}

if !Flags.HooksInstalled {
return
return nil, nil
}

name := string(typ)
Expand All @@ -52,21 +80,20 @@ func invokeHook(typ HookType, info tusd.FileInfo) {

jsonInfo, err := json.Marshal(info)
if err != nil {
stderr.Printf("Error encoding JSON for hook: %s", err)
return nil, err
}

reader := bytes.NewReader(jsonInfo)
cmd.Stdin = reader

cmd.Env = env
cmd.Dir = Flags.HooksDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

go func() {
err := cmd.Run()
if err != nil {
stderr.Printf("Error running %s hook for %s: %s", name, info.ID, err)
}
}()
if !captureOutput {
cmd.Stdout = os.Stdout
return nil, cmd.Run()
} else {
return cmd.Output()
}
}
4 changes: 3 additions & 1 deletion cmd/tusd/cli/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
)

func Serve() {
SetupPreHooks(Composer)

handler, err := tusd.NewHandler(tusd.Config{
MaxSize: Flags.MaxSize,
BasePath: Flags.Basepath,
Expand All @@ -27,7 +29,7 @@ func Serve() {
stdout.Printf("Using %s as the base path.\n", basepath)
stdout.Printf(Composer.Capabilities())

SetupHooks(handler)
SetupPostHooks(handler)

if Flags.ExposeMetrics {
SetupMetrics(handler)
Expand Down

0 comments on commit 3530ffd

Please sign in to comment.