wip: switch to io/fs

References: https://github.com/emersion/go-webdav/issues/40
This commit is contained in:
Simon Ser 2022-05-02 16:14:48 +02:00
parent 3f8b212b0d
commit 4d71a37916
2 changed files with 57 additions and 9 deletions

View File

@ -3,6 +3,7 @@ package webdav
import (
"fmt"
"io"
"io/fs"
"net/http"
"time"
@ -74,13 +75,13 @@ var fileInfoPropfind = internal.NewPropNamePropfind(
internal.GetETagName,
)
func fileInfoFromResponse(resp *internal.Response) (*FileInfo, error) {
func fileInfoFromResponse(resp *internal.Response) (*fileInfo, error) {
path, err := resp.Path()
if err != nil {
return nil, err
}
fi := &FileInfo{Path: path}
fi := FileInfo{Path: path}
var resType internal.ResourceType
if err := resp.DecodeProp(&resType); err != nil {
@ -116,10 +117,10 @@ func fileInfoFromResponse(resp *internal.Response) (*FileInfo, error) {
}
fi.ModTime = time.Time(getMod.LastModified)
return fi, nil
return &fileInfo{fi}, nil
}
func (c *Client) Stat(name string) (*FileInfo, error) {
func (c *Client) Stat(name string) (fs.FileInfo, error) {
resp, err := c.ic.PropfindFlat(name, fileInfoPropfind)
if err != nil {
return nil, err
@ -141,24 +142,24 @@ func (c *Client) Open(name string) (io.ReadCloser, error) {
return resp.Body, nil
}
func (c *Client) Readdir(name string, recursive bool) ([]FileInfo, error) {
func (c *Client) ReadDir(name string) ([]fs.DirEntry, error) {
depth := internal.DepthOne
if recursive {
/*if recursive {
depth = internal.DepthInfinity
}
}*/
ms, err := c.ic.Propfind(name, depth, fileInfoPropfind)
if err != nil {
return nil, err
}
l := make([]FileInfo, 0, len(ms.Responses))
l := make([]fs.DirEntry, 0, len(ms.Responses))
for _, resp := range ms.Responses {
fi, err := fileInfoFromResponse(&resp)
if err != nil {
return l, err
}
l = append(l, *fi)
l = append(l, fi)
}
return l, nil

View File

@ -4,6 +4,8 @@
package webdav
import (
"io/fs"
"path"
"time"
)
@ -15,3 +17,48 @@ type FileInfo struct {
MIMEType string
ETag string
}
type fileInfo struct {
FileInfo
}
var (
_ fs.FileInfo = (*fileInfo)(nil)
_ fs.DirEntry = (*fileInfo)(nil)
)
func (fi *fileInfo) Name() string {
return path.Base(fi.Path)
}
func (fi *fileInfo) Size() int64 {
return fi.FileInfo.Size
}
func (fi *fileInfo) Mode() fs.FileMode {
var mode fs.FileMode
if fi.FileInfo.IsDir {
mode |= fs.ModeDir
}
return mode
}
func (fi *fileInfo) ModTime() time.Time {
return fi.FileInfo.ModTime
}
func (fi *fileInfo) IsDir() bool {
return fi.FileInfo.IsDir
}
func (fi *fileInfo) Sys() interface{} {
return nil
}
func (fi *fileInfo) Type() fs.FileMode {
return fi.Mode()
}
func (fi *fileInfo) Info() (fs.FileInfo, error) {
return fi, nil
}