hacky proppatch support for lastmodified property (#438)

This commit is contained in:
Michael Quigley 2023-12-01 13:53:36 -05:00
parent b5210a61c6
commit ba3d1b5032
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
3 changed files with 36 additions and 6 deletions

View File

@ -4,6 +4,7 @@ import (
"fmt"
"github.com/openziti/zrok/util/sync/webdavClient"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"io"
"os"
"path/filepath"
@ -72,5 +73,14 @@ func (t *WebDAVTarget) WriteStream(path string, stream io.Reader, mode os.FileMo
}
func (t *WebDAVTarget) SetModificationTime(path string, mtime time.Time) error {
modtimeUnix := mtime.Unix()
body := "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
"<propertyupdate xmlns=\"DAV:\" xmlns:z=\"zrok:\"><set><prop><z:lastmodified>" +
fmt.Sprintf("%d", modtimeUnix) +
"</z:lastmodified></prop></set></propertyupdate>"
logrus.Infof("sending '%v'", body)
if err := t.c.Proppatch(path, body, nil, nil); err != nil {
return err
}
return nil
}

View File

@ -99,12 +99,12 @@ type props struct {
Modified string `xml:"DAV: prop>getlastmodified,omitempty"`
}
type response struct {
type Response struct {
Href string `xml:"DAV: href"`
Props []props `xml:"DAV: propstat"`
}
func getProps(r *response, status string) *props {
func getProps(r *Response, status string) *props {
for _, prop := range r.Props {
if strings.Contains(prop.Status, status) {
return &prop
@ -119,7 +119,7 @@ func (c *Client) ReadDir(path string) ([]os.FileInfo, error) {
files := make([]os.FileInfo, 0)
skipSelf := true
parse := func(resp interface{}) error {
r := resp.(*response)
r := resp.(*Response)
if skipSelf {
skipSelf = false
@ -169,7 +169,7 @@ func (c *Client) ReadDir(path string) ([]os.FileInfo, error) {
<d:getlastmodified/>
</d:prop>
</d:propfind>`,
&response{},
&Response{},
parse)
if err != nil {
@ -184,7 +184,7 @@ func (c *Client) ReadDir(path string) ([]os.FileInfo, error) {
func (c *Client) Stat(path string) (os.FileInfo, error) {
var f *File
parse := func(resp interface{}) error {
r := resp.(*response)
r := resp.(*Response)
if p := getProps(r, "200"); p != nil && f == nil {
f = new(File)
f.name = p.Name
@ -221,7 +221,7 @@ func (c *Client) Stat(path string) (os.FileInfo, error) {
<d:getlastmodified/>
</d:prop>
</d:propfind>`,
&response{},
&Response{},
parse)
if err != nil {

View File

@ -105,6 +105,26 @@ func (c *Client) propfind(path string, self bool, body string, resp interface{},
return parseXML(rs.Body, resp, parse)
}
func (c *Client) Proppatch(path string, body string, resp interface{}, parse func(resp interface{}) error) error {
rs, err := c.req("PROPPATCH", path, strings.NewReader(body), func(rq *http.Request) {
rq.Header.Add("Content-Type", "application/xml;charset=UTF-8")
rq.Header.Add("Accept", "application/xml,text/xml")
rq.Header.Add("Accept-Charset", "utf-8")
// TODO add support for 'gzip,deflate;q=0.8,q=0.7'
rq.Header.Add("Accept-Encoding", "")
})
if err != nil {
return err
}
defer rs.Body.Close()
if rs.StatusCode != 207 {
return NewPathError("PROPPATCH", path, rs.StatusCode)
}
return parseXML(rs.Body, resp, parse)
}
func (c *Client) doCopyMove(
method string,
oldpath string,