webdav: add Client.Stat

This commit is contained in:
Simon Ser 2020-01-21 18:41:46 +01:00
parent 63cdea07be
commit e84362bc0a
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48

View File

@ -3,12 +3,15 @@ package webdav
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"os"
"path"
"time"
"github.com/emersion/go-webdav/internal" "github.com/emersion/go-webdav/internal"
) )
type Client struct { type Client struct {
c *internal.Client ic *internal.Client
} }
func NewClient(c *http.Client, endpoint string) (*Client, error) { func NewClient(c *http.Client, endpoint string) (*Client, error) {
@ -20,13 +23,13 @@ func NewClient(c *http.Client, endpoint string) (*Client, error) {
} }
func (c *Client) SetBasicAuth(username, password string) { func (c *Client) SetBasicAuth(username, password string) {
c.c.SetBasicAuth(username, password) c.ic.SetBasicAuth(username, password)
} }
func (c *Client) FindCurrentUserPrincipal() (string, error) { func (c *Client) FindCurrentUserPrincipal() (string, error) {
propfind := internal.NewPropNamePropfind(internal.CurrentUserPrincipalName) propfind := internal.NewPropNamePropfind(internal.CurrentUserPrincipalName)
resp, err := c.c.PropfindFlat("/", propfind) resp, err := c.ic.PropfindFlat("/", propfind)
if err != nil { if err != nil {
return "", err return "", err
} }
@ -41,3 +44,73 @@ func (c *Client) FindCurrentUserPrincipal() (string, error) {
return prop.Href, nil return prop.Href, nil
} }
type fileInfo struct {
filename string
size int64
modTime time.Time
isDir bool
}
func (fi *fileInfo) Name() string {
return fi.filename
}
func (fi *fileInfo) Size() int64 {
return fi.size
}
func (fi *fileInfo) Mode() os.FileMode {
if fi.isDir {
return os.ModePerm | os.ModeDir
} else {
return os.ModePerm
}
}
func (fi *fileInfo) ModTime() time.Time {
return fi.modTime
}
func (fi *fileInfo) IsDir() bool {
return fi.isDir
}
func (fi *fileInfo) Sys() interface{} {
return nil
}
func (c *Client) Stat(name string) (os.FileInfo, error) {
// TODO: getetag, getcontenttype
propfind := internal.NewPropNamePropfind(
internal.ResourceTypeName,
internal.GetContentLengthName,
internal.GetLastModifiedName,
)
resp, err := c.ic.PropfindFlat(name, propfind)
if err != nil {
return nil, err
}
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
}