gowebdav

package module
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 13, 2026 License: BSD-3-Clause Imports: 13 Imported by: 1

README

GoWebDAV

GoDoc Go Report Card Build Coverage Issues

A golang WebDAV client library with a command line tool included.

Main features

This API allows the following actions on remote WebDAV servers:

Get started

go get github.com/rickb777/gowebdav

Usage

Start by creating a Client instance using the NewClient() function:

root := "https://webdav.mydomain.me"
user := "user"
password := "password"

c := gowebdav.NewClient(root)

Then use this Client to perform actions described below.

NOTICE: we will not check errors in examples, to focus you on the gowebdav library's code, but you should do it in your code.

Create folders on a WebDAV server
err := c.Mkdir("folder", 0644)

If you want to create several nested folders, you can use c.MkdirAll():

err := c.MkdirAll("folder/subfolder/subfolder2", 0644)
List files
files, _ := c.ReadDir("folder/subfolder")
for _, file := range files {
    //notice that [file] has os.FileInfo type
    fmt.Println(file.Name())
}
Download file to byte array
webdavFilePath := "folder/subfolder/file.txt"
localFilePath := "/tmp/webdav/file.txt"

bytes, _ := c.ReadFile(webdavFilePath)
ioutil.WriteFile(localFilePath, bytes, 0644)
Download file via reader

Alternatively, use the c.ReadStream() method:

webdavFilePath := "folder/subfolder/file.txt"
localFilePath := "/tmp/webdav/file.txt"

reader, _ := c.ReadStream(webdavFilePath)

file, _ := os.Create(localFilePath)
defer file.Close()

io.Copy(file, reader)
Upload file from byte array
webdavFilePath := "folder/subfolder/file.txt"
localFilePath := "/tmp/webdav/file.txt"

bytes, _ := ioutil.ReadFile(localFilePath)

c.WriteFile(webdavFilePath, bytes, 0644)
Upload file via writer
webdavFilePath := "folder/subfolder/file.txt"
localFilePath := "/tmp/webdav/file.txt"

file, _ := os.Open(localFilePath)
defer file.Close()

c.WriteStream(webdavFilePath, file, 0644)
Get information about specified file/folder
webdavFilePath := "folder/subfolder/file.txt"

info := c.Stat(webdavFilePath)
//notice that [info] has os.FileInfo type
fmt.Println(info)
Move file to another location
oldPath := "folder/subfolder/file.txt"
newPath := "folder/subfolder/moved.txt"
isOverwrite := true

c.Rename(oldPath, newPath)

or use RenameWithoutOverwriting(oldpath, newpath string).

Copy file to another location
oldPath := "folder/subfolder/file.txt"
newPath := "folder/subfolder/file-copy.txt"
isOverwrite := true

c.Copy(oldPath, newPath)

or use CopyWithoutOverwriting(oldpath, newpath string) error.

Delete file
webdavFilePath := "folder/subfolder/file.txt"

c.Remove(webdavFilePath)

Command-Line Tool

See cmd/gowebdav.

You can read more details about WebDAV from the following resources:

Contributing

All contributing are welcome. If you have any suggestions or find some bug, please create an Issue to let us make this project better. We appreciate your help!

License & Acknowledgement

This library is distributed under the BSD 3-Clause license found in the LICENSE file.

My thanks to

  • Studio-b12: did much of the original work - see gowebdav.
  • Andrew Koltyakov: wrote Gosip, on which the SAML authentication used here is based.

Documentation

Overview

Package gowebdav is a WebDAV client library.

Index

Constants

View Source
const (
	MethodMove     = "MOVE"
	MethodCopy     = "COPY"
	MethodMkcol    = "MKCOL"
	MethodPropfind = "PROPFIND"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	// Ping tests the connection to the webdav server.
	Ping() error

	// ReadDir reads the contents of a remote directory
	ReadDir(path string) ([]os.FileInfo, error)

	// Copy copies a file from oldpath to newpath.
	// If newpath already exists and is not a directory, Copy overwrites it.
	Copy(oldpath, newpath string) error

	// CopyWithoutOverwriting copies a file from oldpath to newpath.
	CopyWithoutOverwriting(oldpath, newpath string) error

	// ReadFile reads the contents of a remote file.
	ReadFile(path string) ([]byte, error)

	// ReadStream reads the stream for a given path. The caller must
	// close the returned io.ReadCloser.
	ReadStream(path string) (io.ReadCloser, error)

	// WriteFile writes data to a given path on the webdav server.
	WriteFile(path string, data []byte, contentType string) error

	// WriteStream writes from a stream to a resource on the webdav server.
	WriteStream(path string, stream io.Reader, contentType string) error

	// Mkdir makes a directory (also known as a collection in Webdav)
	Mkdir(path string, perm os.FileMode) error

	// MkdirAll creates a directory path and all parents that do not exist yet.
	MkdirAll(path string, perm os.FileMode) error

	// Remove removes a remote file
	Remove(path string) error

	// RemoveAll removes remote files
	RemoveAll(path string) error

	// Rename renames (moves) oldpath to newpath.
	// If newpath already exists and is not a directory, Rename replaces it.
	Rename(oldname, newname string) error

	// RenameWithoutOverwriting renames (moves) oldpath to newpath.
	// If newpath already exists, a *os.PathError error is returned
	// containing the message "file already exists".
	RenameWithoutOverwriting(oldpath, newpath string) error

	// Stat returns a FileInfo describing the named file, or an error, if any happens.
	Stat(path string) (os.FileInfo, error)

	// The name of this FileSystem.
	Name() string
}

Client is compatible with Afero.Fs. https://pkg.go.dev/github.com/spf13/afero#Fs

func NewClient

func NewClient(uri string, opts ...ClientOpt) Client

NewClient creates a new Client. By default, this uses the default HTTP client.

type ClientOpt

type ClientOpt func(Client)

func AddHeader

func AddHeader(key, value string) ClientOpt

AddHeader lets us set arbitrary headers for a given client

func SetAuthentication

func SetAuthentication(authenticator auth.Authenticator) ClientOpt

SetAuthentication sets the authentication credentials and method. Leave the authenticator method blank to allow HTTP challenges to select an appropriate method. Otherwise it should be "basic".

func SetHttpClient

func SetHttpClient(httpClient HttpClient) ClientOpt

SetHttpClient changes the http.Client. This allows control over the http.Transport, timeouts etc.

type HttpClient

type HttpClient interface {
	Do(req *http.Request) (*http.Response, error)
}

Directories

Path Synopsis
cmd
gowebdav command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL