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"
"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,
`<d:propfind xmlns:d='DAV:'>
<d:prop>
<d:displayname/>
@ -96,7 +103,7 @@ func (c *Client) List(path string) (*[]*File, error) {
</d:propfind>`,
&response{},
parse)
return &files, err
return files, err
}
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 (
"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())
}
}

View File

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

View File

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