2014-10-23 10:39:55 +02:00
|
|
|
package gowebdav
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/xml"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2014-10-23 13:15:02 +02:00
|
|
|
"os"
|
2014-10-23 10:39:55 +02:00
|
|
|
"strings"
|
2014-10-23 13:15:02 +02:00
|
|
|
"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)
|
|
|
|
}
|
|
|
|
|
2014-10-23 13:15:02 +02:00
|
|
|
c.root = FixSlash(c.root)
|
2014-10-23 10:39:55 +02:00
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Connect() error {
|
2014-10-23 15:31:34 +02:00
|
|
|
if rs, err := c.options("/"); err == nil {
|
2014-10-23 10:39:55 +02:00
|
|
|
defer rs.Body.Close()
|
|
|
|
|
|
|
|
if rs.StatusCode != 200 || (rs.Header.Get("Dav") == "" && rs.Header.Get("DAV") == "") {
|
|
|
|
return errors.New(fmt.Sprintf("Bad Request: %d - %s", rs.StatusCode, c.root))
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO check PROPFIND if path is collection
|
|
|
|
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-10-23 13:15:02 +02:00
|
|
|
func (c *Client) ReadDir(path string) ([]os.FileInfo, error) {
|
2014-10-24 11:26:21 +02:00
|
|
|
// TODO return newPathError
|
2014-10-23 14:26:08 +02:00
|
|
|
path = FixSlashes(path)
|
2014-10-23 13:15:02 +02:00
|
|
|
files := make([]os.FileInfo, 0)
|
2014-10-23 13:38:49 +02:00
|
|
|
skipSelf := true
|
2014-10-23 10:39:55 +02:00
|
|
|
parse := func(resp interface{}) {
|
|
|
|
r := resp.(*response)
|
2014-10-23 13:38:49 +02:00
|
|
|
|
|
|
|
if skipSelf {
|
|
|
|
skipSelf = false
|
|
|
|
r.Props = nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-10-23 10:39:55 +02:00
|
|
|
if p := getProps(r, "200"); p != nil {
|
2014-10-23 13:15:02 +02:00
|
|
|
f := new(File)
|
|
|
|
f.name = p.Name
|
2014-10-23 13:38:16 +02:00
|
|
|
f.path = path + f.name
|
2014-10-23 13:15:02 +02:00
|
|
|
|
2014-10-23 10:39:55 +02:00
|
|
|
if p.Type.Local == "collection" {
|
2014-10-23 13:38:16 +02:00
|
|
|
f.path += "/"
|
2014-10-23 13:15:02 +02:00
|
|
|
f.size = 0
|
|
|
|
f.modified = time.Unix(0, 0)
|
|
|
|
f.isdir = true
|
2014-10-23 10:39:55 +02:00
|
|
|
} else {
|
2014-10-23 13:15:02 +02:00
|
|
|
f.size = parseInt64(&p.Size)
|
|
|
|
f.modified = parseModified(&p.Modified)
|
|
|
|
f.isdir = false
|
2014-10-23 10:39:55 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 13:15:02 +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-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-23 13:15:02 +02:00
|
|
|
return files, err
|
2014-10-23 10:39:55 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 14:10:31 +02:00
|
|
|
func (c *Client) Remove(path string) error {
|
2014-10-24 11:26:21 +02:00
|
|
|
// TODO return newPathError
|
2014-10-23 14:10:31 +02:00
|
|
|
rs, err := c.reqDo("DELETE", path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rs.Body.Close()
|
|
|
|
|
|
|
|
if rs.StatusCode == 200 {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return Error(rs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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:30:08 +02:00
|
|
|
|
2014-10-23 10:39:55 +02:00
|
|
|
func (c *Client) Read(path string) {
|
|
|
|
fmt.Println("Read " + path)
|
|
|
|
}
|