implement READ & WRITE

This commit is contained in:
Christoph Polcin 2014-10-27 14:32:16 +01:00
parent d8aaad0e18
commit e4fac5eebf
3 changed files with 103 additions and 5 deletions

View File

@ -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)
}

View File

@ -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 <PATH>")
fmt.Println(" MV | MOVE | RENAME <OLD_PATH> <NEW_PATH>")
fmt.Println(" CP | COPY <OLD_PATH> <NEW_PATH>")
fmt.Println(" GET | PULL | READ <PATH>")
fmt.Println(" PUT | PUSH | WRITE <PATH> <FILE>")
}
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
}
}

View File

@ -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
}