refactor list into ReadDir and use os.FileInfo

This commit is contained in:
Christoph Polcin 2014-10-23 13:15:02 +02:00
parent 542e8e73bc
commit 48d4d0ff90
5 changed files with 69 additions and 69 deletions

View File

@ -6,7 +6,9 @@ import (
"errors" "errors"
"fmt" "fmt"
"net/http" "net/http"
"os"
"strings" "strings"
"time"
) )
type Client struct { type Client struct {
@ -24,9 +26,7 @@ func NewClient(uri string, user string, pw string) *Client {
c.headers.Add("Authorization", auth) c.headers.Add("Authorization", auth)
} }
if !strings.HasSuffix(c.root, "/") { c.root = FixSlash(c.root)
c.root += "/"
}
return c return c
} }
@ -68,24 +68,31 @@ func getProps(r *response, status string) *props {
return nil return nil
} }
func (c *Client) List(path string) (*[]*File, error) { func (c *Client) ReadDir(path string) ([]os.FileInfo, error) {
files := make([]*File, 0) files := make([]os.FileInfo, 0)
parse := func(resp interface{}) { parse := func(resp interface{}) {
r := resp.(*response) r := resp.(*response)
if p := getProps(r, "200"); p != nil { 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" { if p.Type.Local == "collection" {
f = directory{p.Name} f.size = 0
f.modified = time.Unix(0, 0)
f.isdir = true
} else { } 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 r.Props = nil
} }
} }
err := c.Propfind(path, false, err := c.Propfind(FixSlash(path), false,
`<d:propfind xmlns:d='DAV:'> `<d:propfind xmlns:d='DAV:'>
<d:prop> <d:prop>
<d:displayname/> <d:displayname/>
@ -96,7 +103,7 @@ func (c *Client) List(path string) (*[]*File, error) {
</d:propfind>`, </d:propfind>`,
&response{}, &response{},
parse) parse)
return &files, err return files, err
} }
func (c *Client) Read(path string) { func (c *Client) Read(path string) {

View File

@ -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)
}

55
file.go
View File

@ -2,39 +2,50 @@ package gowebdav
import ( import (
"fmt" "fmt"
"os"
"time" "time"
) )
type File interface { type File struct {
Name() string path string
Size() uint
Modified() time.Time
IsDirectory() bool
String() string
}
type file struct {
name string name string
size uint size int64
modified time.Time modified time.Time
isdir bool
} }
func (_ file) IsDirectory() bool { func (f File) Name() string {
return false
}
func (f file) Modified() time.Time {
return f.modified
}
func (f file) Name() string {
return f.name return f.name
} }
func (f file) Size() uint { func (f File) Size() int64 {
return f.size return f.size
} }
func (f file) String() string { func (f File) Mode() os.FileMode {
return fmt.Sprintf("FILE: %s SIZE: %d MODIFIED: %s", f.name, f.size, f.modified.String()) 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())
}
} }

View File

@ -38,10 +38,10 @@ func main() {
path := flag.Args()[0] path := flag.Args()[0]
switch *m { switch *m {
case "LIST", "PROPFIND": case "LIST", "PROPFIND":
if files, err := c.List(path); err == nil { if files, err := c.ReadDir(path); err == nil {
fmt.Println(len(*files)) fmt.Println(len(files))
for _, f := range *files { for _, f := range files {
fmt.Println(*f) fmt.Println(f)
} }
} else { } else {
fmt.Println(err) fmt.Println(err)

View File

@ -9,6 +9,13 @@ import (
"time" "time"
) )
func FixSlash(s string) string {
if !strings.HasSuffix(s, "/") {
s += "/"
}
return s
}
func Join(path0 string, path1 string) string { func Join(path0 string, path1 string) string {
return strings.TrimSuffix(path0, "/") + "/" + strings.TrimPrefix(path1, "/") return strings.TrimSuffix(path0, "/") + "/" + strings.TrimPrefix(path1, "/")
} }
@ -26,6 +33,13 @@ func parseUint(s *string) uint {
return 0 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 { func parseModified(s *string) time.Time {
if t, e := time.Parse(time.RFC1123, *s); e == nil { if t, e := time.Parse(time.RFC1123, *s); e == nil {
return t return t