From e4fac5eebf531789d600be84721ab5449ed4b549 Mon Sep 17 00:00:00 2001 From: Christoph Polcin Date: Mon, 27 Oct 2014 14:32:16 +0100 Subject: [PATCH] implement READ & WRITE --- client.go | 36 ++++++++++++++++++++++++++--- main/client.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++-- requests.go | 9 ++++++++ 3 files changed, 103 insertions(+), 5 deletions(-) diff --git a/client.go b/client.go index d864753..ea628dc 100644 --- a/client.go +++ b/client.go @@ -1,9 +1,9 @@ package gowebdav import ( + "bytes" "encoding/base64" "encoding/xml" - "fmt" "net/http" "os" "strings" @@ -184,6 +184,36 @@ func (c *Client) Copy(oldpath string, newpath string, overwrite bool) error { return c.copymove("COPY", oldpath, newpath, overwrite) } -func (c *Client) Read(path string) { - fmt.Println("Read " + path) +func (c *Client) Read(path string) ([]byte, error) { + rs, err := c.reqDo("GET", path, nil) + if err != nil { + return nil, newPathErrorErr("Read", path, err) + } + defer rs.Body.Close() + buf := new(bytes.Buffer) + buf.ReadFrom(rs.Body) + return buf.Bytes(), nil +} + +func (c *Client) Write(path string, data []byte, _ os.FileMode) error { + s := c.put(path, bytes.NewReader(data)) + switch s { + + case 200, 201: + return nil + + case 409: + if idx := strings.LastIndex(path, "/"); idx == -1 { + // faulty root + return newPathError("Write", path, 500) + } else { + if err := c.MkdirAll(path[0:idx+1], 0755); err == nil { + s = c.put(path, bytes.NewReader(data)) + if s == 200 || s == 201 { + return nil + } + } + } + } + return newPathError("Write", path, s) } diff --git a/main/client.go b/main/client.go index 54433c1..3b5223d 100644 --- a/main/client.go +++ b/main/client.go @@ -1,9 +1,12 @@ package main import ( + "bytes" + "errors" "flag" "fmt" d "gowebdav" + "io" "os" "strings" ) @@ -22,10 +25,23 @@ func Fail(err interface{}) { fmt.Println(" MKDIRALL | MKCOLALL ") fmt.Println(" MV | MOVE | RENAME ") fmt.Println(" CP | COPY ") + fmt.Println(" GET | PULL | READ ") + fmt.Println(" PUT | PUSH | WRITE ") + } os.Exit(-1) } +func writeFile(path string, bytes []byte, mode os.FileMode) error { + f, err := os.Create(path) + if err != nil { + return err + } + defer f.Close() + _, err = f.Write(bytes) + return err +} + func main() { root := flag.String("root", "URL", "WebDAV Endpoint") usr := flag.String("user", "", "user") @@ -58,8 +74,19 @@ func main() { fmt.Println(err) } - case "GET": - c.Read(path) + case "GET", "PULL", "READ": + if bytes, err := c.Read(path); err == nil { + if lidx := strings.LastIndex(path, "/"); lidx != -1 { + path = path[lidx+1:] + } + if err := writeFile(path, bytes, 0644); err != nil { + fmt.Println(err) + } else { + fmt.Println(fmt.Sprintf("Written %d bytes to: %s", len(bytes), path)) + } + } else { + fmt.Println(err) + } case "DELETE", "RM", "DEL": if err := c.Remove(path); err != nil { @@ -104,6 +131,17 @@ func main() { fmt.Println("Copy: " + a0 + " -> " + a1) } + case "PUT", "PUSH", "WRITE": + bytes, err := getBytes(a1) + if err != nil { + Fail(err) + } + if err := c.Write(a0, bytes, 0644); err != nil { + fmt.Println(err) + } else { + fmt.Println(fmt.Sprintf("Written: '%s' -> %s", a1, a0)) + } + default: Fail(nil) } @@ -111,3 +149,24 @@ func main() { Fail(nil) } } + +func getBytes(pathOrString string) ([]byte, error) { + fi, err := os.Stat(pathOrString) + if err == nil { + if fi.IsDir() { + return nil, &os.PathError{"Open", pathOrString, errors.New("Path: '" + pathOrString + "' is a directory")} + } + f, err := os.Open(pathOrString) + if err == nil { + defer f.Close() + buf := bytes.NewBuffer(nil) + _, err := io.Copy(buf, f) + if err == nil { + return buf.Bytes(), nil + } + } + return nil, &os.PathError{"Open", pathOrString, err} + } else { + return []byte(pathOrString), nil + } +} diff --git a/requests.go b/requests.go index 72dd79c..236a4e4 100644 --- a/requests.go +++ b/requests.go @@ -118,3 +118,12 @@ func (c *Client) copymove(method string, oldpath string, newpath string, overwri return newPathError(method, oldpath, rs.StatusCode) } + +func (c *Client) put(path string, stream io.Reader) int { + rs, err := c.reqDo("PUT", path, stream) + if err != nil { + return 400 + } + defer rs.Body.Close() + return rs.StatusCode +}