2014-10-23 10:39:55 +02:00
|
|
|
package gowebdav
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2014-10-23 11:29:26 +02:00
|
|
|
"encoding/xml"
|
2014-10-23 14:10:31 +02:00
|
|
|
"fmt"
|
2014-10-23 10:39:55 +02:00
|
|
|
"io"
|
2014-10-24 11:25:17 +02:00
|
|
|
"os"
|
2014-10-23 10:39:55 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-10-24 11:25:28 +02:00
|
|
|
func log(msg interface{}) {
|
|
|
|
fmt.Println(msg)
|
|
|
|
}
|
|
|
|
|
2014-10-24 11:25:17 +02:00
|
|
|
func newPathError(op string, path string, statusCode int) error {
|
2017-10-05 16:22:10 +02:00
|
|
|
return &os.PathError{
|
|
|
|
Op: op,
|
|
|
|
Path: path,
|
|
|
|
Err: fmt.Errorf("%d", statusCode),
|
|
|
|
}
|
2014-10-24 11:25:17 +02:00
|
|
|
}
|
|
|
|
|
2014-10-24 14:06:52 +02:00
|
|
|
func newPathErrorErr(op string, path string, err error) error {
|
2017-10-05 16:22:10 +02:00
|
|
|
return &os.PathError{
|
|
|
|
Op: op,
|
|
|
|
Path: path,
|
|
|
|
Err: err,
|
|
|
|
}
|
2014-10-24 14:06:52 +02:00
|
|
|
}
|
|
|
|
|
2017-10-05 16:22:10 +02:00
|
|
|
// FixSlash appends a trailing / to our string
|
2014-10-23 13:15:02 +02:00
|
|
|
func FixSlash(s string) string {
|
|
|
|
if !strings.HasSuffix(s, "/") {
|
|
|
|
s += "/"
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2017-10-05 16:22:10 +02:00
|
|
|
// FixSlashes appends and prepends a / if they are missing
|
2014-10-23 14:26:08 +02:00
|
|
|
func FixSlashes(s string) string {
|
|
|
|
if s[0] != '/' {
|
|
|
|
s = "/" + s
|
|
|
|
}
|
|
|
|
return FixSlash(s)
|
|
|
|
}
|
|
|
|
|
2017-10-05 16:22:10 +02:00
|
|
|
// Join joins two paths
|
2014-10-23 10:39:55 +02:00
|
|
|
func Join(path0 string, path1 string) string {
|
|
|
|
return strings.TrimSuffix(path0, "/") + "/" + strings.TrimPrefix(path1, "/")
|
|
|
|
}
|
|
|
|
|
2017-10-05 16:22:10 +02:00
|
|
|
// String pulls a string out of our io.Reader
|
2014-10-23 10:39:55 +02:00
|
|
|
func String(r io.Reader) string {
|
|
|
|
buf := new(bytes.Buffer)
|
2017-10-05 16:22:10 +02:00
|
|
|
// TODO - mkae String return an error as well
|
|
|
|
_, _ = buf.ReadFrom(r)
|
2014-10-23 10:39:55 +02:00
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseUint(s *string) uint {
|
|
|
|
if n, e := strconv.ParseUint(*s, 10, 32); e == nil {
|
|
|
|
return uint(n)
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2014-10-23 13:15:02 +02:00
|
|
|
func parseInt64(s *string) int64 {
|
|
|
|
if n, e := strconv.ParseInt(*s, 10, 64); e == nil {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2014-10-23 10:39:55 +02:00
|
|
|
func parseModified(s *string) time.Time {
|
|
|
|
if t, e := time.Parse(time.RFC1123, *s); e == nil {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
return time.Unix(0, 0)
|
|
|
|
}
|
2014-10-23 11:29:26 +02:00
|
|
|
|
2014-10-24 12:39:35 +02:00
|
|
|
func parseXML(data io.Reader, resp interface{}, parse func(resp interface{}) error) error {
|
2014-10-23 11:29:26 +02:00
|
|
|
decoder := xml.NewDecoder(data)
|
|
|
|
for t, _ := decoder.Token(); t != nil; t, _ = decoder.Token() {
|
|
|
|
switch se := t.(type) {
|
|
|
|
case xml.StartElement:
|
|
|
|
if se.Name.Local == "response" {
|
|
|
|
if e := decoder.DecodeElement(resp, &se); e == nil {
|
2014-10-24 12:39:35 +02:00
|
|
|
if err := parse(resp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-10-23 11:29:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-24 12:39:35 +02:00
|
|
|
return nil
|
2014-10-23 11:29:26 +02:00
|
|
|
}
|