Skip to content

Allows to disable making parent collection while writing stream #76

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
22 changes: 13 additions & 9 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ func (c *Client) ReadStreamRange(path string, offset, length int64) (io.ReadClos
}

// Write writes data to a given path
func (c *Client) Write(path string, data []byte, _ os.FileMode) (err error) {
func (c *Client) Write(path string, data []byte, _ os.FileMode, doParents ...bool) (err error) {
s, err := c.put(path, bytes.NewReader(data))
if err != nil {
return
Expand All @@ -398,7 +398,7 @@ func (c *Client) Write(path string, data []byte, _ os.FileMode) (err error) {
return nil

case 404, 409:
err = c.createParentCollection(path)
err = c.createParentCollection(path, c.parseParentOption(doParents...))
if err != nil {
return
}
Expand All @@ -416,23 +416,27 @@ func (c *Client) Write(path string, data []byte, _ os.FileMode) (err error) {
}

// WriteStream writes a stream
func (c *Client) WriteStream(path string, stream io.Reader, _ os.FileMode) (err error) {

err = c.createParentCollection(path)
if err != nil {
func (c *Client) WriteStream(path string, stream io.Reader, _ os.FileMode, doParents ...bool) (err error) {
if err := c.createParentCollection(path, c.parseParentOption(doParents...)); err != nil {
return err
}

s, err := c.put(path, stream)
if err != nil {
return err
}

switch s {
case 200, 201, 204:
return nil

default:
return NewPathError("WriteStream", path, s)
}
}

// parseParentOption parses variadic bool argument. If present, first element is returned.
func (c *Client) parseParentOption(b ...bool) bool {
create := true
if len(b) > 0 {
create = b[0]
}
return create
}
31 changes: 31 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,22 @@ func TestWriteStream(t *testing.T) {
if info, err := fs.Stat(ctx, "/404/works.txt"); err != nil {
t.Fatalf("got: %v, want file info: %v", err, info)
}

if err := cli.WriteStream("/test/newFile.txt", strings.NewReader("foo bar\n"), 0660, false); err != nil {
t.Fatalf("got: %v, want nil", err)
}

if info, err := fs.Stat(ctx, "/test/newFile.txt"); err != nil {
t.Fatalf("got: %v, want file info: %v", err, info)
}

if err := cli.WriteStream("/test/fld1/fld2/newFile.txt", strings.NewReader("foo bar\n"), 0660, true); err != nil {
t.Fatalf("got: %v, want nil", err)
}

if info, err := fs.Stat(ctx, "/test/fld1/fld2/newFile.txt"); err != nil {
t.Fatalf("got: %v, want file info: %v", err, info)
}
}

func TestWriteStreamFromPipe(t *testing.T) {
Expand Down Expand Up @@ -572,3 +588,18 @@ func TestWriteStreamFromPipe(t *testing.T) {
t.Fatalf("got: %v, want file size: %d bytes", info.Size(), 8)
}
}

func TestClient_createParentCollection(t *testing.T) {
cli, srv, _, _ := newServer(t)
defer srv.Close()

err := cli.createParentCollection("/test/folder1/folder2/")
if err != nil {
t.Fatalf("got: %v, want no err", err)
}

err = cli.createParentCollection("/some/folder/", false)
if err != nil {
t.Fatalf("got: %v, want no err", err)
}
}
9 changes: 6 additions & 3 deletions requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (c *Client) doCopyMove(
return
}

func (c *Client) copymove(method string, oldpath string, newpath string, overwrite bool) (err error) {
func (c *Client) copymove(method string, oldpath string, newpath string, overwrite bool, doParents ...bool) (err error) {
s, data, err := c.doCopyMove(method, oldpath, newpath, overwrite)
if err != nil {
return
Expand All @@ -149,7 +149,7 @@ func (c *Client) copymove(method string, oldpath string, newpath string, overwri
log.Printf("TODO handle %s - %s multistatus result %s\n", method, oldpath, String(data))

case 409:
err := c.createParentCollection(newpath)
err := c.createParentCollection(newpath, c.parseParentOption(doParents...))
if err != nil {
return err
}
Expand All @@ -171,7 +171,10 @@ func (c *Client) put(path string, stream io.Reader) (status int, err error) {
return
}

func (c *Client) createParentCollection(itemPath string) (err error) {
func (c *Client) createParentCollection(itemPath string, doParents ...bool) (err error) {
if len(doParents) > 0 && !doParents[0] {
return nil
}
parentPath := path.Dir(itemPath)
if parentPath == "." || parentPath == "/" {
return nil
Expand Down