diff --git a/client.go b/client.go index 0bdd723..079ec39 100644 --- a/client.go +++ b/client.go @@ -6,7 +6,9 @@ import ( "errors" "fmt" "net/http" + "os" "strings" + "time" ) type Client struct { @@ -24,9 +26,7 @@ func NewClient(uri string, user string, pw string) *Client { c.headers.Add("Authorization", auth) } - if !strings.HasSuffix(c.root, "/") { - c.root += "/" - } + c.root = FixSlash(c.root) return c } @@ -68,24 +68,31 @@ func getProps(r *response, status string) *props { return nil } -func (c *Client) List(path string) (*[]*File, error) { - files := make([]*File, 0) +func (c *Client) ReadDir(path string) ([]os.FileInfo, error) { + files := make([]os.FileInfo, 0) parse := func(resp interface{}) { r := resp.(*response) if p := getProps(r, "200"); p != nil { - var f File + f := new(File) + f.path = "/TODO/" + f.name = p.Name + if p.Type.Local == "collection" { - f = directory{p.Name} + f.size = 0 + f.modified = time.Unix(0, 0) + f.isdir = true } else { - f = file{p.Name, parseUint(&p.Size), parseModified(&p.Modified)} + f.size = parseInt64(&p.Size) + f.modified = parseModified(&p.Modified) + f.isdir = false } - files = append(files, &f) + files = append(files, *f) r.Props = nil } } - err := c.Propfind(path, false, + err := c.Propfind(FixSlash(path), false, ` @@ -96,7 +103,7 @@ func (c *Client) List(path string) (*[]*File, error) { `, &response{}, parse) - return &files, err + return files, err } func (c *Client) Read(path string) { diff --git a/directory.go b/directory.go deleted file mode 100644 index b198bbf..0000000 --- a/directory.go +++ /dev/null @@ -1,32 +0,0 @@ -package gowebdav - -import ( - "fmt" - "time" -) - -type Directory interface { -} - -type directory struct { - name string -} - -func (d directory) Name() string { - return d.name -} - -func (_ directory) Size() uint { - return 0 -} - -func (_ directory) IsDirectory() bool { - return true -} - -func (_ directory) Modified() time.Time { - return time.Unix(0, 9) -} -func (d directory) String() string { - return fmt.Sprintf("DIRECTORY: %s", d.name) -} diff --git a/file.go b/file.go index a37079c..d44b98b 100644 --- a/file.go +++ b/file.go @@ -2,39 +2,50 @@ package gowebdav import ( "fmt" + "os" "time" ) -type File interface { - Name() string - Size() uint - Modified() time.Time - IsDirectory() bool - String() string -} - -type file struct { +type File struct { + path string name string - size uint + size int64 modified time.Time + isdir bool } -func (_ file) IsDirectory() bool { - return false -} - -func (f file) Modified() time.Time { - return f.modified -} - -func (f file) Name() string { +func (f File) Name() string { return f.name } -func (f file) Size() uint { +func (f File) Size() int64 { return f.size } -func (f file) String() string { - return fmt.Sprintf("FILE: %s SIZE: %d MODIFIED: %s", f.name, f.size, f.modified.String()) +func (f File) Mode() os.FileMode { + if f.isdir { + return 0777 | os.ModeDir + } else { + return 0622 + } +} + +func (f File) ModTime() time.Time { + return f.modified +} + +func (f File) IsDir() bool { + return f.isdir +} + +func (f File) Sys() interface{} { + return nil +} + +func (f File) String() string { + if f.isdir { + return fmt.Sprintf("Directory: %s", f.name) + } else { + return fmt.Sprintf("File: %s SIZE: %d MODIFIED: %s", f.name, f.size, f.modified.String()) + } } diff --git a/main/client.go b/main/client.go index 728aa13..f7248bc 100644 --- a/main/client.go +++ b/main/client.go @@ -38,10 +38,10 @@ func main() { path := flag.Args()[0] switch *m { case "LIST", "PROPFIND": - if files, err := c.List(path); err == nil { - fmt.Println(len(*files)) - for _, f := range *files { - fmt.Println(*f) + if files, err := c.ReadDir(path); err == nil { + fmt.Println(len(files)) + for _, f := range files { + fmt.Println(f) } } else { fmt.Println(err) diff --git a/utils.go b/utils.go index dff9112..8246f10 100644 --- a/utils.go +++ b/utils.go @@ -9,6 +9,13 @@ import ( "time" ) +func FixSlash(s string) string { + if !strings.HasSuffix(s, "/") { + s += "/" + } + return s +} + func Join(path0 string, path1 string) string { return strings.TrimSuffix(path0, "/") + "/" + strings.TrimPrefix(path1, "/") } @@ -26,6 +33,13 @@ func parseUint(s *string) uint { return 0 } +func parseInt64(s *string) int64 { + if n, e := strconv.ParseInt(*s, 10, 64); e == nil { + return n + } + return 0 +} + func parseModified(s *string) time.Time { if t, e := time.Parse(time.RFC1123, *s); e == nil { return t