webdav: add Client.Readdir

This commit is contained in:
Simon Ser 2020-01-21 18:55:29 +01:00
parent 8e50764757
commit 6526cef9eb
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48

View File

@ -53,6 +53,34 @@ type fileInfo struct {
isDir bool isDir bool
} }
func fileInfoFromResponse(resp *internal.Response) (*fileInfo, error) {
href, err := resp.Href()
if err != nil {
return nil, err
}
filename, _ := path.Split(href)
fi := &fileInfo{filename: filename}
var resType internal.ResourceType
if err := resp.DecodeProp(&resType); err != nil {
return nil, err
}
if resType.Is(internal.CollectionName) {
fi.isDir = true
} else {
var getLen internal.GetContentLength
var getMod internal.GetLastModified
if err := resp.DecodeProp(&getLen, &getMod); err != nil {
return nil, err
}
fi.size = getLen.Length
fi.modTime = time.Time(getMod.LastModified)
}
return fi, nil
}
func (fi *fileInfo) Name() string { func (fi *fileInfo) Name() string {
return fi.filename return fi.filename
} }
@ -81,39 +109,19 @@ func (fi *fileInfo) Sys() interface{} {
return nil return nil
} }
func (c *Client) Stat(name string) (os.FileInfo, error) { // TODO: getetag, getcontenttype
// TODO: getetag, getcontenttype var fileInfoPropfind = internal.NewPropNamePropfind(
propfind := internal.NewPropNamePropfind(
internal.ResourceTypeName, internal.ResourceTypeName,
internal.GetContentLengthName, internal.GetContentLengthName,
internal.GetLastModifiedName, internal.GetLastModifiedName,
) )
resp, err := c.ic.PropfindFlat(name, propfind)
func (c *Client) Stat(name string) (os.FileInfo, error) {
resp, err := c.ic.PropfindFlat(name, fileInfoPropfind)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return fileInfoFromResponse(resp)
filename, _ := path.Split(name)
fi := &fileInfo{filename: filename}
var resType internal.ResourceType
if err := resp.DecodeProp(&resType); err != nil {
return nil, err
}
if resType.Is(internal.CollectionName) {
fi.isDir = true
} else {
var getLen internal.GetContentLength
var getMod internal.GetLastModified
if err := resp.DecodeProp(&getLen, &getMod); err != nil {
return nil, err
}
fi.size = getLen.Length
fi.modTime = time.Time(getMod.LastModified)
}
return fi, nil
} }
func (c *Client) Open(name string) (io.ReadCloser, error) { func (c *Client) Open(name string) (io.ReadCloser, error) {
@ -129,3 +137,23 @@ func (c *Client) Open(name string) (io.ReadCloser, error) {
return resp.Body, nil return resp.Body, nil
} }
func (c *Client) Readdir(name string) ([]os.FileInfo, error) {
// TODO: filter out the directory we're listing
ms, err := c.ic.Propfind(name, internal.DepthOne, fileInfoPropfind)
if err != nil {
return nil, err
}
l := make([]os.FileInfo, 0, len(ms.Responses))
for _, resp := range ms.Responses {
fi, err := fileInfoFromResponse(&resp)
if err != nil {
return l, err
}
l = append(l, fi)
}
return l, nil
}