From 4e8842d91cbefe1396e5a085cb0fcaa6064b8bd8 Mon Sep 17 00:00:00 2001 From: Marius Date: Sat, 9 Apr 2016 22:09:22 +0200 Subject: [PATCH] Do not pass chunk for uploaded uploads to data store --- patch_test.go | 38 ++++++++++++++++++++++++++++++++++++++ unrouted_handler.go | 9 ++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/patch_test.go b/patch_test.go index 58fed54..162e609 100644 --- a/patch_test.go +++ b/patch_test.go @@ -286,3 +286,41 @@ func TestLockingPatch(t *testing.T) { t.Error("expected no more calls to happen") } } + +type finishedPatchStore struct { + zeroStore +} + +func (s finishedPatchStore) GetInfo(id string) (FileInfo, error) { + return FileInfo{ + Offset: 20, + Size: 20, + }, nil +} + +func (s finishedPatchStore) WriteChunk(id string, offset int64, src io.Reader) (int64, error) { + panic("WriteChunk must not be called") +} + +func TestFinishedPatch(t *testing.T) { + + handler, _ := NewHandler(Config{ + DataStore: finishedPatchStore{}, + }) + + (&httpTest{ + Name: "Uploading to finished upload", + Method: "PATCH", + URL: "yes", + ReqHeader: map[string]string{ + "Tus-Resumable": "1.0.0", + "Content-Type": "application/offset+octet-stream", + "Upload-Offset": "20", + }, + ReqBody: strings.NewReader(""), + Code: http.StatusNoContent, + ResHeader: map[string]string{ + "Upload-Offset": "20", + }, + }).Run(handler, t) +} diff --git a/unrouted_handler.go b/unrouted_handler.go index 3303a8a..0e0baed 100644 --- a/unrouted_handler.go +++ b/unrouted_handler.go @@ -348,6 +348,13 @@ func (handler *UnroutedHandler) PatchFile(w http.ResponseWriter, r *http.Request return } + // Do not proxy the call to the data store if the upload is already completed + if info.Offset == info.Size { + w.Header().Set("Upload-Offset", strconv.FormatInt(offset, 10)) + w.WriteHeader(http.StatusNoContent) + return + } + // Get Content-Length if possible length := r.ContentLength @@ -362,7 +369,7 @@ func (handler *UnroutedHandler) PatchFile(w http.ResponseWriter, r *http.Request maxSize = length } - // Limit the + // Limit the data read from the request's body to the allowed maxiumum reader := io.LimitReader(r.Body, maxSize) bytesWritten, err := handler.composer.Core.WriteChunk(id, offset, reader)