From 38f79aeaf1ac948ed7b23e8524a6f7f9612243bb Mon Sep 17 00:00:00 2001 From: vitalii Date: Thu, 3 Jan 2019 20:38:58 +0200 Subject: [PATCH] fix uploading file with wrong content. close #30 --- cmd/gowebdav/main.go | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/cmd/gowebdav/main.go b/cmd/gowebdav/main.go index cf0e945..e0f6692 100644 --- a/cmd/gowebdav/main.go +++ b/cmd/gowebdav/main.go @@ -211,32 +211,28 @@ func writeFile(path string, bytes []byte, mode os.FileMode) error { } func getStream(pathOrString string) (io.ReadCloser, error) { + fi, err := os.Stat(pathOrString) - if err == nil { - if fi.IsDir() { - return nil, &os.PathError{ - Op: "Open", - Path: pathOrString, - Err: errors.New("Path: '" + pathOrString + "' is a directory"), - } - } - f, err := os.Open(pathOrString) - if err == nil { - return f, nil - } + if err != nil { + return nil, err + } + + if fi.IsDir() { return nil, &os.PathError{ Op: "Open", Path: pathOrString, - Err: err, + Err: errors.New("Path: '" + pathOrString + "' is a directory"), } } - return nopCloser{strings.NewReader(pathOrString)}, nil -} -type nopCloser struct { - io.Reader -} + f, err := os.Open(pathOrString) + if err == nil { + return f, nil + } -func (nopCloser) Close() error { - return nil + return nil, &os.PathError{ + Op: "Open", + Path: pathOrString, + Err: err, + } }