core: Fix wrong offset in upload progress notifications

Closes https://github.com/tus/tusd/issues/500
This commit is contained in:
Marius 2021-10-13 21:08:09 +02:00
parent 1b11885823
commit 0ad435b4c8
2 changed files with 11 additions and 8 deletions

View File

@ -497,14 +497,16 @@ func TestPatch(t *testing.T) {
defer ctrl.Finish()
upload := NewMockFullUpload(ctrl)
// We simulate that the upload has already an offset of 10 bytes. Therefore, the progress notifications
// must be the sum of the exisiting offset and the newly read bytes.
gomock.InOrder(
store.EXPECT().GetUpload(context.Background(), "yes").Return(upload, nil),
upload.EXPECT().GetInfo(context.Background()).Return(FileInfo{
ID: "yes",
Offset: 0,
Offset: 10,
Size: 100,
}, nil),
upload.EXPECT().WriteChunk(context.Background(), int64(0), NewReaderMatcher("first second third")).Return(int64(18), nil),
upload.EXPECT().WriteChunk(context.Background(), int64(10), NewReaderMatcher("first second third")).Return(int64(18), nil),
)
handler, _ := NewHandler(Config{
@ -525,7 +527,7 @@ func TestPatch(t *testing.T) {
info := event.Upload
a.Equal("yes", info.ID)
a.Equal(int64(100), info.Size)
a.Equal(int64(6), info.Offset)
a.Equal(int64(16), info.Offset)
writer.Write([]byte("second "))
writer.Write([]byte("third"))
@ -534,7 +536,7 @@ func TestPatch(t *testing.T) {
info = event.Upload
a.Equal("yes", info.ID)
a.Equal(int64(100), info.Size)
a.Equal(int64(18), info.Offset)
a.Equal(int64(28), info.Offset)
writer.Close()
@ -548,12 +550,12 @@ func TestPatch(t *testing.T) {
ReqHeader: map[string]string{
"Tus-Resumable": "1.0.0",
"Content-Type": "application/offset+octet-stream",
"Upload-Offset": "0",
"Upload-Offset": "10",
},
ReqBody: reader,
Code: http.StatusNoContent,
ResHeader: map[string]string{
"Upload-Offset": "18",
"Upload-Offset": "28",
},
}).Run(handler, t)

View File

@ -986,20 +986,21 @@ func (handler *UnroutedHandler) absFileURL(r *http.Request, id string) string {
// closed.
func (handler *UnroutedHandler) sendProgressMessages(hook HookEvent, reader *bodyReader) chan<- struct{} {
previousOffset := int64(0)
originalOffset := hook.Upload.Offset
stop := make(chan struct{}, 1)
go func() {
for {
select {
case <-stop:
hook.Upload.Offset = reader.bytesRead()
hook.Upload.Offset = originalOffset + reader.bytesRead()
if hook.Upload.Offset != previousOffset {
handler.UploadProgress <- hook
previousOffset = hook.Upload.Offset
}
return
case <-time.After(1 * time.Second):
hook.Upload.Offset = reader.bytesRead()
hook.Upload.Offset = originalOffset + reader.bytesRead()
if hook.Upload.Offset != previousOffset {
handler.UploadProgress <- hook
previousOffset = hook.Upload.Offset