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 package gowebdav
import ( import (
"bytes"
"encoding/base64" "encoding/base64"
"encoding/xml" "encoding/xml"
"fmt"
"net/http" "net/http"
"os" "os"
"strings" "strings"
@ -184,6 +184,36 @@ func (c *Client) Copy(oldpath string, newpath string, overwrite bool) error {
return c.copymove("COPY", oldpath, newpath, overwrite) return c.copymove("COPY", oldpath, newpath, overwrite)
} }
func (c *Client) Read(path string) { func (c *Client) Read(path string) ([]byte, error) {
fmt.Println("Read " + path) 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 package main
import ( import (
"bytes"
"errors"
"flag" "flag"
"fmt" "fmt"
d "gowebdav" d "gowebdav"
"io"
"os" "os"
"strings" "strings"
) )
@ -22,10 +25,23 @@ func Fail(err interface{}) {
fmt.Println(" MKDIRALL | MKCOLALL <PATH>") fmt.Println(" MKDIRALL | MKCOLALL <PATH>")
fmt.Println(" MV | MOVE | RENAME <OLD_PATH> <NEW_PATH>") fmt.Println(" MV | MOVE | RENAME <OLD_PATH> <NEW_PATH>")
fmt.Println(" CP | COPY <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) 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() { func main() {
root := flag.String("root", "URL", "WebDAV Endpoint") root := flag.String("root", "URL", "WebDAV Endpoint")
usr := flag.String("user", "", "user") usr := flag.String("user", "", "user")
@ -58,8 +74,19 @@ func main() {
fmt.Println(err) fmt.Println(err)
} }
case "GET": case "GET", "PULL", "READ":
c.Read(path) 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": case "DELETE", "RM", "DEL":
if err := c.Remove(path); err != nil { if err := c.Remove(path); err != nil {
@ -104,6 +131,17 @@ func main() {
fmt.Println("Copy: " + a0 + " -> " + a1) 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: default:
Fail(nil) Fail(nil)
} }
@ -111,3 +149,24 @@ func main() {
Fail(nil) 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) 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
}