From 30b913d544ac0c1ca5dc9109387c8084f79ba93b Mon Sep 17 00:00:00 2001 From: Acconut Date: Sun, 26 Apr 2015 00:46:53 +0200 Subject: [PATCH] rename uploads channel and add option --- handler.go | 27 ++++++++++++++++----------- tusd/main.go | 9 +++++---- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/handler.go b/handler.go index 62438c9..18a65d5 100644 --- a/handler.go +++ b/handler.go @@ -61,6 +61,9 @@ type Config struct { // If no trailing slash is presented it will be added. You may specify an // absolute URL containing a scheme, e.g. "http://tus.io" BasePath string + // Initiate the CompleteUploads channel in the Handler struct in order to + // be notified about complete uploads + NotifyCompleteUploads bool } type Handler struct { @@ -71,8 +74,10 @@ type Handler struct { routeHandler http.Handler locks map[string]bool - // For each finished upload the corresponding info object will be sent - Uploads chan FileInfo + // For each finished upload the corresponding info object will be sent using + // this unbuffered channel. The NotifyCompleteUploads property in the Config + // struct must be set to true in order to work. + CompleteUploads chan FileInfo } // Create a new handler using the given configuration. @@ -96,13 +101,13 @@ func NewHandler(config Config) (*Handler, error) { mux := pat.New() handler := &Handler{ - config: config, - dataStore: config.DataStore, - basePath: base, - isBasePathAbs: uri.IsAbs(), - routeHandler: mux, - locks: make(map[string]bool), - Uploads: make(chan FileInfo), + config: config, + dataStore: config.DataStore, + basePath: base, + isBasePathAbs: uri.IsAbs(), + routeHandler: mux, + locks: make(map[string]bool), + CompleteUploads: make(chan FileInfo), } mux.Post("", http.HandlerFunc(handler.postFile)) @@ -329,9 +334,9 @@ func (handler *Handler) patchFile(w http.ResponseWriter, r *http.Request) { w.Header().Set("Upload-Offset", strconv.FormatInt(newOffset, 10)) // If the upload is completed, send the info out to the channel - if newOffset == info.Size { + if handler.config.NotifyCompleteUploads && newOffset == info.Size { info.Size = newOffset - handler.Uploads <- info + handler.CompleteUploads <- info } w.WriteHeader(http.StatusNoContent) diff --git a/tusd/main.go b/tusd/main.go index d385f2d..6f21e27 100644 --- a/tusd/main.go +++ b/tusd/main.go @@ -56,9 +56,10 @@ func main() { stdout.Printf("Using %.2fMB as maximum size.\n", float64(maxSize)/1024/1024) handler, err := tusd.NewHandler(tusd.Config{ - MaxSize: maxSize, - BasePath: "files/", - DataStore: store, + MaxSize: maxSize, + BasePath: "files/", + DataStore: store, + NotifyCompleteUploads: true, }) if err != nil { stderr.Fatalf("Unable to create handler: %s", err) @@ -70,7 +71,7 @@ func main() { go func() { for { select { - case info := <-handler.Uploads: + case info := <-handler.CompleteUploads: stdout.Printf("Upload %s (%d bytes) finished\n", info.ID, info.Size) } }