gowebdav/client.go

293 lines
6.0 KiB
Go
Raw Permalink Normal View History

2014-10-23 10:39:55 +02:00
package gowebdav
import (
2014-10-27 14:32:16 +01:00
"bytes"
2014-10-23 10:39:55 +02:00
"encoding/base64"
"encoding/xml"
2014-10-27 15:17:54 +01:00
"io"
2014-10-23 10:39:55 +02:00
"net/http"
"net/url"
"os"
pathpkg "path"
2014-10-23 10:39:55 +02:00
"strings"
"time"
2014-10-23 10:39:55 +02:00
)
type Client struct {
root string
headers http.Header
c *http.Client
}
func NewClient(uri string, user string, pw string) *Client {
c := &Client{uri, make(http.Header), &http.Client{}}
if len(user) > 0 && len(pw) > 0 {
a := user + ":" + pw
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(a))
c.headers.Add("Authorization", auth)
}
c.root = FixSlash(c.root)
2014-10-23 10:39:55 +02:00
return c
}
func (c *Client) Connect() error {
rs, err := c.options("/")
if err == nil {
2014-10-27 16:29:57 +01:00
rs.Body.Close()
2014-10-23 10:39:55 +02:00
if rs.StatusCode != 200 || (rs.Header.Get("Dav") == "" && rs.Header.Get("DAV") == "") {
return newPathError("Connect", c.root, rs.StatusCode)
2014-10-23 10:39:55 +02:00
}
_, err = c.ReadDir("/")
2014-10-23 10:39:55 +02:00
}
return err
2014-10-23 10:39:55 +02:00
}
type props struct {
Status string `xml:"DAV: status"`
Name string `xml:"DAV: prop>displayname,omitempty"`
Type xml.Name `xml:"DAV: prop>resourcetype>collection,omitempty"`
Size string `xml:"DAV: prop>getcontentlength,omitempty"`
Modified string `xml:"DAV: prop>getlastmodified,omitempty"`
}
type response struct {
Href string `xml:"DAV: href"`
Props []props `xml:"DAV: propstat"`
}
2014-10-23 11:28:46 +02:00
func getProps(r *response, status string) *props {
2014-10-23 10:39:55 +02:00
for _, prop := range r.Props {
if strings.Index(prop.Status, status) != -1 {
return &prop
}
}
return nil
}
func (c *Client) ReadDir(path string) ([]os.FileInfo, error) {
2014-10-23 14:26:08 +02:00
path = FixSlashes(path)
files := make([]os.FileInfo, 0)
2014-10-23 13:38:49 +02:00
skipSelf := true
2014-10-24 12:39:35 +02:00
parse := func(resp interface{}) error {
2014-10-23 10:39:55 +02:00
r := resp.(*response)
2014-10-23 13:38:49 +02:00
if skipSelf {
skipSelf = false
2014-10-24 12:39:35 +02:00
if p := getProps(r, "200"); p != nil && p.Type.Local == "collection" {
r.Props = nil
return nil
}
return newPathError("ReadDir", path, 405)
2014-10-23 13:38:49 +02:00
}
2014-10-23 10:39:55 +02:00
if p := getProps(r, "200"); p != nil {
f := new(File)
if ps, err := url.QueryUnescape(r.Href); err == nil {
f.name = pathpkg.Base(ps)
} else {
f.name = p.Name
}
2014-10-23 13:38:16 +02:00
f.path = path + f.name
2015-12-09 10:02:37 +01:00
f.modified = parseModified(&p.Modified)
2014-10-23 10:39:55 +02:00
if p.Type.Local == "collection" {
2014-10-23 13:38:16 +02:00
f.path += "/"
f.size = 0
f.isdir = true
2014-10-23 10:39:55 +02:00
} else {
f.size = parseInt64(&p.Size)
f.isdir = false
2014-10-23 10:39:55 +02:00
}
files = append(files, *f)
2014-10-23 10:39:55 +02:00
}
2014-10-23 13:38:16 +02:00
r.Props = nil
2014-10-24 12:39:35 +02:00
return nil
2014-10-23 10:39:55 +02:00
}
2014-10-23 15:31:34 +02:00
err := c.propfind(path, false,
2014-10-23 10:39:55 +02:00
`<d:propfind xmlns:d='DAV:'>
<d:prop>
<d:displayname/>
<d:resourcetype/>
<d:getcontentlength/>
<d:getlastmodified/>
</d:prop>
</d:propfind>`,
&response{},
parse)
2014-10-24 12:39:35 +02:00
2014-10-24 11:38:11 +02:00
if err != nil {
2014-10-24 12:39:35 +02:00
if _, ok := err.(*os.PathError); !ok {
err = &os.PathError{"ReadDir", path, err}
}
2014-10-24 11:38:11 +02:00
}
return files, err
2014-10-23 10:39:55 +02:00
}
2014-10-27 17:02:28 +01:00
func (c *Client) Stat(path string) (os.FileInfo, error) {
var f *File = nil
parse := func(resp interface{}) error {
r := resp.(*response)
if p := getProps(r, "200"); p != nil && f == nil {
f = new(File)
f.name = p.Name
f.path = path
if p.Type.Local == "collection" {
if !strings.HasSuffix(f.path, "/") {
f.path += "/"
}
f.size = 0
f.modified = time.Unix(0, 0)
f.isdir = true
} else {
f.size = parseInt64(&p.Size)
f.modified = parseModified(&p.Modified)
f.isdir = false
}
}
r.Props = nil
return nil
}
err := c.propfind(path, true,
`<d:propfind xmlns:d='DAV:'>
<d:prop>
<d:displayname/>
<d:resourcetype/>
<d:getcontentlength/>
<d:getlastmodified/>
</d:prop>
</d:propfind>`,
&response{},
parse)
if err != nil {
if _, ok := err.(*os.PathError); !ok {
err = &os.PathError{"ReadDir", path, err}
}
}
return f, err
}
2014-10-23 14:10:31 +02:00
func (c *Client) Remove(path string) error {
2014-10-24 12:01:54 +02:00
return c.RemoveAll(path)
}
func (c *Client) RemoveAll(path string) error {
2014-10-27 16:19:17 +01:00
rs, err := c.req("DELETE", path, nil, nil)
2014-10-23 14:10:31 +02:00
if err != nil {
2014-10-24 11:38:11 +02:00
return newPathError("Remove", path, 400)
2014-10-23 14:10:31 +02:00
}
2014-10-27 16:29:57 +01:00
rs.Body.Close()
2014-10-23 14:10:31 +02:00
2014-10-27 16:19:17 +01:00
if rs.StatusCode == 200 || rs.StatusCode == 404 {
2014-10-23 14:10:31 +02:00
return nil
} else {
2014-10-24 11:38:11 +02:00
return newPathError("Remove", path, rs.StatusCode)
2014-10-23 14:10:31 +02:00
}
}
2014-10-24 11:30:08 +02:00
func (c *Client) Mkdir(path string, _ os.FileMode) error {
2014-10-23 15:00:20 +02:00
path = FixSlashes(path)
2014-10-23 15:31:34 +02:00
status := c.mkcol(path)
2014-10-23 15:00:20 +02:00
if status == 201 {
return nil
2014-10-24 11:30:08 +02:00
} else {
return newPathError("Mkdir", path, status)
2014-10-23 15:00:20 +02:00
}
}
2014-10-24 11:31:16 +02:00
func (c *Client) MkdirAll(path string, _ os.FileMode) error {
path = FixSlashes(path)
status := c.mkcol(path)
if status == 201 {
return nil
} else if status == 409 {
paths := strings.Split(path, "/")
sub := "/"
for _, e := range paths {
if e == "" {
continue
}
sub += e + "/"
status = c.mkcol(sub)
if status != 201 {
return newPathError("MkdirAll", sub, status)
}
}
return nil
}
return newPathError("MkdirAll", path, status)
}
2014-10-24 11:30:08 +02:00
2014-10-24 14:08:42 +02:00
func (c *Client) Rename(oldpath string, newpath string, overwrite bool) error {
return c.copymove("MOVE", oldpath, newpath, overwrite)
}
func (c *Client) Copy(oldpath string, newpath string, overwrite bool) error {
return c.copymove("COPY", oldpath, newpath, overwrite)
}
2014-10-27 14:32:16 +01:00
func (c *Client) Read(path string) ([]byte, error) {
2014-10-27 15:17:54 +01:00
if stream, err := c.ReadStream(path); err == nil {
defer stream.Close()
buf := new(bytes.Buffer)
buf.ReadFrom(stream)
return buf.Bytes(), nil
} else {
return nil, err
}
}
func (c *Client) ReadStream(path string) (io.ReadCloser, error) {
2014-10-27 16:19:17 +01:00
rs, err := c.req("GET", path, nil, nil)
2014-10-27 14:32:16 +01:00
if err != nil {
2014-10-27 15:17:54 +01:00
return nil, newPathErrorErr("ReadStream", path, err)
2014-10-27 14:32:16 +01:00
}
2014-10-27 15:17:54 +01:00
return rs.Body, nil
2014-10-27 14:32:16 +01:00
}
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)
2014-10-23 10:39:55 +02:00
}
2014-10-27 15:17:54 +01:00
func (c *Client) WriteStream(path string, stream io.Reader, _ os.FileMode) error {
// TODO check if parent collection exists
s := c.put(path, stream)
switch s {
case 200, 201:
return nil
default:
return newPathError("WriteStream", path, s)
}
}