From 5d493089d7a347cdb5f83855ce60d0b5494c05e7 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 7 Sep 2021 14:44:01 +0000 Subject: [PATCH] Fix upload function --- pkg/storage/fs/nextcloud/nextcloud.go | 22 ++++++-------- .../fs/nextcloud/nextcloud_server_mock.go | 3 ++ pkg/storage/fs/nextcloud/nextcloud_test.go | 29 ++++++++++++++++++- 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/pkg/storage/fs/nextcloud/nextcloud.go b/pkg/storage/fs/nextcloud/nextcloud.go index 772026dda7..3e90adeb10 100644 --- a/pkg/storage/fs/nextcloud/nextcloud.go +++ b/pkg/storage/fs/nextcloud/nextcloud.go @@ -112,12 +112,13 @@ func (nc *StorageDriver) SetHTTPClient(c *http.Client) { nc.client = c } -func (nc *StorageDriver) doUpload(r io.ReadCloser) error { - filePath := "test.txt" - - // initialize http client - client := &http.Client{} - url := nc.endPoint + "Upload/" + filePath +func (nc *StorageDriver) doUpload(ctx context.Context, filePath string, r io.ReadCloser) error { + // log := appctx.GetLogger(ctx) + user, err := getUser(ctx) + if err != nil { + return err + } + url := nc.endPoint + "~" + user.Username + "/api/Upload/" + filePath req, err := http.NewRequest(http.MethodPut, url, r) if err != nil { panic(err) @@ -126,7 +127,7 @@ func (nc *StorageDriver) doUpload(r io.ReadCloser) error { // set the request header Content-Type for the upload // FIXME: get the actual content type from somewhere req.Header.Set("Content-Type", "text/plain") - resp, err := client.Do(req) + resp, err := nc.client.Do(req) if err != nil { panic(err) } @@ -373,12 +374,7 @@ func (nc *StorageDriver) Upload(ctx context.Context, ref *provider.Reference, r log := appctx.GetLogger(ctx) log.Info().Msgf("Upload %s", bodyStr) - err := nc.doUpload(r) - if err != nil { - return err - } - _, _, err = nc.do(ctx, Action{"Upload", string(bodyStr)}) - return err + return nc.doUpload(ctx, ref.Path, r) } // Download as defined in the storage.FS interface diff --git a/pkg/storage/fs/nextcloud/nextcloud_server_mock.go b/pkg/storage/fs/nextcloud/nextcloud_server_mock.go index f86a9be7a1..1f7cd1793b 100644 --- a/pkg/storage/fs/nextcloud/nextcloud_server_mock.go +++ b/pkg/storage/fs/nextcloud/nextcloud_server_mock.go @@ -141,8 +141,11 @@ var responses = map[string]Response{ `POST /apps/sciencemesh/~tester/api/GetMD {"ref":{"resource_id":{"storage_id":"storage-id","opaque_id":"opaque-id"},"path":"/some/path"},"mdKeys":["val1","val2","val3"]}`: {200, `{ "size": 1, "path":"/some/path", "metadata": { "foo": "bar" }, "etag": "in-json-etag", "mimetype": "in-json-mimetype" }`, serverStateEmpty}, `POST /apps/sciencemesh/~tester/api/ListFolder {"ref":{"resource_id":{"storage_id":"storage-id","opaque_id":"opaque-id"},"path":"/some/path"},"mdKeys":["val1","val2","val3"]}`: {200, `[{ "size": 1, "path":"/some/path", "metadata": { "foo": "bar" }, "etag": "in-json-etag", "mimetype": "in-json-mimetype" }]`, serverStateEmpty}, `POST /apps/sciencemesh/~tester/api/InitiateUpload {"ref":{"resource_id":{"storage_id":"storage-id","opaque_id":"opaque-id"},"path":"/some/path"},"uploadLength":12345,"metadata":{"key1":"val1","key2":"val2","key3":"val3"}}`: {200, `{ "not":"sure", "what": "should be", "returned": "here" }`, serverStateEmpty}, + `POST /apps/sciencemesh/~tester/api/Upload {"resource_id":{"storage_id":"storage-id","opaque_id":"opaque-id"},"path":"/some/path"}`: {200, ``, serverStateEmpty}, + `PUT /apps/sciencemesh/~tester/api/Upload/some/file/path.txt shiny!`: {200, ``, serverStateEmpty}, } + // GetNextcloudServerMock returns a handler that pretends to be a remote Nextcloud server func GetNextcloudServerMock(called *[]string) (http.Handler) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { diff --git a/pkg/storage/fs/nextcloud/nextcloud_test.go b/pkg/storage/fs/nextcloud/nextcloud_test.go index 0d4d7d8863..03b9d9b00d 100644 --- a/pkg/storage/fs/nextcloud/nextcloud_test.go +++ b/pkg/storage/fs/nextcloud/nextcloud_test.go @@ -21,8 +21,9 @@ package nextcloud_test import ( "context" "encoding/json" - // "net/http" + "io" "os" + "strings" "google.golang.org/grpc/metadata" @@ -321,6 +322,32 @@ var _ = Describe("Nextcloud", func() { }) // Upload(ctx context.Context, ref *provider.Reference, r io.ReadCloser) error + Describe("Upload", func() { + It("calls the files API with PUT", func() { + nc, _ := nextcloud.NewStorageDriver(&nextcloud.StorageDriverConfig{ + EndPoint: "http://mock.com/apps/sciencemesh/", + MockHTTP: true, + }) + called := make([]string, 0) + h := nextcloud.GetNextcloudServerMock(&called) + mock, teardown := nextcloud.TestingHTTPClient(h) + defer teardown() + nc.SetHTTPClient(mock) + // https://github.com/cs3org/go-cs3apis/blob/970eec3/cs3/storage/provider/v1beta1/resources.pb.go#L550-L561 + ref := &provider.Reference{ + ResourceId: &provider.ResourceId{ + StorageId: "storage-id", + OpaqueId: "opaque-id", + }, + Path: "some/file/path.txt", + } + stringReader := strings.NewReader("shiny!") + stringReadCloser := io.NopCloser(stringReader) + err := nc.Upload(ctx, ref, stringReadCloser) + Expect(err).ToNot(HaveOccurred()) + Expect(called[0]).To(Equal("PUT /apps/sciencemesh/~tester/api/Upload/some/file/path.txt shiny!")) + }) + }) // Download(ctx context.Context, ref *provider.Reference) (io.ReadCloser, error) // ListRevisions(ctx context.Context, ref *provider.Reference) ([]*provider.FileVersion, error) // DownloadRevision(ctx context.Context, ref *provider.Reference, key string) (io.ReadCloser, error)