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

16
file.go
View File

@ -6,6 +6,7 @@ import (
"time"
)
// File is our structure for a given file
type File struct {
path string
name string
@ -14,39 +15,46 @@ type File struct {
isdir bool
}
// Name returns the name of a file
func (f File) Name() string {
return f.name
}
// Size returns the size of a file
func (f File) Size() int64 {
return f.size
}
// Mode will return the mode of a given file
func (f File) Mode() os.FileMode {
// TODO check webdav perms
if f.isdir {
return 0775 | os.ModeDir
} else {
return 0664
}
return 0664
}
// ModTime returns the modified time of a file
func (f File) ModTime() time.Time {
return f.modified
}
// IsDir let us see if a given file is a directory or not
func (f File) IsDir() bool {
return f.isdir
}
// Sys ????
func (f File) Sys() interface{} {
return nil
}
// String lets us see file information
func (f File) String() string {
if f.isdir {
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 {
f, err := os.Create(path)
defer f.Close()
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(bytes)
return err
}

View File

@ -1,14 +1,13 @@
package gowebdav
import (
"errors"
"fmt"
"io"
"net/http"
"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)
if err != nil {
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 {
rs, err := c.req("MKCOL", path, nil, nil)
defer rs.Body.Close()
if err != nil {
return 400
}
rs.Body.Close()
if rs.StatusCode == 201 || rs.StatusCode == 405 {
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'
rq.Header.Add("Accept-Encoding", "")
})
defer rs.Body.Close()
if err != nil {
return err
}
defer rs.Body.Close()
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)
@ -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 {
s, data := c.doCopyMove(method, oldpath, newpath, overwrite)
if data != nil {
defer data.Close()
}
defer data.Close()
switch s {
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 {
rs, err := c.req("PUT", path, stream, nil)
defer rs.Body.Close()
if err != nil {
return 400
}
rs.Body.Close()
return rs.StatusCode
}

View File

@ -3,7 +3,6 @@ package gowebdav
import (
"bytes"
"encoding/xml"
"errors"
"fmt"
"io"
"os"
@ -17,13 +16,22 @@ func log(msg interface{}) {
}
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 {
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 {
if !strings.HasSuffix(s, "/") {
s += "/"
@ -31,6 +39,7 @@ func FixSlash(s string) string {
return s
}
// FixSlashes appends and prepends a / if they are missing
func FixSlashes(s string) string {
if s[0] != '/' {
s = "/" + s
@ -38,13 +47,16 @@ func FixSlashes(s string) string {
return FixSlash(s)
}
// Join joins two paths
func Join(path0 string, path1 string) string {
return strings.TrimSuffix(path0, "/") + "/" + strings.TrimPrefix(path1, "/")
}
// String pulls a string out of our io.Reader
func String(r io.Reader) string {
buf := new(bytes.Buffer)
buf.ReadFrom(r)
// TODO - mkae String return an error as well
_, _ = buf.ReadFrom(r)
return buf.String()
}