Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: switch to io/fs #72

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package webdav
import (
"fmt"
"io"
"io/fs"
"net/http"
"time"

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
47 changes: 47 additions & 0 deletions webdav.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package webdav

import (
"io/fs"
"path"
"time"
)

Expand All @@ -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
}