diff --git a/.travis.yml b/.travis.yml index 1ca29c5..76bfb65 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,3 +2,9 @@ language: go go: - "1.x" + +install: + - go get ./... + +script: + - go test -v --short ./... \ No newline at end of file diff --git a/client.go b/client.go index 59525e3..40990ba 100644 --- a/client.go +++ b/client.go @@ -346,13 +346,14 @@ 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 - } - } + err := c.createParentCollection(path) + if err != nil { + return err + } + + s = c.put(path, bytes.NewReader(data)) + if s == 200 || s == 201 || s == 204 { + return nil } } @@ -361,8 +362,14 @@ 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 + + err := c.createParentCollection(path) + if err != nil { + return err + } + s := c.put(path, stream) + switch s { case 200, 201, 204: return nil diff --git a/requests.go b/requests.go index 73db2c8..7a9981d 100644 --- a/requests.go +++ b/requests.go @@ -6,6 +6,7 @@ import ( "io" "net/http" "strings" + "path" ) func (c *Client) req(method, path string, body io.Reader, intercept func(*http.Request)) (req *http.Response, err error) { @@ -133,7 +134,12 @@ func (c *Client) copymove(method string, oldpath string, newpath string, overwri log(fmt.Sprintf(" TODO handle %s - %s multistatus result %s", method, oldpath, String(data))) case 409: - // TODO create dst path + err := c.createParentCollection(newpath) + if err != nil { + return err + } + + return c.copymove(method, oldpath, newpath, overwrite) } return newPathError(method, oldpath, s) @@ -148,3 +154,8 @@ func (c *Client) put(path string, stream io.Reader) int { return rs.StatusCode } + +func (c *Client) createParentCollection(itemPath string) (err error) { + parentPath := path.Dir(itemPath) + return c.MkdirAll(parentPath, 0755) +}