Merge branch 'master' into composer

This commit is contained in:
Marius 2016-02-22 14:12:24 +01:00
commit 03ab994a4e
4 changed files with 58 additions and 3 deletions

4
.hooks/post-finish Executable file
View File

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

View File

@ -17,7 +17,7 @@ matrix:
- go: tip - go: tip
install: install:
- export PACKAGES=$(find ./ -maxdepth 1 -type d -not \( -name ".git" -or -name "cmd" -or -name "vendor" -or -name "data" \)) - export PACKAGES=$(find ./ -maxdepth 1 -type d -not \( -name ".git" -or -name "cmd" -or -name "vendor" -or -name "data" -or -name ".hooks" \))
- rsync -r ./vendor/ $GOPATH/src - rsync -r ./vendor/ $GOPATH/src
script: script:

View File

@ -1,12 +1,17 @@
package main package main
import ( import (
"bytes"
"encoding/json"
"flag" "flag"
"fmt" "fmt"
"log" "log"
"net" "net"
"net/http" "net/http"
"os" "os"
"os/exec"
"path/filepath"
"strconv"
"time" "time"
"github.com/tus/tusd" "github.com/tus/tusd"
@ -33,11 +38,14 @@ var storeSize int64
var basepath string var basepath string
var timeout int64 var timeout int64
var s3Bucket string var s3Bucket string
var hooksDir string
var version bool var version bool
var stdout = log.New(os.Stdout, "[tusd] ", 0) var stdout = log.New(os.Stdout, "[tusd] ", 0)
var stderr = log.New(os.Stderr, "[tusd] ", 0) var stderr = log.New(os.Stderr, "[tusd] ", 0)
var hookInstalled bool
func init() { func init() {
flag.StringVar(&httpHost, "host", "0.0.0.0", "Host to bind HTTP server to") flag.StringVar(&httpHost, "host", "0.0.0.0", "Host to bind HTTP server to")
flag.StringVar(&httpPort, "port", "1080", "Port to bind HTTP server to") flag.StringVar(&httpPort, "port", "1080", "Port to bind HTTP server to")
@ -47,9 +55,17 @@ func init() {
flag.StringVar(&basepath, "base-path", "/files/", "Basepath of the HTTP server") flag.StringVar(&basepath, "base-path", "/files/", "Basepath of the HTTP server")
flag.Int64Var(&timeout, "timeout", 30*1000, "Read timeout for connections in milliseconds") flag.Int64Var(&timeout, "timeout", 30*1000, "Read timeout for connections in milliseconds")
flag.StringVar(&s3Bucket, "s3-bucket", "", "Use AWS S3 with this bucket as storage backend (requires the AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_REGION environment variables to be set)") flag.StringVar(&s3Bucket, "s3-bucket", "", "Use AWS S3 with this bucket as storage backend (requires the AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_REGION environment variables to be set)")
flag.StringVar(&hooksDir, "hooks-dir", "", "")
flag.BoolVar(&version, "version", false, "Print tusd version information") flag.BoolVar(&version, "version", false, "Print tusd version information")
flag.Parse() flag.Parse()
if hooksDir != "" {
hooksDir, _ = filepath.Abs(hooksDir)
hookInstalled = true
stdout.Printf("Using '%s' for hooks", hooksDir)
}
} }
func main() { func main() {
@ -112,7 +128,7 @@ func main() {
for { for {
select { select {
case info := <-handler.CompleteUploads: case info := <-handler.CompleteUploads:
stdout.Printf("Upload %s (%d bytes) finished\n", info.ID, info.Size) invokeHook(info)
} }
} }
}() }()
@ -130,6 +146,41 @@ func main() {
} }
} }
func invokeHook(info tusd.FileInfo) {
stdout.Printf("Upload %s (%d bytes) finished\n", info.ID, info.Size)
if !hookInstalled {
return
}
stdout.Println("Invoking hooks…")
cmd := exec.Command(hooksDir + "/post-finish")
env := os.Environ()
env = append(env, "TUS_ID="+info.ID)
env = append(env, "TUS_SIZE="+strconv.FormatInt(info.Size, 10))
jsonInfo, err := json.Marshal(info)
if err != nil {
stderr.Printf("Error encoding JSON for hook: %s", err)
}
reader := bytes.NewReader(jsonInfo)
cmd.Stdin = reader
cmd.Env = env
cmd.Dir = hooksDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
go func() {
err := cmd.Run()
if err != nil {
stderr.Printf("Error running postfinish hook for %s: %s", info.ID, err)
}
}()
}
// Listener wraps a net.Listener, and gives a place to store the timeout // Listener wraps a net.Listener, and gives a place to store the timeout
// parameters. On Accept, it will wrap the net.Conn with our own Conn for us. // parameters. On Accept, it will wrap the net.Conn with our own Conn for us.
// Original implementation taken from https://gist.github.com/jbardin/9663312 // Original implementation taken from https://gist.github.com/jbardin/9663312

View File

@ -376,7 +376,7 @@ func (handler *UnroutedHandler) PatchFile(w http.ResponseWriter, r *http.Request
// ... send the info out to the channel // ... send the info out to the channel
if handler.config.NotifyCompleteUploads { if handler.config.NotifyCompleteUploads {
info.Size = newOffset info.Offset = newOffset
handler.CompleteUploads <- info handler.CompleteUploads <- info
} }
} }