Skip to content

Commit

Permalink
Fix upload function
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Sep 7, 2021
1 parent 8b89bab commit 5d49308
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
22 changes: 9 additions & 13 deletions pkg/storage/fs/nextcloud/nextcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions pkg/storage/fs/nextcloud/nextcloud_server_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
29 changes: 28 additions & 1 deletion pkg/storage/fs/nextcloud/nextcloud_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ package nextcloud_test
import (
"context"
"encoding/json"
// "net/http"
"io"
"os"
"strings"

"google.golang.org/grpc/metadata"

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 5d49308

Please sign in to comment.