Allows to disable making parent collection in createParentCollection
This commit is contained in:
parent
f9157dbec1
commit
4852177de4
22
client.go
22
client.go
@ -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
|
||||
@ -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
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
|
@ -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) {
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
@ -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
|
||||
}
|
||||
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user