diff --git a/handler.go b/handler.go index 49e59cf..62438c9 100644 --- a/handler.go +++ b/handler.go @@ -70,6 +70,9 @@ type Handler struct { basePath string routeHandler http.Handler locks map[string]bool + + // For each finished upload the corresponding info object will be sent + Uploads chan FileInfo } // Create a new handler using the given configuration. @@ -99,6 +102,7 @@ func NewHandler(config Config) (*Handler, error) { isBasePathAbs: uri.IsAbs(), routeHandler: mux, locks: make(map[string]bool), + Uploads: make(chan FileInfo), } mux.Post("", http.HandlerFunc(handler.postFile)) @@ -321,7 +325,14 @@ func (handler *Handler) patchFile(w http.ResponseWriter, r *http.Request) { } // Send new offset to client - w.Header().Set("Upload-Offset", strconv.FormatInt(offset+bytesWritten, 10)) + newOffset := offset + bytesWritten + 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 { + info.Size = newOffset + handler.Uploads <- info + } w.WriteHeader(http.StatusNoContent) } diff --git a/tusd/main.go b/tusd/main.go index 9842155..d385f2d 100644 --- a/tusd/main.go +++ b/tusd/main.go @@ -67,6 +67,15 @@ func main() { address := httpHost + ":" + httpPort stdout.Printf("Using %s as address to listen.\n", address) + go func() { + for { + select { + case info := <-handler.Uploads: + stdout.Printf("Upload %s (%d bytes) finished\n", info.ID, info.Size) + } + } + }() + http.Handle(basepath, http.StripPrefix(basepath, handler)) err = http.ListenAndServe(address, nil) if err != nil {