From 1038298a7999922f09a925e2f59edc813fa8deca Mon Sep 17 00:00:00 2001 From: Marius Date: Sun, 19 Jun 2022 12:18:02 +0200 Subject: [PATCH] core: Change GetReader to return io.ReadCloser (#739) Squashed commit of the following: commit e8b5b3751a86d86cae10e0bcf89caa481e5c3de6 Author: Marius Date: Sun Jun 19 12:15:22 2022 +0200 Fix generated mocks commit 736e2e7bb6af4538a602cc5ebcf0f08c5ce086f4 Merge: 9d7096f 1e69d9b Author: Stefan Scheidewig Date: Sat Jun 18 07:53:29 2022 +0200 Merge branch 'v2' into readcloser_in_getreader commit 9d7096fcb32927b4ddc8444fd5779ec5eb4f2ddc Author: Stefan Scheidewig Date: Tue May 24 14:16:01 2022 +0200 Return ReadCloser in getReader --- pkg/azurestore/azureservice.go | 29 +-- pkg/azurestore/azurestore.go | 13 +- pkg/azurestore/azurestore_mock_test.go | 49 ++--- pkg/azurestore/azurestore_test.go | 21 ++- pkg/filestore/filestore.go | 2 +- pkg/gcsstore/gcsstore.go | 9 +- pkg/gcsstore/gcsstore_mock_test.go | 73 ++++---- pkg/handler/datastore.go | 6 +- pkg/handler/handler_mock_test.go | 249 +++++++++++++------------ pkg/handler/unrouted_handler.go | 5 +- pkg/handler/utils_test.go | 2 +- pkg/s3store/s3store.go | 2 +- pkg/s3store/s3store_mock_test.go | 52 +++--- 13 files changed, 245 insertions(+), 267 deletions(-) diff --git a/pkg/azurestore/azureservice.go b/pkg/azurestore/azureservice.go index 9f27a30..d8210dc 100644 --- a/pkg/azurestore/azureservice.go +++ b/pkg/azurestore/azureservice.go @@ -15,7 +15,6 @@ package azurestore import ( - "bytes" "context" "encoding/base64" "encoding/binary" @@ -59,8 +58,8 @@ type AzBlob interface { Delete(ctx context.Context) error // Upload the blob Upload(ctx context.Context, body io.ReadSeeker) error - // Download the contents of the blob - Download(ctx context.Context) ([]byte, error) + // Download returns a readcloser to download the contents of the blob + Download(ctx context.Context) (io.ReadCloser, error) // Get the offset of the blob and its indexes GetOffset(ctx context.Context) (int64, error) // Commit the uploaded blocks to the BlockBlob @@ -171,7 +170,7 @@ func (blockBlob *BlockBlob) Upload(ctx context.Context, body io.ReadSeeker) erro } // Download the blockBlob from Azure Blob Storage -func (blockBlob *BlockBlob) Download(ctx context.Context) (data []byte, err error) { +func (blockBlob *BlockBlob) Download(ctx context.Context) (io.ReadCloser, error) { downloadResponse, err := blockBlob.Blob.Download(ctx, 0, azblob.CountToEnd, azblob.BlobAccessConditions{}, false, azblob.ClientProvidedKeyOptions{}) // If the file does not exist, it will not return an error, but a 404 status and body @@ -186,15 +185,7 @@ func (blockBlob *BlockBlob) Download(ctx context.Context) (data []byte, err erro return nil, err } - bodyStream := downloadResponse.Body(azblob.RetryReaderOptions{MaxRetryRequests: 20}) - downloadedData := bytes.Buffer{} - - _, err = downloadedData.ReadFrom(bodyStream) - if err != nil { - return nil, err - } - - return downloadedData.Bytes(), nil + return downloadResponse.Body(azblob.RetryReaderOptions{MaxRetryRequests: 20}), nil } func (blockBlob *BlockBlob) GetOffset(ctx context.Context) (int64, error) { @@ -258,7 +249,7 @@ func (infoBlob *InfoBlob) Upload(ctx context.Context, body io.ReadSeeker) error } // Download the infoBlob from Azure Blob Storage -func (infoBlob *InfoBlob) Download(ctx context.Context) ([]byte, error) { +func (infoBlob *InfoBlob) Download(ctx context.Context) (io.ReadCloser, error) { downloadResponse, err := infoBlob.Blob.Download(ctx, 0, azblob.CountToEnd, azblob.BlobAccessConditions{}, false, azblob.ClientProvidedKeyOptions{}) // If the file does not exist, it will not return an error, but a 404 status and body @@ -272,15 +263,7 @@ func (infoBlob *InfoBlob) Download(ctx context.Context) ([]byte, error) { return nil, err } - bodyStream := downloadResponse.Body(azblob.RetryReaderOptions{MaxRetryRequests: 20}) - downloadedData := bytes.Buffer{} - - _, err = downloadedData.ReadFrom(bodyStream) - if err != nil { - return nil, err - } - - return downloadedData.Bytes(), nil + return downloadResponse.Body(azblob.RetryReaderOptions{MaxRetryRequests: 20}), nil } // infoBlob does not utilise offset, so just return 0, nil diff --git a/pkg/azurestore/azurestore.go b/pkg/azurestore/azurestore.go index d21cbf0..9fb6d40 100644 --- a/pkg/azurestore/azurestore.go +++ b/pkg/azurestore/azurestore.go @@ -96,8 +96,9 @@ func (store AzureStore) GetUpload(ctx context.Context, id string) (handler.Uploa if err != nil { return nil, err } + defer data.Close() - if err := json.Unmarshal(data, &info); err != nil { + if err := json.NewDecoder(data).Decode(&info); err != nil { return nil, err } @@ -173,7 +174,7 @@ func (upload *AzUpload) GetInfo(ctx context.Context) (handler.FileInfo, error) { return info, err } - if err := json.Unmarshal(data, &info); err != nil { + if err := json.NewDecoder(data).Decode(&info); err != nil { return info, err } @@ -182,12 +183,8 @@ func (upload *AzUpload) GetInfo(ctx context.Context) (handler.FileInfo, error) { } // Get the uploaded file from the Azure storage -func (upload *AzUpload) GetReader(ctx context.Context) (io.Reader, error) { - b, err := upload.BlockBlob.Download(ctx) - if err != nil { - return nil, err - } - return bytes.NewReader(b), nil +func (upload *AzUpload) GetReader(ctx context.Context) (io.ReadCloser, error) { + return upload.BlockBlob.Download(ctx) } // Finish the file upload and commit the block list diff --git a/pkg/azurestore/azurestore_mock_test.go b/pkg/azurestore/azurestore_mock_test.go index 0af7cba..8d11838 100644 --- a/pkg/azurestore/azurestore_mock_test.go +++ b/pkg/azurestore/azurestore_mock_test.go @@ -6,36 +6,37 @@ package azurestore_test import ( context "context" - gomock "github.com/golang/mock/gomock" - azurestore "github.com/tus/tusd/pkg/azurestore" io "io" reflect "reflect" + + gomock "github.com/golang/mock/gomock" + azurestore "github.com/tus/tusd/pkg/azurestore" ) -// MockAzService is a mock of AzService interface +// MockAzService is a mock of AzService interface. type MockAzService struct { ctrl *gomock.Controller recorder *MockAzServiceMockRecorder } -// MockAzServiceMockRecorder is the mock recorder for MockAzService +// MockAzServiceMockRecorder is the mock recorder for MockAzService. type MockAzServiceMockRecorder struct { mock *MockAzService } -// NewMockAzService creates a new mock instance +// NewMockAzService creates a new mock instance. func NewMockAzService(ctrl *gomock.Controller) *MockAzService { mock := &MockAzService{ctrl: ctrl} mock.recorder = &MockAzServiceMockRecorder{mock} return mock } -// EXPECT returns an object that allows the caller to indicate expected use +// EXPECT returns an object that allows the caller to indicate expected use. func (m *MockAzService) EXPECT() *MockAzServiceMockRecorder { return m.recorder } -// NewBlob mocks base method +// NewBlob mocks base method. func (m *MockAzService) NewBlob(arg0 context.Context, arg1 string) (azurestore.AzBlob, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "NewBlob", arg0, arg1) @@ -44,36 +45,36 @@ func (m *MockAzService) NewBlob(arg0 context.Context, arg1 string) (azurestore.A return ret0, ret1 } -// NewBlob indicates an expected call of NewBlob +// NewBlob indicates an expected call of NewBlob. func (mr *MockAzServiceMockRecorder) NewBlob(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewBlob", reflect.TypeOf((*MockAzService)(nil).NewBlob), arg0, arg1) } -// MockAzBlob is a mock of AzBlob interface +// MockAzBlob is a mock of AzBlob interface. type MockAzBlob struct { ctrl *gomock.Controller recorder *MockAzBlobMockRecorder } -// MockAzBlobMockRecorder is the mock recorder for MockAzBlob +// MockAzBlobMockRecorder is the mock recorder for MockAzBlob. type MockAzBlobMockRecorder struct { mock *MockAzBlob } -// NewMockAzBlob creates a new mock instance +// NewMockAzBlob creates a new mock instance. func NewMockAzBlob(ctrl *gomock.Controller) *MockAzBlob { mock := &MockAzBlob{ctrl: ctrl} mock.recorder = &MockAzBlobMockRecorder{mock} return mock } -// EXPECT returns an object that allows the caller to indicate expected use +// EXPECT returns an object that allows the caller to indicate expected use. func (m *MockAzBlob) EXPECT() *MockAzBlobMockRecorder { return m.recorder } -// Commit mocks base method +// Commit mocks base method. func (m *MockAzBlob) Commit(arg0 context.Context) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Commit", arg0) @@ -81,13 +82,13 @@ func (m *MockAzBlob) Commit(arg0 context.Context) error { return ret0 } -// Commit indicates an expected call of Commit +// Commit indicates an expected call of Commit. func (mr *MockAzBlobMockRecorder) Commit(arg0 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Commit", reflect.TypeOf((*MockAzBlob)(nil).Commit), arg0) } -// Delete mocks base method +// Delete mocks base method. func (m *MockAzBlob) Delete(arg0 context.Context) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Delete", arg0) @@ -95,28 +96,28 @@ func (m *MockAzBlob) Delete(arg0 context.Context) error { return ret0 } -// Delete indicates an expected call of Delete +// Delete indicates an expected call of Delete. func (mr *MockAzBlobMockRecorder) Delete(arg0 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockAzBlob)(nil).Delete), arg0) } -// Download mocks base method -func (m *MockAzBlob) Download(arg0 context.Context) ([]byte, error) { +// Download mocks base method. +func (m *MockAzBlob) Download(arg0 context.Context) (io.ReadCloser, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Download", arg0) - ret0, _ := ret[0].([]byte) + ret0, _ := ret[0].(io.ReadCloser) ret1, _ := ret[1].(error) return ret0, ret1 } -// Download indicates an expected call of Download +// Download indicates an expected call of Download. func (mr *MockAzBlobMockRecorder) Download(arg0 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Download", reflect.TypeOf((*MockAzBlob)(nil).Download), arg0) } -// GetOffset mocks base method +// GetOffset mocks base method. func (m *MockAzBlob) GetOffset(arg0 context.Context) (int64, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetOffset", arg0) @@ -125,13 +126,13 @@ func (m *MockAzBlob) GetOffset(arg0 context.Context) (int64, error) { return ret0, ret1 } -// GetOffset indicates an expected call of GetOffset +// GetOffset indicates an expected call of GetOffset. func (mr *MockAzBlobMockRecorder) GetOffset(arg0 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetOffset", reflect.TypeOf((*MockAzBlob)(nil).GetOffset), arg0) } -// Upload mocks base method +// Upload mocks base method. func (m *MockAzBlob) Upload(arg0 context.Context, arg1 io.ReadSeeker) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Upload", arg0, arg1) @@ -139,7 +140,7 @@ func (m *MockAzBlob) Upload(arg0 context.Context, arg1 io.ReadSeeker) error { return ret0 } -// Upload indicates an expected call of Upload +// Upload indicates an expected call of Upload. func (mr *MockAzBlobMockRecorder) Upload(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Upload", reflect.TypeOf((*MockAzBlob)(nil).Upload), arg0, arg1) diff --git a/pkg/azurestore/azurestore_test.go b/pkg/azurestore/azurestore_test.go index abd09db..5bbe15a 100644 --- a/pkg/azurestore/azurestore_test.go +++ b/pkg/azurestore/azurestore_test.go @@ -5,6 +5,7 @@ import ( "context" "encoding/json" "errors" + "io" "testing" "github.com/Azure/azure-storage-blob-go/azblob" @@ -153,7 +154,7 @@ func TestGetUpload(t *testing.T) { gomock.InOrder( service.EXPECT().NewBlob(ctx, mockID+".info").Return(infoBlob, nil).Times(1), - infoBlob.EXPECT().Download(ctx).Return(data, nil).Times(1), + infoBlob.EXPECT().Download(ctx).Return(newReadCloser(data), nil).Times(1), service.EXPECT().NewBlob(ctx, mockID).Return(blockBlob, nil).Times(1), blockBlob.EXPECT().GetOffset(ctx).Return(int64(0), nil).Times(1), ) @@ -189,7 +190,7 @@ func TestGetUploadTooLargeBlob(t *testing.T) { gomock.InOrder( service.EXPECT().NewBlob(ctx, mockID+".info").Return(infoBlob, nil).Times(1), - infoBlob.EXPECT().Download(ctx).Return(data, nil).Times(1), + infoBlob.EXPECT().Download(ctx).Return(newReadCloser(data), nil).Times(1), ) upload, err := store.GetUpload(ctx, mockID) @@ -246,10 +247,10 @@ func TestGetReader(t *testing.T) { gomock.InOrder( service.EXPECT().NewBlob(ctx, mockID+".info").Return(infoBlob, nil).Times(1), - infoBlob.EXPECT().Download(ctx).Return(data, nil).Times(1), + infoBlob.EXPECT().Download(ctx).Return(newReadCloser(data), nil).Times(1), service.EXPECT().NewBlob(ctx, mockID).Return(blockBlob, nil).Times(1), blockBlob.EXPECT().GetOffset(ctx).Return(int64(0), nil).Times(1), - blockBlob.EXPECT().Download(ctx).Return([]byte(mockReaderData), nil).Times(1), + blockBlob.EXPECT().Download(ctx).Return(newReadCloser([]byte(mockReaderData)), nil).Times(1), ) upload, err := store.GetUpload(ctx, mockID) @@ -286,7 +287,7 @@ func TestWriteChunk(t *testing.T) { gomock.InOrder( service.EXPECT().NewBlob(ctx, mockID+".info").Return(infoBlob, nil).Times(1), - infoBlob.EXPECT().Download(ctx).Return(data, nil).Times(1), + infoBlob.EXPECT().Download(ctx).Return(newReadCloser(data), nil).Times(1), service.EXPECT().NewBlob(ctx, mockID).Return(blockBlob, nil).Times(1), blockBlob.EXPECT().GetOffset(ctx).Return(offset, nil).Times(1), blockBlob.EXPECT().Upload(ctx, bytes.NewReader([]byte(mockReaderData))).Return(nil).Times(1), @@ -325,7 +326,7 @@ func TestFinishUpload(t *testing.T) { gomock.InOrder( service.EXPECT().NewBlob(ctx, mockID+".info").Return(infoBlob, nil).Times(1), - infoBlob.EXPECT().Download(ctx).Return(data, nil).Times(1), + infoBlob.EXPECT().Download(ctx).Return(newReadCloser(data), nil).Times(1), service.EXPECT().NewBlob(ctx, mockID).Return(blockBlob, nil).Times(1), blockBlob.EXPECT().GetOffset(ctx).Return(offset, nil).Times(1), blockBlob.EXPECT().Commit(ctx).Return(nil).Times(1), @@ -362,7 +363,7 @@ func TestTerminate(t *testing.T) { gomock.InOrder( service.EXPECT().NewBlob(ctx, mockID+".info").Return(infoBlob, nil).Times(1), - infoBlob.EXPECT().Download(ctx).Return(data, nil).Times(1), + infoBlob.EXPECT().Download(ctx).Return(newReadCloser(data), nil).Times(1), service.EXPECT().NewBlob(ctx, mockID).Return(blockBlob, nil).Times(1), blockBlob.EXPECT().GetOffset(ctx).Return(int64(0), nil).Times(1), infoBlob.EXPECT().Delete(ctx).Return(nil).Times(1), @@ -405,7 +406,7 @@ func TestDeclareLength(t *testing.T) { gomock.InOrder( service.EXPECT().NewBlob(ctx, mockID+".info").Return(infoBlob, nil).Times(1), - infoBlob.EXPECT().Download(ctx).Return(data, nil).Times(1), + infoBlob.EXPECT().Download(ctx).Return(newReadCloser(data), nil).Times(1), service.EXPECT().NewBlob(ctx, mockID).Return(blockBlob, nil).Times(1), blockBlob.EXPECT().GetOffset(ctx).Return(int64(0), nil).Times(1), infoBlob.EXPECT().Upload(ctx, r).Return(nil).Times(1), @@ -424,3 +425,7 @@ func TestDeclareLength(t *testing.T) { cancel() } + +func newReadCloser(b []byte) io.ReadCloser { + return io.NopCloser(bytes.NewReader(b)) +} diff --git a/pkg/filestore/filestore.go b/pkg/filestore/filestore.go index 1f79279..f528142 100644 --- a/pkg/filestore/filestore.go +++ b/pkg/filestore/filestore.go @@ -167,7 +167,7 @@ func (upload *fileUpload) WriteChunk(ctx context.Context, offset int64, src io.R return n, err } -func (upload *fileUpload) GetReader(ctx context.Context) (io.Reader, error) { +func (upload *fileUpload) GetReader(ctx context.Context) (io.ReadCloser, error) { return os.Open(upload.binPath) } diff --git a/pkg/gcsstore/gcsstore.go b/pkg/gcsstore/gcsstore.go index 04a757c..421bd4e 100644 --- a/pkg/gcsstore/gcsstore.go +++ b/pkg/gcsstore/gcsstore.go @@ -325,7 +325,7 @@ func (upload gcsUpload) Terminate(ctx context.Context) error { return nil } -func (upload gcsUpload) GetReader(ctx context.Context) (io.Reader, error) { +func (upload gcsUpload) GetReader(ctx context.Context) (io.ReadCloser, error) { id := upload.id store := upload.store @@ -334,12 +334,7 @@ func (upload gcsUpload) GetReader(ctx context.Context) (io.Reader, error) { ID: store.keyWithPrefix(id), } - r, err := store.Service.ReadObject(ctx, params) - if err != nil { - return nil, err - } - - return r, nil + return store.Service.ReadObject(ctx, params) } func (store GCSStore) keyWithPrefix(key string) string { diff --git a/pkg/gcsstore/gcsstore_mock_test.go b/pkg/gcsstore/gcsstore_mock_test.go index 774538a..8ccecdd 100644 --- a/pkg/gcsstore/gcsstore_mock_test.go +++ b/pkg/gcsstore/gcsstore_mock_test.go @@ -6,36 +6,37 @@ package gcsstore_test import ( context "context" - gomock "github.com/golang/mock/gomock" - gcsstore "github.com/tus/tusd/pkg/gcsstore" io "io" reflect "reflect" + + gomock "github.com/golang/mock/gomock" + gcsstore "github.com/tus/tusd/pkg/gcsstore" ) -// MockGCSReader is a mock of GCSReader interface +// MockGCSReader is a mock of GCSReader interface. type MockGCSReader struct { ctrl *gomock.Controller recorder *MockGCSReaderMockRecorder } -// MockGCSReaderMockRecorder is the mock recorder for MockGCSReader +// MockGCSReaderMockRecorder is the mock recorder for MockGCSReader. type MockGCSReaderMockRecorder struct { mock *MockGCSReader } -// NewMockGCSReader creates a new mock instance +// NewMockGCSReader creates a new mock instance. func NewMockGCSReader(ctrl *gomock.Controller) *MockGCSReader { mock := &MockGCSReader{ctrl: ctrl} mock.recorder = &MockGCSReaderMockRecorder{mock} return mock } -// EXPECT returns an object that allows the caller to indicate expected use +// EXPECT returns an object that allows the caller to indicate expected use. func (m *MockGCSReader) EXPECT() *MockGCSReaderMockRecorder { return m.recorder } -// Close mocks base method +// Close mocks base method. func (m *MockGCSReader) Close() error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Close") @@ -43,13 +44,13 @@ func (m *MockGCSReader) Close() error { return ret0 } -// Close indicates an expected call of Close +// Close indicates an expected call of Close. func (mr *MockGCSReaderMockRecorder) Close() *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Close", reflect.TypeOf((*MockGCSReader)(nil).Close)) } -// ContentType mocks base method +// ContentType mocks base method. func (m *MockGCSReader) ContentType() string { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ContentType") @@ -57,13 +58,13 @@ func (m *MockGCSReader) ContentType() string { return ret0 } -// ContentType indicates an expected call of ContentType +// ContentType indicates an expected call of ContentType. func (mr *MockGCSReaderMockRecorder) ContentType() *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ContentType", reflect.TypeOf((*MockGCSReader)(nil).ContentType)) } -// Read mocks base method +// Read mocks base method. func (m *MockGCSReader) Read(arg0 []byte) (int, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Read", arg0) @@ -72,13 +73,13 @@ func (m *MockGCSReader) Read(arg0 []byte) (int, error) { return ret0, ret1 } -// Read indicates an expected call of Read +// Read indicates an expected call of Read. func (mr *MockGCSReaderMockRecorder) Read(arg0 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Read", reflect.TypeOf((*MockGCSReader)(nil).Read), arg0) } -// Remain mocks base method +// Remain mocks base method. func (m *MockGCSReader) Remain() int64 { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Remain") @@ -86,13 +87,13 @@ func (m *MockGCSReader) Remain() int64 { return ret0 } -// Remain indicates an expected call of Remain +// Remain indicates an expected call of Remain. func (mr *MockGCSReaderMockRecorder) Remain() *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Remain", reflect.TypeOf((*MockGCSReader)(nil).Remain)) } -// Size mocks base method +// Size mocks base method. func (m *MockGCSReader) Size() int64 { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Size") @@ -100,36 +101,36 @@ func (m *MockGCSReader) Size() int64 { return ret0 } -// Size indicates an expected call of Size +// Size indicates an expected call of Size. func (mr *MockGCSReaderMockRecorder) Size() *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Size", reflect.TypeOf((*MockGCSReader)(nil).Size)) } -// MockGCSAPI is a mock of GCSAPI interface +// MockGCSAPI is a mock of GCSAPI interface. type MockGCSAPI struct { ctrl *gomock.Controller recorder *MockGCSAPIMockRecorder } -// MockGCSAPIMockRecorder is the mock recorder for MockGCSAPI +// MockGCSAPIMockRecorder is the mock recorder for MockGCSAPI. type MockGCSAPIMockRecorder struct { mock *MockGCSAPI } -// NewMockGCSAPI creates a new mock instance +// NewMockGCSAPI creates a new mock instance. func NewMockGCSAPI(ctrl *gomock.Controller) *MockGCSAPI { mock := &MockGCSAPI{ctrl: ctrl} mock.recorder = &MockGCSAPIMockRecorder{mock} return mock } -// EXPECT returns an object that allows the caller to indicate expected use +// EXPECT returns an object that allows the caller to indicate expected use. func (m *MockGCSAPI) EXPECT() *MockGCSAPIMockRecorder { return m.recorder } -// ComposeObjects mocks base method +// ComposeObjects mocks base method. func (m *MockGCSAPI) ComposeObjects(arg0 context.Context, arg1 gcsstore.GCSComposeParams) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ComposeObjects", arg0, arg1) @@ -137,13 +138,13 @@ func (m *MockGCSAPI) ComposeObjects(arg0 context.Context, arg1 gcsstore.GCSCompo return ret0 } -// ComposeObjects indicates an expected call of ComposeObjects +// ComposeObjects indicates an expected call of ComposeObjects. func (mr *MockGCSAPIMockRecorder) ComposeObjects(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ComposeObjects", reflect.TypeOf((*MockGCSAPI)(nil).ComposeObjects), arg0, arg1) } -// DeleteObject mocks base method +// DeleteObject mocks base method. func (m *MockGCSAPI) DeleteObject(arg0 context.Context, arg1 gcsstore.GCSObjectParams) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "DeleteObject", arg0, arg1) @@ -151,13 +152,13 @@ func (m *MockGCSAPI) DeleteObject(arg0 context.Context, arg1 gcsstore.GCSObjectP return ret0 } -// DeleteObject indicates an expected call of DeleteObject +// DeleteObject indicates an expected call of DeleteObject. func (mr *MockGCSAPIMockRecorder) DeleteObject(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteObject", reflect.TypeOf((*MockGCSAPI)(nil).DeleteObject), arg0, arg1) } -// DeleteObjectsWithFilter mocks base method +// DeleteObjectsWithFilter mocks base method. func (m *MockGCSAPI) DeleteObjectsWithFilter(arg0 context.Context, arg1 gcsstore.GCSFilterParams) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "DeleteObjectsWithFilter", arg0, arg1) @@ -165,13 +166,13 @@ func (m *MockGCSAPI) DeleteObjectsWithFilter(arg0 context.Context, arg1 gcsstore return ret0 } -// DeleteObjectsWithFilter indicates an expected call of DeleteObjectsWithFilter +// DeleteObjectsWithFilter indicates an expected call of DeleteObjectsWithFilter. func (mr *MockGCSAPIMockRecorder) DeleteObjectsWithFilter(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteObjectsWithFilter", reflect.TypeOf((*MockGCSAPI)(nil).DeleteObjectsWithFilter), arg0, arg1) } -// FilterObjects mocks base method +// FilterObjects mocks base method. func (m *MockGCSAPI) FilterObjects(arg0 context.Context, arg1 gcsstore.GCSFilterParams) ([]string, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "FilterObjects", arg0, arg1) @@ -180,13 +181,13 @@ func (m *MockGCSAPI) FilterObjects(arg0 context.Context, arg1 gcsstore.GCSFilter return ret0, ret1 } -// FilterObjects indicates an expected call of FilterObjects +// FilterObjects indicates an expected call of FilterObjects. func (mr *MockGCSAPIMockRecorder) FilterObjects(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FilterObjects", reflect.TypeOf((*MockGCSAPI)(nil).FilterObjects), arg0, arg1) } -// GetObjectSize mocks base method +// GetObjectSize mocks base method. func (m *MockGCSAPI) GetObjectSize(arg0 context.Context, arg1 gcsstore.GCSObjectParams) (int64, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetObjectSize", arg0, arg1) @@ -195,13 +196,13 @@ func (m *MockGCSAPI) GetObjectSize(arg0 context.Context, arg1 gcsstore.GCSObject return ret0, ret1 } -// GetObjectSize indicates an expected call of GetObjectSize +// GetObjectSize indicates an expected call of GetObjectSize. func (mr *MockGCSAPIMockRecorder) GetObjectSize(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetObjectSize", reflect.TypeOf((*MockGCSAPI)(nil).GetObjectSize), arg0, arg1) } -// ReadObject mocks base method +// ReadObject mocks base method. func (m *MockGCSAPI) ReadObject(arg0 context.Context, arg1 gcsstore.GCSObjectParams) (gcsstore.GCSReader, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "ReadObject", arg0, arg1) @@ -210,13 +211,13 @@ func (m *MockGCSAPI) ReadObject(arg0 context.Context, arg1 gcsstore.GCSObjectPar return ret0, ret1 } -// ReadObject indicates an expected call of ReadObject +// ReadObject indicates an expected call of ReadObject. func (mr *MockGCSAPIMockRecorder) ReadObject(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReadObject", reflect.TypeOf((*MockGCSAPI)(nil).ReadObject), arg0, arg1) } -// SetObjectMetadata mocks base method +// SetObjectMetadata mocks base method. func (m *MockGCSAPI) SetObjectMetadata(arg0 context.Context, arg1 gcsstore.GCSObjectParams, arg2 map[string]string) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "SetObjectMetadata", arg0, arg1, arg2) @@ -224,13 +225,13 @@ func (m *MockGCSAPI) SetObjectMetadata(arg0 context.Context, arg1 gcsstore.GCSOb return ret0 } -// SetObjectMetadata indicates an expected call of SetObjectMetadata +// SetObjectMetadata indicates an expected call of SetObjectMetadata. func (mr *MockGCSAPIMockRecorder) SetObjectMetadata(arg0, arg1, arg2 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "SetObjectMetadata", reflect.TypeOf((*MockGCSAPI)(nil).SetObjectMetadata), arg0, arg1, arg2) } -// WriteObject mocks base method +// WriteObject mocks base method. func (m *MockGCSAPI) WriteObject(arg0 context.Context, arg1 gcsstore.GCSObjectParams, arg2 io.Reader) (int64, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "WriteObject", arg0, arg1, arg2) @@ -239,7 +240,7 @@ func (m *MockGCSAPI) WriteObject(arg0 context.Context, arg1 gcsstore.GCSObjectPa return ret0, ret1 } -// WriteObject indicates an expected call of WriteObject +// WriteObject indicates an expected call of WriteObject. func (mr *MockGCSAPIMockRecorder) WriteObject(arg0, arg1, arg2 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WriteObject", reflect.TypeOf((*MockGCSAPI)(nil).WriteObject), arg0, arg1, arg2) diff --git a/pkg/handler/datastore.go b/pkg/handler/datastore.go index 0f8b6e3..2321141 100644 --- a/pkg/handler/datastore.go +++ b/pkg/handler/datastore.go @@ -63,14 +63,12 @@ type Upload interface { // requests. It may return an os.ErrNotExist which will be interpreted as a // 404 Not Found. GetInfo(ctx context.Context) (FileInfo, error) - // GetReader returns a reader which allows iterating of the content of an + // GetReader returns an io.ReadCloser which allows iterating of the content of an // upload specified by its ID. It should attempt to provide a reader even if // the upload has not been finished yet but it's not required. - // If the returned reader also implements the io.Closer interface, the - // Close() method will be invoked once everything has been read. // If the given upload could not be found, the error tusd.ErrNotFound should // be returned. - GetReader(ctx context.Context) (io.Reader, error) + GetReader(ctx context.Context) (io.ReadCloser, error) // FinisherDataStore is the interface which can be implemented by DataStores // which need to do additional operations once an entire upload has been // completed. These tasks may include but are not limited to freeing unused diff --git a/pkg/handler/handler_mock_test.go b/pkg/handler/handler_mock_test.go index 3a49eed..58d1a69 100644 --- a/pkg/handler/handler_mock_test.go +++ b/pkg/handler/handler_mock_test.go @@ -6,51 +6,79 @@ package handler_test import ( context "context" - gomock "github.com/golang/mock/gomock" - handler "github.com/tus/tusd/pkg/handler" io "io" reflect "reflect" + + gomock "github.com/golang/mock/gomock" + handler "github.com/tus/tusd/pkg/handler" ) -// MockFullDataStore is a mock of FullDataStore interface +// MockFullDataStore is a mock of FullDataStore interface. type MockFullDataStore struct { ctrl *gomock.Controller recorder *MockFullDataStoreMockRecorder } -// MockFullDataStoreMockRecorder is the mock recorder for MockFullDataStore +// MockFullDataStoreMockRecorder is the mock recorder for MockFullDataStore. type MockFullDataStoreMockRecorder struct { mock *MockFullDataStore } -// NewMockFullDataStore creates a new mock instance +// NewMockFullDataStore creates a new mock instance. func NewMockFullDataStore(ctrl *gomock.Controller) *MockFullDataStore { mock := &MockFullDataStore{ctrl: ctrl} mock.recorder = &MockFullDataStoreMockRecorder{mock} return mock } -// EXPECT returns an object that allows the caller to indicate expected use +// EXPECT returns an object that allows the caller to indicate expected use. func (m *MockFullDataStore) EXPECT() *MockFullDataStoreMockRecorder { return m.recorder } -// NewUpload mocks base method -func (m *MockFullDataStore) NewUpload(ctx context.Context, info handler.FileInfo) (handler.Upload, error) { +// AsConcatableUpload mocks base method. +func (m *MockFullDataStore) AsConcatableUpload(upload handler.Upload) handler.ConcatableUpload { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "NewUpload", ctx, info) - ret0, _ := ret[0].(handler.Upload) - ret1, _ := ret[1].(error) - return ret0, ret1 + ret := m.ctrl.Call(m, "AsConcatableUpload", upload) + ret0, _ := ret[0].(handler.ConcatableUpload) + return ret0 } -// NewUpload indicates an expected call of NewUpload -func (mr *MockFullDataStoreMockRecorder) NewUpload(ctx, info interface{}) *gomock.Call { +// AsConcatableUpload indicates an expected call of AsConcatableUpload. +func (mr *MockFullDataStoreMockRecorder) AsConcatableUpload(upload interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewUpload", reflect.TypeOf((*MockFullDataStore)(nil).NewUpload), ctx, info) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AsConcatableUpload", reflect.TypeOf((*MockFullDataStore)(nil).AsConcatableUpload), upload) } -// GetUpload mocks base method +// AsLengthDeclarableUpload mocks base method. +func (m *MockFullDataStore) AsLengthDeclarableUpload(upload handler.Upload) handler.LengthDeclarableUpload { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "AsLengthDeclarableUpload", upload) + ret0, _ := ret[0].(handler.LengthDeclarableUpload) + return ret0 +} + +// AsLengthDeclarableUpload indicates an expected call of AsLengthDeclarableUpload. +func (mr *MockFullDataStoreMockRecorder) AsLengthDeclarableUpload(upload interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AsLengthDeclarableUpload", reflect.TypeOf((*MockFullDataStore)(nil).AsLengthDeclarableUpload), upload) +} + +// AsTerminatableUpload mocks base method. +func (m *MockFullDataStore) AsTerminatableUpload(upload handler.Upload) handler.TerminatableUpload { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "AsTerminatableUpload", upload) + ret0, _ := ret[0].(handler.TerminatableUpload) + return ret0 +} + +// AsTerminatableUpload indicates an expected call of AsTerminatableUpload. +func (mr *MockFullDataStoreMockRecorder) AsTerminatableUpload(upload interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AsTerminatableUpload", reflect.TypeOf((*MockFullDataStore)(nil).AsTerminatableUpload), upload) +} + +// GetUpload mocks base method. func (m *MockFullDataStore) GetUpload(ctx context.Context, id string) (handler.Upload, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetUpload", ctx, id) @@ -59,93 +87,93 @@ func (m *MockFullDataStore) GetUpload(ctx context.Context, id string) (handler.U return ret0, ret1 } -// GetUpload indicates an expected call of GetUpload +// GetUpload indicates an expected call of GetUpload. func (mr *MockFullDataStoreMockRecorder) GetUpload(ctx, id interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUpload", reflect.TypeOf((*MockFullDataStore)(nil).GetUpload), ctx, id) } -// AsTerminatableUpload mocks base method -func (m *MockFullDataStore) AsTerminatableUpload(upload handler.Upload) handler.TerminatableUpload { +// NewUpload mocks base method. +func (m *MockFullDataStore) NewUpload(ctx context.Context, info handler.FileInfo) (handler.Upload, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "AsTerminatableUpload", upload) - ret0, _ := ret[0].(handler.TerminatableUpload) - return ret0 + ret := m.ctrl.Call(m, "NewUpload", ctx, info) + ret0, _ := ret[0].(handler.Upload) + ret1, _ := ret[1].(error) + return ret0, ret1 } -// AsTerminatableUpload indicates an expected call of AsTerminatableUpload -func (mr *MockFullDataStoreMockRecorder) AsTerminatableUpload(upload interface{}) *gomock.Call { +// NewUpload indicates an expected call of NewUpload. +func (mr *MockFullDataStoreMockRecorder) NewUpload(ctx, info interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AsTerminatableUpload", reflect.TypeOf((*MockFullDataStore)(nil).AsTerminatableUpload), upload) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewUpload", reflect.TypeOf((*MockFullDataStore)(nil).NewUpload), ctx, info) } -// AsConcatableUpload mocks base method -func (m *MockFullDataStore) AsConcatableUpload(upload handler.Upload) handler.ConcatableUpload { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "AsConcatableUpload", upload) - ret0, _ := ret[0].(handler.ConcatableUpload) - return ret0 -} - -// AsConcatableUpload indicates an expected call of AsConcatableUpload -func (mr *MockFullDataStoreMockRecorder) AsConcatableUpload(upload interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AsConcatableUpload", reflect.TypeOf((*MockFullDataStore)(nil).AsConcatableUpload), upload) -} - -// AsLengthDeclarableUpload mocks base method -func (m *MockFullDataStore) AsLengthDeclarableUpload(upload handler.Upload) handler.LengthDeclarableUpload { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "AsLengthDeclarableUpload", upload) - ret0, _ := ret[0].(handler.LengthDeclarableUpload) - return ret0 -} - -// AsLengthDeclarableUpload indicates an expected call of AsLengthDeclarableUpload -func (mr *MockFullDataStoreMockRecorder) AsLengthDeclarableUpload(upload interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AsLengthDeclarableUpload", reflect.TypeOf((*MockFullDataStore)(nil).AsLengthDeclarableUpload), upload) -} - -// MockFullUpload is a mock of FullUpload interface +// MockFullUpload is a mock of FullUpload interface. type MockFullUpload struct { ctrl *gomock.Controller recorder *MockFullUploadMockRecorder } -// MockFullUploadMockRecorder is the mock recorder for MockFullUpload +// MockFullUploadMockRecorder is the mock recorder for MockFullUpload. type MockFullUploadMockRecorder struct { mock *MockFullUpload } -// NewMockFullUpload creates a new mock instance +// NewMockFullUpload creates a new mock instance. func NewMockFullUpload(ctrl *gomock.Controller) *MockFullUpload { mock := &MockFullUpload{ctrl: ctrl} mock.recorder = &MockFullUploadMockRecorder{mock} return mock } -// EXPECT returns an object that allows the caller to indicate expected use +// EXPECT returns an object that allows the caller to indicate expected use. func (m *MockFullUpload) EXPECT() *MockFullUploadMockRecorder { return m.recorder } -// WriteChunk mocks base method -func (m *MockFullUpload) WriteChunk(ctx context.Context, offset int64, src io.Reader) (int64, error) { +// ConcatUploads mocks base method. +func (m *MockFullUpload) ConcatUploads(ctx context.Context, partialUploads []handler.Upload) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "WriteChunk", ctx, offset, src) - ret0, _ := ret[0].(int64) - ret1, _ := ret[1].(error) - return ret0, ret1 + ret := m.ctrl.Call(m, "ConcatUploads", ctx, partialUploads) + ret0, _ := ret[0].(error) + return ret0 } -// WriteChunk indicates an expected call of WriteChunk -func (mr *MockFullUploadMockRecorder) WriteChunk(ctx, offset, src interface{}) *gomock.Call { +// ConcatUploads indicates an expected call of ConcatUploads. +func (mr *MockFullUploadMockRecorder) ConcatUploads(ctx, partialUploads interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WriteChunk", reflect.TypeOf((*MockFullUpload)(nil).WriteChunk), ctx, offset, src) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ConcatUploads", reflect.TypeOf((*MockFullUpload)(nil).ConcatUploads), ctx, partialUploads) } -// GetInfo mocks base method +// DeclareLength mocks base method. +func (m *MockFullUpload) DeclareLength(ctx context.Context, length int64) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "DeclareLength", ctx, length) + ret0, _ := ret[0].(error) + return ret0 +} + +// DeclareLength indicates an expected call of DeclareLength. +func (mr *MockFullUploadMockRecorder) DeclareLength(ctx, length interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeclareLength", reflect.TypeOf((*MockFullUpload)(nil).DeclareLength), ctx, length) +} + +// FinishUpload mocks base method. +func (m *MockFullUpload) FinishUpload(ctx context.Context) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "FinishUpload", ctx) + ret0, _ := ret[0].(error) + return ret0 +} + +// FinishUpload indicates an expected call of FinishUpload. +func (mr *MockFullUploadMockRecorder) FinishUpload(ctx interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FinishUpload", reflect.TypeOf((*MockFullUpload)(nil).FinishUpload), ctx) +} + +// GetInfo mocks base method. func (m *MockFullUpload) GetInfo(ctx context.Context) (handler.FileInfo, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetInfo", ctx) @@ -154,42 +182,28 @@ func (m *MockFullUpload) GetInfo(ctx context.Context) (handler.FileInfo, error) return ret0, ret1 } -// GetInfo indicates an expected call of GetInfo +// GetInfo indicates an expected call of GetInfo. func (mr *MockFullUploadMockRecorder) GetInfo(ctx interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetInfo", reflect.TypeOf((*MockFullUpload)(nil).GetInfo), ctx) } -// GetReader mocks base method -func (m *MockFullUpload) GetReader(ctx context.Context) (io.Reader, error) { +// GetReader mocks base method. +func (m *MockFullUpload) GetReader(ctx context.Context) (io.ReadCloser, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "GetReader", ctx) - ret0, _ := ret[0].(io.Reader) + ret0, _ := ret[0].(io.ReadCloser) ret1, _ := ret[1].(error) return ret0, ret1 } -// GetReader indicates an expected call of GetReader +// GetReader indicates an expected call of GetReader. func (mr *MockFullUploadMockRecorder) GetReader(ctx interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetReader", reflect.TypeOf((*MockFullUpload)(nil).GetReader), ctx) } -// FinishUpload mocks base method -func (m *MockFullUpload) FinishUpload(ctx context.Context) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "FinishUpload", ctx) - ret0, _ := ret[0].(error) - return ret0 -} - -// FinishUpload indicates an expected call of FinishUpload -func (mr *MockFullUploadMockRecorder) FinishUpload(ctx interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FinishUpload", reflect.TypeOf((*MockFullUpload)(nil).FinishUpload), ctx) -} - -// Terminate mocks base method +// Terminate mocks base method. func (m *MockFullUpload) Terminate(ctx context.Context) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Terminate", ctx) @@ -197,64 +211,51 @@ func (m *MockFullUpload) Terminate(ctx context.Context) error { return ret0 } -// Terminate indicates an expected call of Terminate +// Terminate indicates an expected call of Terminate. func (mr *MockFullUploadMockRecorder) Terminate(ctx interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Terminate", reflect.TypeOf((*MockFullUpload)(nil).Terminate), ctx) } -// DeclareLength mocks base method -func (m *MockFullUpload) DeclareLength(ctx context.Context, length int64) error { +// WriteChunk mocks base method. +func (m *MockFullUpload) WriteChunk(ctx context.Context, offset int64, src io.Reader) (int64, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "DeclareLength", ctx, length) - ret0, _ := ret[0].(error) - return ret0 + ret := m.ctrl.Call(m, "WriteChunk", ctx, offset, src) + ret0, _ := ret[0].(int64) + ret1, _ := ret[1].(error) + return ret0, ret1 } -// DeclareLength indicates an expected call of DeclareLength -func (mr *MockFullUploadMockRecorder) DeclareLength(ctx, length interface{}) *gomock.Call { +// WriteChunk indicates an expected call of WriteChunk. +func (mr *MockFullUploadMockRecorder) WriteChunk(ctx, offset, src interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeclareLength", reflect.TypeOf((*MockFullUpload)(nil).DeclareLength), ctx, length) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WriteChunk", reflect.TypeOf((*MockFullUpload)(nil).WriteChunk), ctx, offset, src) } -// ConcatUploads mocks base method -func (m *MockFullUpload) ConcatUploads(ctx context.Context, partialUploads []handler.Upload) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ConcatUploads", ctx, partialUploads) - ret0, _ := ret[0].(error) - return ret0 -} - -// ConcatUploads indicates an expected call of ConcatUploads -func (mr *MockFullUploadMockRecorder) ConcatUploads(ctx, partialUploads interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ConcatUploads", reflect.TypeOf((*MockFullUpload)(nil).ConcatUploads), ctx, partialUploads) -} - -// MockFullLocker is a mock of FullLocker interface +// MockFullLocker is a mock of FullLocker interface. type MockFullLocker struct { ctrl *gomock.Controller recorder *MockFullLockerMockRecorder } -// MockFullLockerMockRecorder is the mock recorder for MockFullLocker +// MockFullLockerMockRecorder is the mock recorder for MockFullLocker. type MockFullLockerMockRecorder struct { mock *MockFullLocker } -// NewMockFullLocker creates a new mock instance +// NewMockFullLocker creates a new mock instance. func NewMockFullLocker(ctrl *gomock.Controller) *MockFullLocker { mock := &MockFullLocker{ctrl: ctrl} mock.recorder = &MockFullLockerMockRecorder{mock} return mock } -// EXPECT returns an object that allows the caller to indicate expected use +// EXPECT returns an object that allows the caller to indicate expected use. func (m *MockFullLocker) EXPECT() *MockFullLockerMockRecorder { return m.recorder } -// NewLock mocks base method +// NewLock mocks base method. func (m *MockFullLocker) NewLock(id string) (handler.Lock, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "NewLock", id) @@ -263,36 +264,36 @@ func (m *MockFullLocker) NewLock(id string) (handler.Lock, error) { return ret0, ret1 } -// NewLock indicates an expected call of NewLock +// NewLock indicates an expected call of NewLock. func (mr *MockFullLockerMockRecorder) NewLock(id interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewLock", reflect.TypeOf((*MockFullLocker)(nil).NewLock), id) } -// MockFullLock is a mock of FullLock interface +// MockFullLock is a mock of FullLock interface. type MockFullLock struct { ctrl *gomock.Controller recorder *MockFullLockMockRecorder } -// MockFullLockMockRecorder is the mock recorder for MockFullLock +// MockFullLockMockRecorder is the mock recorder for MockFullLock. type MockFullLockMockRecorder struct { mock *MockFullLock } -// NewMockFullLock creates a new mock instance +// NewMockFullLock creates a new mock instance. func NewMockFullLock(ctrl *gomock.Controller) *MockFullLock { mock := &MockFullLock{ctrl: ctrl} mock.recorder = &MockFullLockMockRecorder{mock} return mock } -// EXPECT returns an object that allows the caller to indicate expected use +// EXPECT returns an object that allows the caller to indicate expected use. func (m *MockFullLock) EXPECT() *MockFullLockMockRecorder { return m.recorder } -// Lock mocks base method +// Lock mocks base method. func (m *MockFullLock) Lock(ctx context.Context, requestUnlock func()) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Lock", ctx, requestUnlock) @@ -300,13 +301,13 @@ func (m *MockFullLock) Lock(ctx context.Context, requestUnlock func()) error { return ret0 } -// Lock indicates an expected call of Lock +// Lock indicates an expected call of Lock. func (mr *MockFullLockMockRecorder) Lock(ctx, requestUnlock interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Lock", reflect.TypeOf((*MockFullLock)(nil).Lock), ctx, requestUnlock) } -// Unlock mocks base method +// Unlock mocks base method. func (m *MockFullLock) Unlock() error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Unlock") @@ -314,7 +315,7 @@ func (m *MockFullLock) Unlock() error { return ret0 } -// Unlock indicates an expected call of Unlock +// Unlock indicates an expected call of Unlock. func (mr *MockFullLockMockRecorder) Unlock() *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Unlock", reflect.TypeOf((*MockFullLock)(nil).Unlock)) diff --git a/pkg/handler/unrouted_handler.go b/pkg/handler/unrouted_handler.go index 0f1dbab..be44c50 100644 --- a/pkg/handler/unrouted_handler.go +++ b/pkg/handler/unrouted_handler.go @@ -749,10 +749,7 @@ func (handler *UnroutedHandler) GetFile(w http.ResponseWriter, r *http.Request) handler.sendResp(c, resp) io.Copy(w, src) - // Try to close the reader if the io.Closer interface is implemented - if closer, ok := src.(io.Closer); ok { - closer.Close() - } + src.Close() } // mimeInlineBrowserWhitelist is a map containing MIME types which should be diff --git a/pkg/handler/utils_test.go b/pkg/handler/utils_test.go index c2e6356..8c4ed35 100644 --- a/pkg/handler/utils_test.go +++ b/pkg/handler/utils_test.go @@ -13,7 +13,7 @@ import ( "github.com/tus/tusd/pkg/handler" ) -//go:generate mockgen -package handler_test -source utils_test.go -aux_files handler=datastore.go -destination=handler_mock_test.go +//go:generate mockgen -package handler_test -source utils_test.go -destination=handler_mock_test.go // FullDataStore is an interface combining most interfaces for data stores. // This is used by mockgen(1) to generate a mocked data store used for testing diff --git a/pkg/s3store/s3store.go b/pkg/s3store/s3store.go index 9ddb517..4988194 100644 --- a/pkg/s3store/s3store.go +++ b/pkg/s3store/s3store.go @@ -686,7 +686,7 @@ func (upload s3Upload) fetchInfo(ctx context.Context) (info handler.FileInfo, pa return info, parts, incompletePartSize, nil } -func (upload s3Upload) GetReader(ctx context.Context) (io.Reader, error) { +func (upload s3Upload) GetReader(ctx context.Context) (io.ReadCloser, error) { id := upload.id store := upload.store uploadId, multipartId := splitIds(id) diff --git a/pkg/s3store/s3store_mock_test.go b/pkg/s3store/s3store_mock_test.go index 3d2af90..b99c0f2 100644 --- a/pkg/s3store/s3store_mock_test.go +++ b/pkg/s3store/s3store_mock_test.go @@ -13,30 +13,30 @@ import ( gomock "github.com/golang/mock/gomock" ) -// MockS3API is a mock of S3API interface +// MockS3API is a mock of S3API interface. type MockS3API struct { ctrl *gomock.Controller recorder *MockS3APIMockRecorder } -// MockS3APIMockRecorder is the mock recorder for MockS3API +// MockS3APIMockRecorder is the mock recorder for MockS3API. type MockS3APIMockRecorder struct { mock *MockS3API } -// NewMockS3API creates a new mock instance +// NewMockS3API creates a new mock instance. func NewMockS3API(ctrl *gomock.Controller) *MockS3API { mock := &MockS3API{ctrl: ctrl} mock.recorder = &MockS3APIMockRecorder{mock} return mock } -// EXPECT returns an object that allows the caller to indicate expected use +// EXPECT returns an object that allows the caller to indicate expected use. func (m *MockS3API) EXPECT() *MockS3APIMockRecorder { return m.recorder } -// AbortMultipartUploadWithContext mocks base method +// AbortMultipartUploadWithContext mocks base method. func (m *MockS3API) AbortMultipartUploadWithContext(arg0 context.Context, arg1 *s3.AbortMultipartUploadInput, arg2 ...request.Option) (*s3.AbortMultipartUploadOutput, error) { m.ctrl.T.Helper() varargs := []interface{}{arg0, arg1} @@ -49,14 +49,14 @@ func (m *MockS3API) AbortMultipartUploadWithContext(arg0 context.Context, arg1 * return ret0, ret1 } -// AbortMultipartUploadWithContext indicates an expected call of AbortMultipartUploadWithContext +// AbortMultipartUploadWithContext indicates an expected call of AbortMultipartUploadWithContext. func (mr *MockS3APIMockRecorder) AbortMultipartUploadWithContext(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() varargs := append([]interface{}{arg0, arg1}, arg2...) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AbortMultipartUploadWithContext", reflect.TypeOf((*MockS3API)(nil).AbortMultipartUploadWithContext), varargs...) } -// CompleteMultipartUploadWithContext mocks base method +// CompleteMultipartUploadWithContext mocks base method. func (m *MockS3API) CompleteMultipartUploadWithContext(arg0 context.Context, arg1 *s3.CompleteMultipartUploadInput, arg2 ...request.Option) (*s3.CompleteMultipartUploadOutput, error) { m.ctrl.T.Helper() varargs := []interface{}{arg0, arg1} @@ -69,14 +69,14 @@ func (m *MockS3API) CompleteMultipartUploadWithContext(arg0 context.Context, arg return ret0, ret1 } -// CompleteMultipartUploadWithContext indicates an expected call of CompleteMultipartUploadWithContext +// CompleteMultipartUploadWithContext indicates an expected call of CompleteMultipartUploadWithContext. func (mr *MockS3APIMockRecorder) CompleteMultipartUploadWithContext(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() varargs := append([]interface{}{arg0, arg1}, arg2...) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CompleteMultipartUploadWithContext", reflect.TypeOf((*MockS3API)(nil).CompleteMultipartUploadWithContext), varargs...) } -// CreateMultipartUploadWithContext mocks base method +// CreateMultipartUploadWithContext mocks base method. func (m *MockS3API) CreateMultipartUploadWithContext(arg0 context.Context, arg1 *s3.CreateMultipartUploadInput, arg2 ...request.Option) (*s3.CreateMultipartUploadOutput, error) { m.ctrl.T.Helper() varargs := []interface{}{arg0, arg1} @@ -89,14 +89,14 @@ func (m *MockS3API) CreateMultipartUploadWithContext(arg0 context.Context, arg1 return ret0, ret1 } -// CreateMultipartUploadWithContext indicates an expected call of CreateMultipartUploadWithContext +// CreateMultipartUploadWithContext indicates an expected call of CreateMultipartUploadWithContext. func (mr *MockS3APIMockRecorder) CreateMultipartUploadWithContext(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() varargs := append([]interface{}{arg0, arg1}, arg2...) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateMultipartUploadWithContext", reflect.TypeOf((*MockS3API)(nil).CreateMultipartUploadWithContext), varargs...) } -// DeleteObjectWithContext mocks base method +// DeleteObjectWithContext mocks base method. func (m *MockS3API) DeleteObjectWithContext(arg0 context.Context, arg1 *s3.DeleteObjectInput, arg2 ...request.Option) (*s3.DeleteObjectOutput, error) { m.ctrl.T.Helper() varargs := []interface{}{arg0, arg1} @@ -109,14 +109,14 @@ func (m *MockS3API) DeleteObjectWithContext(arg0 context.Context, arg1 *s3.Delet return ret0, ret1 } -// DeleteObjectWithContext indicates an expected call of DeleteObjectWithContext +// DeleteObjectWithContext indicates an expected call of DeleteObjectWithContext. func (mr *MockS3APIMockRecorder) DeleteObjectWithContext(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() varargs := append([]interface{}{arg0, arg1}, arg2...) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteObjectWithContext", reflect.TypeOf((*MockS3API)(nil).DeleteObjectWithContext), varargs...) } -// DeleteObjectsWithContext mocks base method +// DeleteObjectsWithContext mocks base method. func (m *MockS3API) DeleteObjectsWithContext(arg0 context.Context, arg1 *s3.DeleteObjectsInput, arg2 ...request.Option) (*s3.DeleteObjectsOutput, error) { m.ctrl.T.Helper() varargs := []interface{}{arg0, arg1} @@ -129,14 +129,14 @@ func (m *MockS3API) DeleteObjectsWithContext(arg0 context.Context, arg1 *s3.Dele return ret0, ret1 } -// DeleteObjectsWithContext indicates an expected call of DeleteObjectsWithContext +// DeleteObjectsWithContext indicates an expected call of DeleteObjectsWithContext. func (mr *MockS3APIMockRecorder) DeleteObjectsWithContext(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() varargs := append([]interface{}{arg0, arg1}, arg2...) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteObjectsWithContext", reflect.TypeOf((*MockS3API)(nil).DeleteObjectsWithContext), varargs...) } -// GetObjectWithContext mocks base method +// GetObjectWithContext mocks base method. func (m *MockS3API) GetObjectWithContext(arg0 context.Context, arg1 *s3.GetObjectInput, arg2 ...request.Option) (*s3.GetObjectOutput, error) { m.ctrl.T.Helper() varargs := []interface{}{arg0, arg1} @@ -149,14 +149,14 @@ func (m *MockS3API) GetObjectWithContext(arg0 context.Context, arg1 *s3.GetObjec return ret0, ret1 } -// GetObjectWithContext indicates an expected call of GetObjectWithContext +// GetObjectWithContext indicates an expected call of GetObjectWithContext. func (mr *MockS3APIMockRecorder) GetObjectWithContext(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() varargs := append([]interface{}{arg0, arg1}, arg2...) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetObjectWithContext", reflect.TypeOf((*MockS3API)(nil).GetObjectWithContext), varargs...) } -// HeadObjectWithContext mocks base method +// HeadObjectWithContext mocks base method. func (m *MockS3API) HeadObjectWithContext(arg0 context.Context, arg1 *s3.HeadObjectInput, arg2 ...request.Option) (*s3.HeadObjectOutput, error) { m.ctrl.T.Helper() varargs := []interface{}{arg0, arg1} @@ -169,14 +169,14 @@ func (m *MockS3API) HeadObjectWithContext(arg0 context.Context, arg1 *s3.HeadObj return ret0, ret1 } -// HeadObjectWithContext indicates an expected call of HeadObjectWithContext +// HeadObjectWithContext indicates an expected call of HeadObjectWithContext. func (mr *MockS3APIMockRecorder) HeadObjectWithContext(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() varargs := append([]interface{}{arg0, arg1}, arg2...) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "HeadObjectWithContext", reflect.TypeOf((*MockS3API)(nil).HeadObjectWithContext), varargs...) } -// ListPartsWithContext mocks base method +// ListPartsWithContext mocks base method. func (m *MockS3API) ListPartsWithContext(arg0 context.Context, arg1 *s3.ListPartsInput, arg2 ...request.Option) (*s3.ListPartsOutput, error) { m.ctrl.T.Helper() varargs := []interface{}{arg0, arg1} @@ -189,14 +189,14 @@ func (m *MockS3API) ListPartsWithContext(arg0 context.Context, arg1 *s3.ListPart return ret0, ret1 } -// ListPartsWithContext indicates an expected call of ListPartsWithContext +// ListPartsWithContext indicates an expected call of ListPartsWithContext. func (mr *MockS3APIMockRecorder) ListPartsWithContext(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() varargs := append([]interface{}{arg0, arg1}, arg2...) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListPartsWithContext", reflect.TypeOf((*MockS3API)(nil).ListPartsWithContext), varargs...) } -// PutObjectWithContext mocks base method +// PutObjectWithContext mocks base method. func (m *MockS3API) PutObjectWithContext(arg0 context.Context, arg1 *s3.PutObjectInput, arg2 ...request.Option) (*s3.PutObjectOutput, error) { m.ctrl.T.Helper() varargs := []interface{}{arg0, arg1} @@ -209,14 +209,14 @@ func (m *MockS3API) PutObjectWithContext(arg0 context.Context, arg1 *s3.PutObjec return ret0, ret1 } -// PutObjectWithContext indicates an expected call of PutObjectWithContext +// PutObjectWithContext indicates an expected call of PutObjectWithContext. func (mr *MockS3APIMockRecorder) PutObjectWithContext(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() varargs := append([]interface{}{arg0, arg1}, arg2...) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PutObjectWithContext", reflect.TypeOf((*MockS3API)(nil).PutObjectWithContext), varargs...) } -// UploadPartCopyWithContext mocks base method +// UploadPartCopyWithContext mocks base method. func (m *MockS3API) UploadPartCopyWithContext(arg0 context.Context, arg1 *s3.UploadPartCopyInput, arg2 ...request.Option) (*s3.UploadPartCopyOutput, error) { m.ctrl.T.Helper() varargs := []interface{}{arg0, arg1} @@ -229,14 +229,14 @@ func (m *MockS3API) UploadPartCopyWithContext(arg0 context.Context, arg1 *s3.Upl return ret0, ret1 } -// UploadPartCopyWithContext indicates an expected call of UploadPartCopyWithContext +// UploadPartCopyWithContext indicates an expected call of UploadPartCopyWithContext. func (mr *MockS3APIMockRecorder) UploadPartCopyWithContext(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() varargs := append([]interface{}{arg0, arg1}, arg2...) return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "UploadPartCopyWithContext", reflect.TypeOf((*MockS3API)(nil).UploadPartCopyWithContext), varargs...) } -// UploadPartWithContext mocks base method +// UploadPartWithContext mocks base method. func (m *MockS3API) UploadPartWithContext(arg0 context.Context, arg1 *s3.UploadPartInput, arg2 ...request.Option) (*s3.UploadPartOutput, error) { m.ctrl.T.Helper() varargs := []interface{}{arg0, arg1} @@ -249,7 +249,7 @@ func (m *MockS3API) UploadPartWithContext(arg0 context.Context, arg1 *s3.UploadP return ret0, ret1 } -// UploadPartWithContext indicates an expected call of UploadPartWithContext +// UploadPartWithContext indicates an expected call of UploadPartWithContext. func (mr *MockS3APIMockRecorder) UploadPartWithContext(arg0, arg1 interface{}, arg2 ...interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() varargs := append([]interface{}{arg0, arg1}, arg2...)