createParentCollection() function was added

This commit is contained in:
vitalii 2018-06-20 06:51:06 +03:00
parent 6ca20e2a70
commit 68824ef55e
2 changed files with 12 additions and 7 deletions

View File

@ -346,22 +346,20 @@ func (c *Client) Write(path string, data []byte, _ os.FileMode) error {
return nil return nil
case 409: case 409:
if i := strings.LastIndex(path, "/"); i > -1 { if err := c.createParentCollection(path); err == nil {
if err := c.MkdirAll(path[0:i+1], 0755); err == nil {
s = c.put(path, bytes.NewReader(data)) s = c.put(path, bytes.NewReader(data))
if s == 200 || s == 201 || s == 204 { if s == 200 || s == 201 || s == 204 {
return nil return nil
} }
} }
} }
}
return newPathError("Write", path, s) return newPathError("Write", path, s)
} }
// WriteStream writes a stream // WriteStream writes a stream
func (c *Client) WriteStream(path string, stream io.Reader, _ os.FileMode) error { 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) s := c.put(path, stream)
switch s { switch s {
case 200, 201, 204: case 200, 201, 204:

View File

@ -7,6 +7,7 @@ import (
"net/http" "net/http"
"strings" "strings"
"errors" "errors"
"path/filepath"
) )
func (c *Client) req(method, path string, body io.Reader, intercept func(*http.Request)) (req *http.Response, err error) { 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 return rs.StatusCode
} }
func (c *Client) createParentCollection(itemPath string) (err error) {
parentPath := filepath.Dir(itemPath)
return c.MkdirAll(parentPath, 0755)
}