From 68824ef55e3668c74e3c2c6c4122e2b2fa23c8c4 Mon Sep 17 00:00:00 2001 From: vitalii Date: Wed, 20 Jun 2018 06:51:06 +0300 Subject: [PATCH] createParentCollection() function was added --- client.go | 12 +++++------- requests.go | 7 +++++++ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/client.go b/client.go index c91529f..dcf88a7 100644 --- a/client.go +++ b/client.go @@ -346,12 +346,10 @@ func (c *Client) Write(path string, data []byte, _ os.FileMode) error { return nil case 409: - if i := strings.LastIndex(path, "/"); i > -1 { - if err := c.MkdirAll(path[0:i+1], 0755); err == nil { - s = c.put(path, bytes.NewReader(data)) - if s == 200 || s == 201 || s == 204 { - return nil - } + if err := c.createParentCollection(path); err == nil { + s = c.put(path, bytes.NewReader(data)) + if s == 200 || s == 201 || s == 204 { + return nil } } } @@ -361,7 +359,7 @@ func (c *Client) Write(path string, data []byte, _ os.FileMode) error { // WriteStream writes a stream func (c *Client) WriteStream(path string, stream io.Reader, _ os.FileMode) error { - // TODO check if parent collection exists + c.createParentCollection(path) s := c.put(path, stream) switch s { case 200, 201, 204: diff --git a/requests.go b/requests.go index 58ba034..35fe5f5 100644 --- a/requests.go +++ b/requests.go @@ -7,6 +7,7 @@ import ( "net/http" "strings" "errors" + "path/filepath" ) func (c *Client) req(method, path string, body io.Reader, intercept func(*http.Request)) (req *http.Response, err error) { @@ -138,3 +139,9 @@ func (c *Client) put(path string, stream io.Reader) int { return rs.StatusCode } + + +func (c *Client) createParentCollection(itemPath string) (err error) { + parentPath := filepath.Dir(itemPath) + return c.MkdirAll(parentPath, 0755) +}