From 447aa9c48546d4f160b8f01235b28fdd39796ed8 Mon Sep 17 00:00:00 2001 From: Markus Kienast Date: Thu, 13 Jul 2017 15:57:07 +0200 Subject: [PATCH] added post-create hook added test for handler.UploadCreated go fmt . --- .hooks/post-create | 9 +++++++++ cmd/tusd/cli/hooks.go | 5 +++++ cmd/tusd/cli/serve.go | 1 + config.go | 3 +++ post_test.go | 17 ++++++++++++++--- unrouted_handler.go | 12 ++++++++++++ 6 files changed, 44 insertions(+), 3 deletions(-) create mode 100755 .hooks/post-create diff --git a/.hooks/post-create b/.hooks/post-create new file mode 100755 index 0000000..b9bff36 --- /dev/null +++ b/.hooks/post-create @@ -0,0 +1,9 @@ +#!/bin/bash + +id="$TUS_ID" +offset="$TUS_OFFSET" +size="$TUS_SIZE" +progress=$((100 * $offset/$size)) + +echo "Upload created with ID ${id} and size ${size}" +cat /dev/stdin | jq . diff --git a/cmd/tusd/cli/hooks.go b/cmd/tusd/cli/hooks.go index 602636f..4a79a69 100644 --- a/cmd/tusd/cli/hooks.go +++ b/cmd/tusd/cli/hooks.go @@ -22,6 +22,7 @@ const ( HookPostFinish HookType = "post-finish" HookPostTerminate HookType = "post-terminate" HookPostReceive HookType = "post-receive" + HookPostCreate HookType = "post-create" HookPreCreate HookType = "pre-create" ) @@ -52,6 +53,8 @@ func SetupPostHooks(handler *tusd.Handler) { invokeHook(HookPostTerminate, info) case info := <-handler.UploadProgress: invokeHook(HookPostReceive, info) + case info := <-handler.UploadCreated: + invokeHook(HookPostCreate, info) } } }() @@ -66,6 +69,8 @@ func invokeHook(typ HookType, info tusd.FileInfo) { func invokeHookSync(typ HookType, info tusd.FileInfo, captureOutput bool) ([]byte, error) { switch typ { + case HookPostCreate: + logEv("UploadCreated", "id", info.ID, "size", strconv.FormatInt(info.Size, 10)) case HookPostFinish: logEv("UploadFinished", "id", info.ID, "size", strconv.FormatInt(info.Size, 10)) case HookPostTerminate: diff --git a/cmd/tusd/cli/serve.go b/cmd/tusd/cli/serve.go index 594d66e..63f2252 100644 --- a/cmd/tusd/cli/serve.go +++ b/cmd/tusd/cli/serve.go @@ -18,6 +18,7 @@ func Serve() { NotifyCompleteUploads: true, NotifyTerminatedUploads: true, NotifyUploadProgress: true, + NotifyUploadCreated: true, }) if err != nil { stderr.Fatalf("Unable to create handler: %s", err) diff --git a/config.go b/config.go index 94b4f7e..1848b22 100644 --- a/config.go +++ b/config.go @@ -34,6 +34,9 @@ type Config struct { // NotifyUploadProgress indicates whether sending notifications about // the upload progress using the UploadProgress channel should be enabled. NotifyUploadProgress bool + // NotifyUploadCreated indicates whether sending notifications about + // the upload having been created using the UploadCreated channel should be enabled. + NotifyUploadCreated bool // Logger is the logger to use internally, mostly for printing requests. Logger *log.Logger // Respect the X-Forwarded-Host, X-Forwarded-Proto and Forwarded headers diff --git a/post_test.go b/post_test.go index 605ad7c..8029031 100644 --- a/post_test.go +++ b/post_test.go @@ -7,8 +7,9 @@ import ( "testing" "github.com/golang/mock/gomock" - . "github.com/tus/tusd" + + "github.com/stretchr/testify/assert" ) func TestPost(t *testing.T) { @@ -22,10 +23,14 @@ func TestPost(t *testing.T) { }).Return("foo", nil) handler, _ := NewHandler(Config{ - DataStore: store, - BasePath: "https://buy.art/files/", + DataStore: store, + BasePath: "https://buy.art/files/", + NotifyUploadCreated: true, }) + c := make(chan FileInfo, 1) + handler.UploadCreated = c + (&httpTest{ Method: "POST", ReqHeader: map[string]string{ @@ -39,6 +44,12 @@ func TestPost(t *testing.T) { "Location": "https://buy.art/files/foo", }, }).Run(handler, t) + + info := <-c + + a := assert.New(t) + a.Equal("foo", info.ID) + a.Equal(int64(300), info.Size) }) SubTest(t, "CreateExceedingMaxSizeFail", func(t *testing.T, store *MockFullDataStore) { diff --git a/unrouted_handler.go b/unrouted_handler.go index 0cd6c6a..99468e1 100644 --- a/unrouted_handler.go +++ b/unrouted_handler.go @@ -94,6 +94,12 @@ type UnroutedHandler struct { // happen if the NotifyUploadProgress field is set to true in the Config // structure. UploadProgress chan FileInfo + // UploadCreated is used to send notifications about the uploads having been + // created. It triggers post creation and therefore has all the FileInfo incl. + // the ID available already. It facilitates the post-create hook. Sending to + // this channel will only happen if the NotifyUploadCreated field is set to + // true in the Config structure. + UploadCreated chan FileInfo // Metrics provides numbers of the usage for this handler. Metrics Metrics } @@ -124,6 +130,7 @@ func NewUnroutedHandler(config Config) (*UnroutedHandler, error) { CompleteUploads: make(chan FileInfo), TerminatedUploads: make(chan FileInfo), UploadProgress: make(chan FileInfo), + UploadCreated: make(chan FileInfo), logger: config.Logger, extensions: extensions, Metrics: newMetrics(), @@ -284,6 +291,11 @@ func (handler *UnroutedHandler) PostFile(w http.ResponseWriter, r *http.Request) go handler.Metrics.incUploadsCreated() handler.log("UploadCreated", "id", id, "size", i64toa(size), "url", url) + if handler.config.NotifyUploadCreated { + info.ID = id + handler.UploadCreated <- info + } + if isFinal { if err := handler.composer.Concater.ConcatUploads(id, partialUploads); err != nil { handler.sendError(w, r, err)