2020-01-14 21:19:54 +00:00
|
|
|
package carddav
|
|
|
|
|
|
|
|
import (
|
2020-01-14 22:44:21 +00:00
|
|
|
"bytes"
|
2020-01-19 14:29:51 +00:00
|
|
|
"fmt"
|
2020-02-12 20:36:09 +00:00
|
|
|
"mime"
|
2020-01-20 10:15:22 +00:00
|
|
|
"net"
|
2020-01-14 21:19:54 +00:00
|
|
|
"net/http"
|
2020-01-20 10:15:22 +00:00
|
|
|
"net/url"
|
2020-02-12 18:47:16 +00:00
|
|
|
"strconv"
|
2020-01-20 12:17:19 +00:00
|
|
|
"strings"
|
2020-01-22 14:35:36 +00:00
|
|
|
"time"
|
2020-01-14 21:19:54 +00:00
|
|
|
|
2020-01-14 22:44:21 +00:00
|
|
|
"github.com/emersion/go-vcard"
|
2020-01-14 21:19:54 +00:00
|
|
|
"github.com/emersion/go-webdav"
|
|
|
|
"github.com/emersion/go-webdav/internal"
|
|
|
|
)
|
|
|
|
|
2020-01-20 10:15:22 +00:00
|
|
|
// Discover performs a DNS-based CardDAV service discovery as described in
|
|
|
|
// RFC 6352 section 11. It returns the URL to the CardDAV server.
|
|
|
|
func Discover(domain string) (string, error) {
|
|
|
|
// Only lookup carddavs (not carddav), plaintext connections are insecure
|
|
|
|
_, addrs, err := net.LookupSRV("carddavs", "tcp", domain)
|
|
|
|
if dnsErr, ok := err.(*net.DNSError); ok {
|
|
|
|
if dnsErr.IsTemporary {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(addrs) == 0 {
|
|
|
|
return "", fmt.Errorf("carddav: domain doesn't have an SRV record")
|
|
|
|
}
|
|
|
|
addr := addrs[0]
|
|
|
|
|
2020-01-20 12:17:19 +00:00
|
|
|
target := strings.TrimSuffix(addr.Target, ".")
|
|
|
|
if target == "" {
|
2020-02-19 15:32:35 +00:00
|
|
|
return "", fmt.Errorf("carddav: empty target in SRV record")
|
2020-01-20 12:17:19 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 10:15:22 +00:00
|
|
|
u := url.URL{Scheme: "https"}
|
|
|
|
if addr.Port == 443 {
|
2020-02-19 15:31:03 +00:00
|
|
|
u.Host = target
|
2020-01-20 10:15:22 +00:00
|
|
|
} else {
|
2020-01-20 12:17:19 +00:00
|
|
|
u.Host = fmt.Sprintf("%v:%v", target, addr.Port)
|
2020-01-20 10:15:22 +00:00
|
|
|
}
|
|
|
|
return u.String(), nil
|
|
|
|
}
|
|
|
|
|
2020-01-21 20:01:18 +00:00
|
|
|
// Client provides access to a remote CardDAV server.
|
2020-01-14 21:19:54 +00:00
|
|
|
type Client struct {
|
|
|
|
*webdav.Client
|
|
|
|
|
|
|
|
ic *internal.Client
|
|
|
|
}
|
|
|
|
|
2020-02-19 15:02:49 +00:00
|
|
|
func NewClient(c webdav.HTTPClient, endpoint string) (*Client, error) {
|
2020-01-14 21:19:54 +00:00
|
|
|
wc, err := webdav.NewClient(c, endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ic, err := internal.NewClient(c, endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Client{wc, ic}, nil
|
|
|
|
}
|
|
|
|
|
2020-02-05 15:08:15 +00:00
|
|
|
func (c *Client) HasSupport() error {
|
2020-02-12 16:13:12 +00:00
|
|
|
classes, _, err := c.ic.Options("")
|
2020-02-05 15:08:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !classes["addressbook"] {
|
|
|
|
return fmt.Errorf("carddav: server doesn't support the DAV addressbook class")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-14 22:13:23 +00:00
|
|
|
func (c *Client) FindAddressBookHomeSet(principal string) (string, error) {
|
2020-01-18 11:43:47 +00:00
|
|
|
propfind := internal.NewPropNamePropfind(addressBookHomeSetName)
|
2020-01-14 21:19:54 +00:00
|
|
|
resp, err := c.ic.PropfindFlat(principal, propfind)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var prop addressbookHomeSet
|
2020-01-15 10:44:27 +00:00
|
|
|
if err := resp.DecodeProp(&prop); err != nil {
|
2020-01-14 21:19:54 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-01-22 10:07:30 +00:00
|
|
|
return prop.Href.Path, nil
|
2020-01-14 21:19:54 +00:00
|
|
|
}
|
2020-01-14 22:13:23 +00:00
|
|
|
|
2020-02-27 11:36:14 +00:00
|
|
|
func decodeSupportedAddressData(supported *supportedAddressData) []AddressDataType {
|
|
|
|
l := make([]AddressDataType, len(supported.Types))
|
|
|
|
for i, t := range supported.Types {
|
|
|
|
l[i] = AddressDataType{t.ContentType, t.Version}
|
|
|
|
}
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2020-01-14 22:13:23 +00:00
|
|
|
func (c *Client) FindAddressBooks(addressBookHomeSet string) ([]AddressBook, error) {
|
2020-01-19 14:29:51 +00:00
|
|
|
propfind := internal.NewPropNamePropfind(
|
|
|
|
internal.ResourceTypeName,
|
|
|
|
internal.DisplayNameName,
|
|
|
|
addressBookDescriptionName,
|
|
|
|
maxResourceSizeName,
|
2020-02-27 11:36:14 +00:00
|
|
|
supportedAddressDataName,
|
2020-01-19 14:29:51 +00:00
|
|
|
)
|
2020-01-15 11:30:42 +00:00
|
|
|
ms, err := c.ic.Propfind(addressBookHomeSet, internal.DepthOne, propfind)
|
2020-01-14 22:13:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
l := make([]AddressBook, 0, len(ms.Responses))
|
2020-01-14 22:44:21 +00:00
|
|
|
for _, resp := range ms.Responses {
|
2020-01-22 10:07:30 +00:00
|
|
|
path, err := resp.Path()
|
2020-01-14 22:13:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-21 20:04:19 +00:00
|
|
|
var resType internal.ResourceType
|
|
|
|
if err := resp.DecodeProp(&resType); err != nil {
|
2020-01-14 22:13:23 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-21 20:04:19 +00:00
|
|
|
if !resType.Is(addressBookName) {
|
2020-01-14 22:13:23 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-01-21 20:04:19 +00:00
|
|
|
var desc addressbookDescription
|
|
|
|
if err := resp.DecodeProp(&desc); err != nil && !internal.IsNotFound(err) {
|
2020-01-14 22:13:23 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-21 20:04:19 +00:00
|
|
|
var dispName internal.DisplayName
|
|
|
|
if err := resp.DecodeProp(&dispName); err != nil && !internal.IsNotFound(err) {
|
2020-01-19 14:29:51 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var maxResSize maxResourceSize
|
|
|
|
if err := resp.DecodeProp(&maxResSize); err != nil && !internal.IsNotFound(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if maxResSize.Size < 0 {
|
|
|
|
return nil, fmt.Errorf("carddav: max-resource-size must be a positive integer")
|
|
|
|
}
|
|
|
|
|
2020-02-27 11:36:14 +00:00
|
|
|
var supported supportedAddressData
|
|
|
|
if err := resp.DecodeProp(&supported); err != nil && !internal.IsNotFound(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-14 22:13:23 +00:00
|
|
|
l = append(l, AddressBook{
|
2020-02-27 11:36:14 +00:00
|
|
|
Path: path,
|
|
|
|
Name: dispName.Name,
|
|
|
|
Description: desc.Description,
|
|
|
|
MaxResourceSize: maxResSize.Size,
|
|
|
|
SupportedAddressData: decodeSupportedAddressData(&supported),
|
2020-01-14 22:13:23 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return l, nil
|
|
|
|
}
|
2020-01-14 22:44:21 +00:00
|
|
|
|
2020-01-27 09:30:19 +00:00
|
|
|
func encodeAddressPropReq(req *AddressDataRequest) (*internal.Prop, error) {
|
2020-01-22 14:35:36 +00:00
|
|
|
var addrDataReq addressDataReq
|
2020-01-27 09:30:19 +00:00
|
|
|
if req.AllProp {
|
2020-01-22 18:18:58 +00:00
|
|
|
addrDataReq.Allprop = &struct{}{}
|
|
|
|
} else {
|
2020-01-27 09:30:19 +00:00
|
|
|
for _, name := range req.Props {
|
2020-01-22 18:18:58 +00:00
|
|
|
addrDataReq.Props = append(addrDataReq.Props, prop{Name: name})
|
|
|
|
}
|
2020-01-22 14:35:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getLastModReq := internal.NewRawXMLElement(internal.GetLastModifiedName, nil, nil)
|
|
|
|
getETagReq := internal.NewRawXMLElement(internal.GetETagName, nil, nil)
|
|
|
|
return internal.EncodeProp(&addrDataReq, getLastModReq, getETagReq)
|
|
|
|
}
|
|
|
|
|
2020-01-24 11:24:35 +00:00
|
|
|
func encodePropFilter(pf *PropFilter) (*propFilter, error) {
|
|
|
|
el := &propFilter{Name: pf.Name, Test: filterTest(pf.Test)}
|
|
|
|
if pf.IsNotDefined {
|
|
|
|
if len(pf.TextMatches) > 0 || len(pf.Params) > 0 {
|
|
|
|
return nil, fmt.Errorf("carddav: failed to encode PropFilter: IsNotDefined cannot be set with TextMatches or Params")
|
|
|
|
}
|
|
|
|
el.IsNotDefined = &struct{}{}
|
|
|
|
}
|
|
|
|
for _, tm := range pf.TextMatches {
|
|
|
|
el.TextMatches = append(el.TextMatches, *encodeTextMatch(&tm))
|
|
|
|
}
|
|
|
|
for _, param := range pf.Params {
|
|
|
|
paramEl, err := encodeParamFilter(¶m)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
el.Params = append(el.Params, *paramEl)
|
|
|
|
}
|
|
|
|
return el, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeParamFilter(pf *ParamFilter) (*paramFilter, error) {
|
|
|
|
el := ¶mFilter{Name: pf.Name}
|
|
|
|
if pf.IsNotDefined {
|
|
|
|
if pf.TextMatch != nil {
|
|
|
|
return nil, fmt.Errorf("carddav: failed to encode ParamFilter: only one of IsNotDefined or TextMatch can be set")
|
|
|
|
}
|
|
|
|
el.IsNotDefined = &struct{}{}
|
|
|
|
}
|
|
|
|
if pf.TextMatch != nil {
|
|
|
|
el.TextMatch = encodeTextMatch(pf.TextMatch)
|
|
|
|
}
|
|
|
|
return el, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeTextMatch(tm *TextMatch) *textMatch {
|
|
|
|
return &textMatch{
|
|
|
|
Text: tm.Text,
|
|
|
|
NegateCondition: negateCondition(tm.NegateCondition),
|
|
|
|
MatchType: matchType(tm.MatchType),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-17 15:20:05 +00:00
|
|
|
func decodeAddressList(ms *internal.Multistatus) ([]AddressObject, error) {
|
|
|
|
addrs := make([]AddressObject, 0, len(ms.Responses))
|
2020-01-15 11:09:42 +00:00
|
|
|
for _, resp := range ms.Responses {
|
2020-01-22 10:07:30 +00:00
|
|
|
path, err := resp.Path()
|
2020-01-15 11:09:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var addrData addressDataResp
|
|
|
|
if err := resp.DecodeProp(&addrData); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-22 14:35:36 +00:00
|
|
|
var getLastMod internal.GetLastModified
|
|
|
|
if err := resp.DecodeProp(&getLastMod); err != nil && !internal.IsNotFound(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var getETag internal.GetETag
|
|
|
|
if err := resp.DecodeProp(&getETag); err != nil && !internal.IsNotFound(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:09:42 +00:00
|
|
|
r := bytes.NewReader(addrData.Data)
|
|
|
|
card, err := vcard.NewDecoder(r).Decode()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-17 15:20:05 +00:00
|
|
|
addrs = append(addrs, AddressObject{
|
2020-01-22 14:35:36 +00:00
|
|
|
Path: path,
|
|
|
|
ModTime: time.Time(getLastMod.LastModified),
|
2020-02-03 20:48:31 +00:00
|
|
|
ETag: string(getETag.ETag),
|
2020-01-22 14:35:36 +00:00
|
|
|
Card: card,
|
2020-01-15 11:09:42 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return addrs, nil
|
|
|
|
}
|
|
|
|
|
2020-01-17 15:20:05 +00:00
|
|
|
func (c *Client) QueryAddressBook(addressBook string, query *AddressBookQuery) ([]AddressObject, error) {
|
2020-01-27 09:30:19 +00:00
|
|
|
propReq, err := encodeAddressPropReq(&query.DataRequest)
|
2020-01-15 10:17:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-01-14 22:44:21 +00:00
|
|
|
}
|
|
|
|
|
2020-01-15 10:17:38 +00:00
|
|
|
addressbookQuery := addressbookQuery{Prop: propReq}
|
2020-01-24 11:24:35 +00:00
|
|
|
addressbookQuery.Filter.Test = filterTest(query.FilterTest)
|
|
|
|
for _, pf := range query.PropFilters {
|
|
|
|
el, err := encodePropFilter(&pf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
addressbookQuery.Filter.Props = append(addressbookQuery.Filter.Props, *el)
|
|
|
|
}
|
2020-01-23 09:35:06 +00:00
|
|
|
if query.Limit > 0 {
|
|
|
|
addressbookQuery.Limit = &limit{NResults: uint(query.Limit)}
|
|
|
|
}
|
2020-01-15 10:17:38 +00:00
|
|
|
|
2020-01-14 22:44:21 +00:00
|
|
|
req, err := c.ic.NewXMLRequest("REPORT", addressBook, &addressbookQuery)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("Depth", "1")
|
|
|
|
|
|
|
|
ms, err := c.ic.DoMultiStatus(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-15 11:09:42 +00:00
|
|
|
return decodeAddressList(ms)
|
|
|
|
}
|
2020-01-14 22:44:21 +00:00
|
|
|
|
2020-01-22 10:07:30 +00:00
|
|
|
func (c *Client) MultiGetAddressBook(path string, multiGet *AddressBookMultiGet) ([]AddressObject, error) {
|
2020-01-27 09:30:19 +00:00
|
|
|
propReq, err := encodeAddressPropReq(&multiGet.DataRequest)
|
2020-01-15 11:09:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-14 22:44:21 +00:00
|
|
|
|
2020-01-15 11:09:42 +00:00
|
|
|
addressbookMultiget := addressbookMultiget{Prop: propReq}
|
|
|
|
|
2020-01-22 10:07:30 +00:00
|
|
|
if multiGet == nil || len(multiGet.Paths) == 0 {
|
|
|
|
href := internal.Href{Path: path}
|
|
|
|
addressbookMultiget.Hrefs = []internal.Href{href}
|
2020-01-15 11:09:42 +00:00
|
|
|
} else {
|
2020-01-22 10:07:30 +00:00
|
|
|
addressbookMultiget.Hrefs = make([]internal.Href, len(multiGet.Paths))
|
|
|
|
for i, p := range multiGet.Paths {
|
|
|
|
addressbookMultiget.Hrefs[i] = internal.Href{Path: p}
|
|
|
|
}
|
2020-01-14 22:44:21 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 10:07:30 +00:00
|
|
|
req, err := c.ic.NewXMLRequest("REPORT", path, &addressbookMultiget)
|
2020-01-15 11:09:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("Depth", "1")
|
|
|
|
|
|
|
|
ms, err := c.ic.DoMultiStatus(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return decodeAddressList(ms)
|
2020-01-14 22:44:21 +00:00
|
|
|
}
|
2020-02-12 18:47:16 +00:00
|
|
|
|
2020-02-12 20:36:09 +00:00
|
|
|
func populateAddressObject(ao *AddressObject, resp *http.Response) error {
|
|
|
|
if loc := resp.Header.Get("Location"); loc != "" {
|
|
|
|
u, err := url.Parse(loc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ao.Path = u.Path
|
|
|
|
}
|
|
|
|
if etag := resp.Header.Get("ETag"); etag != "" {
|
|
|
|
etag, err := strconv.Unquote(etag)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ao.ETag = etag
|
|
|
|
}
|
|
|
|
if lastModified := resp.Header.Get("Last-Modified"); lastModified != "" {
|
|
|
|
t, err := http.ParseTime(lastModified)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ao.ModTime = t
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetAddressObject(path string) (*AddressObject, error) {
|
|
|
|
req, err := c.ic.NewRequest(http.MethodGet, path, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Set("Accept", vcard.MIMEType)
|
|
|
|
|
|
|
|
resp, err := c.ic.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
mediaType, _, err := mime.ParseMediaType(resp.Header.Get("Content-Type"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !strings.EqualFold(mediaType, vcard.MIMEType) {
|
|
|
|
return nil, fmt.Errorf("carddav: expected Content-Type %q, got %q", vcard.MIMEType, mediaType)
|
|
|
|
}
|
|
|
|
|
|
|
|
card, err := vcard.NewDecoder(resp.Body).Decode()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ao := &AddressObject{
|
|
|
|
Path: resp.Request.URL.Path,
|
|
|
|
Card: card,
|
|
|
|
}
|
|
|
|
if err := populateAddressObject(ao, resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return ao, nil
|
|
|
|
}
|
|
|
|
|
2020-02-12 18:47:16 +00:00
|
|
|
func (c *Client) PutAddressObject(path string, card vcard.Card) (*AddressObject, error) {
|
|
|
|
// TODO: add support for If-None-Match and If-Match
|
|
|
|
|
2020-02-12 20:10:52 +00:00
|
|
|
// TODO: some servers want a Content-Length header, so we can't stream the
|
|
|
|
// request body here. See the Radicale issue:
|
|
|
|
// https://github.com/Kozea/Radicale/issues/1016
|
2020-02-12 18:47:16 +00:00
|
|
|
|
2020-02-12 20:10:52 +00:00
|
|
|
//pr, pw := io.Pipe()
|
|
|
|
//go func() {
|
|
|
|
// err := vcard.NewEncoder(pw).Encode(card)
|
|
|
|
// pw.CloseWithError(err)
|
|
|
|
//}()
|
2020-02-12 18:47:16 +00:00
|
|
|
|
2020-02-12 20:10:52 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := vcard.NewEncoder(&buf).Encode(card); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := c.ic.NewRequest(http.MethodPut, path, &buf)
|
2020-02-12 18:47:16 +00:00
|
|
|
if err != nil {
|
2020-02-12 20:10:52 +00:00
|
|
|
//pr.Close()
|
2020-02-12 18:47:16 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", vcard.MIMEType)
|
|
|
|
|
|
|
|
resp, err := c.ic.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-12 20:36:09 +00:00
|
|
|
resp.Body.Close()
|
2020-02-12 18:47:16 +00:00
|
|
|
|
|
|
|
ao := &AddressObject{Path: path}
|
2020-02-12 20:36:09 +00:00
|
|
|
if err := populateAddressObject(ao, resp); err != nil {
|
|
|
|
return nil, err
|
2020-02-12 18:47:16 +00:00
|
|
|
}
|
|
|
|
return ao, nil
|
|
|
|
}
|
2020-03-29 13:08:48 +01:00
|
|
|
|
|
|
|
// SyncCollection do a sync-collection operation on resource(path), it returns a SyncResponse
|
|
|
|
func (c *Client) SyncCollection(path string, query *SyncQuery) (*SyncResponse, error) {
|
|
|
|
var limit *internal.Limit
|
|
|
|
if query.Limit > 0 {
|
|
|
|
limit = &internal.Limit{NResults: uint(query.Limit)}
|
|
|
|
}
|
|
|
|
|
|
|
|
propReq, err := encodeAddressPropReq(&query.DataRequest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ms, err := c.ic.SyncCollection(path, query.SyncToken, internal.DepthOne, limit, propReq)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := &SyncResponse{SyncToken: ms.SyncToken}
|
|
|
|
for _, resp := range ms.Responses {
|
|
|
|
p, err := resp.Path()
|
2022-05-03 19:15:21 +01:00
|
|
|
if internal.IsNotFound(err) {
|
|
|
|
ret.Deleted = append(ret.Deleted, p)
|
|
|
|
continue
|
|
|
|
} else if err != nil {
|
2020-03-29 13:08:48 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if p == path || path == fmt.Sprintf("%s/", p) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var getLastMod internal.GetLastModified
|
|
|
|
if err := resp.DecodeProp(&getLastMod); err != nil && !internal.IsNotFound(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var getETag internal.GetETag
|
|
|
|
if err := resp.DecodeProp(&getETag); err != nil && !internal.IsNotFound(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
o := AddressObject{
|
|
|
|
Path: p,
|
|
|
|
ModTime: time.Time(getLastMod.LastModified),
|
|
|
|
ETag: string(getETag.ETag),
|
|
|
|
}
|
|
|
|
ret.Updated = append(ret.Updated, o)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|