diff --git a/docs/hooks.md b/docs/hooks.md index 04acc92..b43e117 100644 --- a/docs/hooks.md +++ b/docs/hooks.md @@ -43,7 +43,7 @@ This event will be triggered after an upload has been terminated, meaning that t ### post-receive -This event will be triggered for every running upload to indicate its current progress. It will occur for each open PATCH request, every second. The offset property will be set to the number of bytes which have been transfered to the server, at the time in total. Please be aware that this number may be higher than the number of bytes which have been stored by the data store! +This event will be triggered for every running upload to indicate its current progress. It will be emitted whenever the server has received more data from the client but at most every second. The offset property will be set to the number of bytes which have been transfered to the server, at the time in total. Please be aware that this number may be higher than the number of bytes which have been stored by the data store! ## File Hooks diff --git a/head_test.go b/head_test.go index 021507b..ff58b53 100644 --- a/head_test.go +++ b/head_test.go @@ -85,7 +85,7 @@ func TestHead(t *testing.T) { SubTest(t, "DeferLengthHeader", func(t *testing.T, store *MockFullDataStore) { store.EXPECT().GetInfo("yes").Return(FileInfo{ SizeIsDeferred: true, - Size: 0, + Size: 0, }, nil) handler, _ := NewHandler(Config{ @@ -109,7 +109,7 @@ func TestHead(t *testing.T) { gomock.InOrder( store.EXPECT().GetInfo("yes").Return(FileInfo{ SizeIsDeferred: false, - Size: 10, + Size: 10, }, nil), ) diff --git a/patch_test.go b/patch_test.go index a2f85c0..750c345 100644 --- a/patch_test.go +++ b/patch_test.go @@ -458,10 +458,8 @@ func TestPatch(t *testing.T) { writer.Close() - info = <-c - a.Equal("yes", info.ID) - a.Equal(int64(100), info.Size) - a.Equal(int64(18), info.Offset) + // No progress event is sent after the writer is closed + // because an event for 18 bytes was already emitted. }() (&httpTest{ diff --git a/post_test.go b/post_test.go index 53f88af..d5915f1 100644 --- a/post_test.go +++ b/post_test.go @@ -149,7 +149,7 @@ func TestPost(t *testing.T) { Method: "POST", URL: "", ReqHeader: map[string]string{ - "Tus-Resumable": "1.0.0", + "Tus-Resumable": "1.0.0", }, Code: http.StatusBadRequest, }).Run(handler, t) diff --git a/unrouted_handler.go b/unrouted_handler.go index 5c697fd..96c1b7c 100644 --- a/unrouted_handler.go +++ b/unrouted_handler.go @@ -822,6 +822,7 @@ func (w *progressWriter) Write(b []byte) (int, error) { // It will stop sending these instances once the returned channel has been // closed. The returned reader should be used to read the request body. func (handler *UnroutedHandler) sendProgressMessages(info FileInfo, reader io.Reader) (io.Reader, chan<- struct{}) { + previousOffset := int64(0) progress := &progressWriter{ Offset: info.Offset, } @@ -833,11 +834,17 @@ func (handler *UnroutedHandler) sendProgressMessages(info FileInfo, reader io.Re select { case <-stop: info.Offset = atomic.LoadInt64(&progress.Offset) - handler.UploadProgress <- info + if info.Offset != previousOffset { + handler.UploadProgress <- info + previousOffset = info.Offset + } return case <-time.After(1 * time.Second): info.Offset = atomic.LoadInt64(&progress.Offset) - handler.UploadProgress <- info + if info.Offset != previousOffset { + handler.UploadProgress <- info + previousOffset = info.Offset + } } } }()