Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix decomposedfs upload #2330

Merged
merged 1 commit into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/unreleased/fix-decomposedfs-upload.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Bugfix: fix decomposedfs upload

The FS.Upload() implementation needs to handle direct uploads that did not initiate a unpload.

https://github.com/cs3org/reva/pull/2330
6 changes: 1 addition & 5 deletions pkg/rhttp/datatx/manager/spaces/spaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ package spaces

import (
"net/http"
"path"
"strings"

provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
"github.com/cs3org/reva/pkg/appctx"
Expand Down Expand Up @@ -78,10 +76,8 @@ func (m *manager) Handler(fs storage.FS) (http.Handler, error) {
case "GET", "HEAD":
download.GetOrHeadFile(w, r, fs, spaceID)
case "PUT":
// make a clean relative path
fn := path.Clean(strings.TrimLeft(r.URL.Path, "/"))
fn := utils.MakeRelativePath(r.URL.Path)
defer r.Body.Close()

// TODO refactor: pass Reference to Upload & GetOrHeadFile
// build a storage space reference
storageid, opaqeid := utils.SplitStorageSpaceID(spaceID)
Expand Down
14 changes: 13 additions & 1 deletion pkg/storage/utils/decomposedfs/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,19 @@ var defaultFilePerm = os.FileMode(0664)
func (fs *Decomposedfs) Upload(ctx context.Context, ref *provider.Reference, r io.ReadCloser) (err error) {
upload, err := fs.GetUpload(ctx, ref.GetPath())
if err != nil {
return errors.Wrap(err, "Decomposedfs: error retrieving upload")
// Upload corresponding to this ID was not found.
// Assume that this corresponds to the resource path to which the file has to be uploaded.

// Set the length to 0 and set SizeIsDeferred to true
metadata := map[string]string{"sizedeferred": "true"}
uploadIDs, err := fs.InitiateUpload(ctx, ref, 0, metadata)
if err != nil {
return err
}
upload, err = fs.GetUpload(ctx, uploadIDs["simple"])
if err != nil {
return errors.Wrap(err, "Decomposedfs: error retrieving upload")
}
}

uploadInfo := upload.(*fileUpload)
Expand Down
33 changes: 32 additions & 1 deletion pkg/storage/utils/decomposedfs/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ var _ = Describe("File uploads", func() {
Expect(err).ToNot(HaveOccurred())
Expect(resp.Status.Code).To(Equal(v1beta11.Code_CODE_OK))
spaceID = resp.StorageSpace.Id.OpaqueId
ref.ResourceId = &provider.ResourceId{StorageId: spaceID}
ref.ResourceId = &provider.ResourceId{StorageId: spaceID, OpaqueId: spaceID}
})

Context("the user's quota is exceeded", func() {
Expand Down Expand Up @@ -308,5 +308,36 @@ var _ = Describe("File uploads", func() {
})
})

When("the user directly uploads a file with a relative reference", func() {
It("succeeds", func() {
var (
fileContent = []byte("0123456789")
)

uploadRef := &provider.Reference{ResourceId: ref.ResourceId, Path: "./newfile.txt"}

bs.On("Upload", mock.AnythingOfType("string"), mock.AnythingOfType("*os.File")).
Return(nil).
Run(func(args mock.Arguments) {
reader := args.Get(1).(io.Reader)
data, err := ioutil.ReadAll(reader)

Expect(err).ToNot(HaveOccurred())
Expect(data).To(Equal(fileContent))
})

err := fs.Upload(ctx, uploadRef, ioutil.NopCloser(bytes.NewReader(fileContent)))

Expect(err).ToNot(HaveOccurred())
bs.AssertCalled(GinkgoT(), "Upload", mock.Anything, mock.Anything)

relativeListRef := &provider.Reference{ResourceId: ref.ResourceId, Path: "."}
resources, err := fs.ListFolder(ctx, relativeListRef, []string{})
Expect(err).ToNot(HaveOccurred())
Expect(len(resources)).To(Equal(2)) // .space folder + newfile.txt
Expect(resources[0].Path).To(Or(Equal("newfile.txt"), Equal(".space")))
Expect(resources[1].Path).To(Or(Equal("newfile.txt"), Equal(".space")))
})
})
})
})