more docs / cleanup

This commit is contained in:
Aaron Bieber 2017-10-05 08:22:10 -06:00 committed by Christoph Polcin
parent b290410ef9
commit beeefa572c
4 changed files with 37 additions and 18 deletions

18
file.go
View File

@ -6,6 +6,7 @@ import (
"time" "time"
) )
// File is our structure for a given file
type File struct { type File struct {
path string path string
name string name string
@ -14,39 +15,46 @@ type File struct {
isdir bool isdir bool
} }
// Name returns the name of a file
func (f File) Name() string { func (f File) Name() string {
return f.name return f.name
} }
// Size returns the size of a file
func (f File) Size() int64 { func (f File) Size() int64 {
return f.size return f.size
} }
// Mode will return the mode of a given file
func (f File) Mode() os.FileMode { func (f File) Mode() os.FileMode {
// TODO check webdav perms // TODO check webdav perms
if f.isdir { if f.isdir {
return 0775 | os.ModeDir return 0775 | os.ModeDir
} else {
return 0664
}
} }
return 0664
}
// ModTime returns the modified time of a file
func (f File) ModTime() time.Time { func (f File) ModTime() time.Time {
return f.modified return f.modified
} }
// IsDir let us see if a given file is a directory or not
func (f File) IsDir() bool { func (f File) IsDir() bool {
return f.isdir return f.isdir
} }
// Sys ????
func (f File) Sys() interface{} { func (f File) Sys() interface{} {
return nil return nil
} }
// String lets us see file information
func (f File) String() string { func (f File) String() string {
if f.isdir { if f.isdir {
return fmt.Sprintf("Dir : '%s' - '%s'", f.path, f.name) return fmt.Sprintf("Dir : '%s' - '%s'", f.path, f.name)
} else { }
return fmt.Sprintf("File: '%s' SIZE: %d MODIFIED: %s", f.path, f.size, f.modified.String()) return fmt.Sprintf("File: '%s' SIZE: %d MODIFIED: %s", f.path, f.size, f.modified.String())
} }
}

View File

@ -33,10 +33,11 @@ func Fail(err interface{}) {
func writeFile(path string, bytes []byte, mode os.FileMode) error { func writeFile(path string, bytes []byte, mode os.FileMode) error {
f, err := os.Create(path) f, err := os.Create(path)
defer f.Close()
if err != nil { if err != nil {
return err return err
} }
defer f.Close()
_, err = f.Write(bytes) _, err = f.Write(bytes)
return err return err
} }

View File

@ -1,14 +1,13 @@
package gowebdav package gowebdav
import ( import (
"errors"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"strings" "strings"
) )
func (c *Client) req(method string, path string, body io.Reader, intercept func(*http.Request)) (req *http.Response, err error) { func (c *Client) req(method, path string, body io.Reader, intercept func(*http.Request)) (req *http.Response, err error) {
r, err := http.NewRequest(method, Join(c.root, path), body) r, err := http.NewRequest(method, Join(c.root, path), body)
if err != nil { if err != nil {
return nil, err return nil, err
@ -28,10 +27,10 @@ func (c *Client) req(method string, path string, body io.Reader, intercept func(
func (c *Client) mkcol(path string) int { func (c *Client) mkcol(path string) int {
rs, err := c.req("MKCOL", path, nil, nil) rs, err := c.req("MKCOL", path, nil, nil)
defer rs.Body.Close()
if err != nil { if err != nil {
return 400 return 400
} }
rs.Body.Close()
if rs.StatusCode == 201 || rs.StatusCode == 405 { if rs.StatusCode == 201 || rs.StatusCode == 405 {
return 201 return 201
@ -59,13 +58,13 @@ func (c *Client) propfind(path string, self bool, body string, resp interface{},
// TODO add support for 'gzip,deflate;q=0.8,q=0.7' // TODO add support for 'gzip,deflate;q=0.8,q=0.7'
rq.Header.Add("Accept-Encoding", "") rq.Header.Add("Accept-Encoding", "")
}) })
defer rs.Body.Close()
if err != nil { if err != nil {
return err return err
} }
defer rs.Body.Close()
if rs.StatusCode != 207 { if rs.StatusCode != 207 {
return errors.New(fmt.Sprintf("%s - %s %s", rs.Status, "PROPFIND", path)) return fmt.Errorf("%s - %s %s", rs.Status, "PROPFIND", path)
} }
return parseXML(rs.Body, resp, parse) return parseXML(rs.Body, resp, parse)
@ -88,9 +87,7 @@ func (c *Client) doCopyMove(method string, oldpath string, newpath string, overw
func (c *Client) copymove(method string, oldpath string, newpath string, overwrite bool) error { func (c *Client) copymove(method string, oldpath string, newpath string, overwrite bool) error {
s, data := c.doCopyMove(method, oldpath, newpath, overwrite) s, data := c.doCopyMove(method, oldpath, newpath, overwrite)
if data != nil {
defer data.Close() defer data.Close()
}
switch s { switch s {
case 201, 204: case 201, 204:
@ -109,9 +106,10 @@ func (c *Client) copymove(method string, oldpath string, newpath string, overwri
func (c *Client) put(path string, stream io.Reader) int { func (c *Client) put(path string, stream io.Reader) int {
rs, err := c.req("PUT", path, stream, nil) rs, err := c.req("PUT", path, stream, nil)
defer rs.Body.Close()
if err != nil { if err != nil {
return 400 return 400
} }
rs.Body.Close()
return rs.StatusCode return rs.StatusCode
} }

View File

@ -3,7 +3,6 @@ package gowebdav
import ( import (
"bytes" "bytes"
"encoding/xml" "encoding/xml"
"errors"
"fmt" "fmt"
"io" "io"
"os" "os"
@ -17,13 +16,22 @@ func log(msg interface{}) {
} }
func newPathError(op string, path string, statusCode int) error { func newPathError(op string, path string, statusCode int) error {
return &os.PathError{op, path, errors.New(fmt.Sprintf("%d", statusCode))} return &os.PathError{
Op: op,
Path: path,
Err: fmt.Errorf("%d", statusCode),
}
} }
func newPathErrorErr(op string, path string, err error) error { func newPathErrorErr(op string, path string, err error) error {
return &os.PathError{op, path, err} return &os.PathError{
Op: op,
Path: path,
Err: err,
}
} }
// FixSlash appends a trailing / to our string
func FixSlash(s string) string { func FixSlash(s string) string {
if !strings.HasSuffix(s, "/") { if !strings.HasSuffix(s, "/") {
s += "/" s += "/"
@ -31,6 +39,7 @@ func FixSlash(s string) string {
return s return s
} }
// FixSlashes appends and prepends a / if they are missing
func FixSlashes(s string) string { func FixSlashes(s string) string {
if s[0] != '/' { if s[0] != '/' {
s = "/" + s s = "/" + s
@ -38,13 +47,16 @@ func FixSlashes(s string) string {
return FixSlash(s) return FixSlash(s)
} }
// Join joins two paths
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, "/")
} }
// String pulls a string out of our io.Reader
func String(r io.Reader) string { func String(r io.Reader) string {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
buf.ReadFrom(r) // TODO - mkae String return an error as well
_, _ = buf.ReadFrom(r)
return buf.String() return buf.String()
} }