Back to Openlist

GoWebDAV

pkg/gowebdav/README.md

4.2.121.3 KB
Original Source

GoWebDAV

A golang WebDAV client library.

Main features

gowebdav library allows to perform following actions on the remote WebDAV server:

Usage

First of all you should create Client instance using NewClient() function:

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

c := gowebdav.NewClient(root, user, password)

After you can 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 path on a WebDAV server

go
err := c.Mkdir("folder", 0644)

In case you want to create several folders you can use c.MkdirAll():

go
err := c.MkdirAll("folder/subfolder/subfolder2", 0644)

Get files list

go
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

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

bytes, _ := c.Read(webdavFilePath)
ioutil.WriteFile(localFilePath, bytes, 0644)

Download file via reader

Also you can use c.ReadStream() method:

go
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

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

bytes, _ := ioutil.ReadFile(localFilePath)

c.Write(webdavFilePath, bytes, 0644)

Upload file via writer

go
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

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

info := c.Stat(webdavFilePath)
//notice that [info] has os.FileInfo type
fmt.Println(info)

Move file to another location

go
oldPath := "folder/subfolder/file.txt"
newPath := "folder/subfolder/moved.txt"
isOverwrite := true

c.Rename(oldPath, newPath, isOverwrite)

Copy file to another location

go
oldPath := "folder/subfolder/file.txt"
newPath := "folder/subfolder/file-copy.txt"
isOverwrite := true

c.Copy(oldPath, newPath, isOverwrite)

Delete file

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

c.Remove(webdavFilePath)

More details about WebDAV server you can read from following resources:

NOTICE: RFC 2518 is obsoleted by RFC 4918 in June 2007

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

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

API

import "github.com/studio-b12/gowebdav"

<a name="pkg-overview">Overview</a>

Package gowebdav is a WebDAV client library with a command line tool included.

<a name="pkg-index">Index</a>

<a name="pkg-examples">Examples</a>
<a name="pkg-files">Package files</a>

basicAuth.go client.go digestAuth.go doc.go file.go netrc.go requests.go utils.go

<a name="FixSlash">func</a> FixSlash

go
func FixSlash(s string) string

FixSlash appends a trailing / to our string

<a name="FixSlashes">func</a> FixSlashes

go
func FixSlashes(s string) string

FixSlashes appends and prepends a / if they are missing

<a name="Join">func</a> Join

go
func Join(path0 string, path1 string) string

Join joins two paths

<a name="PathEscape">func</a> PathEscape

go
func PathEscape(path string) string

PathEscape escapes all segments of a given path

<a name="ReadConfig">func</a> ReadConfig

go
func ReadConfig(uri, netrc string) (string, string)

ReadConfig reads login and password configuration from ~/.netrc machine foo.com login username password 123456

<a name="String">func</a> String

go
func String(r io.Reader) string

String pulls a string out of our io.Reader

<a name="Authenticator">type</a> Authenticator

go
type Authenticator interface {
    Type() string
    User() string
    Pass() string
    Authorize(*http.Request, string, string)
}

Authenticator stub

<a name="BasicAuth">type</a> BasicAuth

go
type BasicAuth struct {
    // contains filtered or unexported fields
}

BasicAuth structure holds our credentials

<a name="BasicAuth.Authorize">func</a> (*BasicAuth) Authorize

go
func (b *BasicAuth) Authorize(req *http.Request, method string, path string)

Authorize the current request

<a name="BasicAuth.Pass">func</a> (*BasicAuth) Pass

go
func (b *BasicAuth) Pass() string

Pass holds the BasicAuth password

<a name="BasicAuth.Type">func</a> (*BasicAuth) Type

go
func (b *BasicAuth) Type() string

Type identifies the BasicAuthenticator

<a name="BasicAuth.User">func</a> (*BasicAuth) User

go
func (b *BasicAuth) User() string

User holds the BasicAuth username

<a name="Client">type</a> Client

go
type Client struct {
    // contains filtered or unexported fields
}

Client defines our structure

<a name="NewClient">func</a> NewClient

go
func NewClient(uri, user, pw string) *Client

NewClient creates a new instance of client

<a name="Client.Connect">func</a> (*Client) Connect

go
func (c *Client) Connect() error

Connect connects to our dav server

<a name="Client.Copy">func</a> (*Client) Copy

go
func (c *Client) Copy(oldpath, newpath string, overwrite bool) error

Copy copies a file from A to B

<a name="Client.Mkdir">func</a> (*Client) Mkdir

go
func (c *Client) Mkdir(path string, _ os.FileMode) error

Mkdir makes a directory

<a name="Client.MkdirAll">func</a> (*Client) MkdirAll

go
func (c *Client) MkdirAll(path string, _ os.FileMode) error

MkdirAll like mkdir -p, but for webdav

<a name="Client.Read">func</a> (*Client) Read

go
func (c *Client) Read(path string) ([]byte, error)

Read reads the contents of a remote file

<a name="Client.ReadDir">func</a> (*Client) ReadDir

go
func (c *Client) ReadDir(path string) ([]os.FileInfo, error)

ReadDir reads the contents of a remote directory

<a name="Client.ReadStream">func</a> (*Client) ReadStream

go
func (c *Client) ReadStream(path string) (io.ReadCloser, error)

ReadStream reads the stream for a given path

<a name="Client.ReadStreamRange">func</a> (*Client) ReadStreamRange

go
func (c *Client) ReadStreamRange(path string, offset, length int64) (io.ReadCloser, error)

ReadStreamRange reads the stream representing a subset of bytes for a given path, utilizing HTTP Range Requests if the server supports it. The range is expressed as offset from the start of the file and length, for example offset=10, length=10 will return bytes 10 through 19.

If the server does not support partial content requests and returns full content instead, this function will emulate the behavior by skipping offset bytes and limiting the result to length.

<a name="Client.Remove">func</a> (*Client) Remove

go
func (c *Client) Remove(path string) error

Remove removes a remote file

<a name="Client.RemoveAll">func</a> (*Client) RemoveAll

go
func (c *Client) RemoveAll(path string) error

RemoveAll removes remote files

<a name="Client.Rename">func</a> (*Client) Rename

go
func (c *Client) Rename(oldpath, newpath string, overwrite bool) error

Rename moves a file from A to B

<a name="Client.SetHeader">func</a> (*Client) SetHeader

go
func (c *Client) SetHeader(key, value string)

SetHeader lets us set arbitrary headers for a given client

<a name="Client.SetInterceptor">func</a> (*Client) SetInterceptor

go
func (c *Client) SetInterceptor(interceptor func(method string, rq *http.Request))

SetInterceptor lets us set an arbitrary interceptor for a given client

<a name="Client.SetTimeout">func</a> (*Client) SetTimeout

go
func (c *Client) SetTimeout(timeout time.Duration)

SetTimeout exposes the ability to set a time limit for requests

<a name="Client.SetTransport">func</a> (*Client) SetTransport

go
func (c *Client) SetTransport(transport http.RoundTripper)

SetTransport exposes the ability to define custom transports

<a name="Client.Stat">func</a> (*Client) Stat

go
func (c *Client) Stat(path string) (os.FileInfo, error)

Stat returns the file stats for a specified path

<a name="Client.Write">func</a> (*Client) Write

go
func (c *Client) Write(path string, data []byte, _ os.FileMode) error

Write writes data to a given path

<a name="Client.WriteStream">func</a> (*Client) WriteStream

go
func (c *Client) WriteStream(path string, stream io.Reader, _ os.FileMode) error

WriteStream writes a stream

<a name="DigestAuth">type</a> DigestAuth

go
type DigestAuth struct {
    // contains filtered or unexported fields
}

DigestAuth structure holds our credentials

<a name="DigestAuth.Authorize">func</a> (*DigestAuth) Authorize

go
func (d *DigestAuth) Authorize(req *http.Request, method string, path string)

Authorize the current request

<a name="DigestAuth.Pass">func</a> (*DigestAuth) Pass

go
func (d *DigestAuth) Pass() string

Pass holds the DigestAuth password

<a name="DigestAuth.Type">func</a> (*DigestAuth) Type

go
func (d *DigestAuth) Type() string

Type identifies the DigestAuthenticator

<a name="DigestAuth.User">func</a> (*DigestAuth) User

go
func (d *DigestAuth) User() string

User holds the DigestAuth username

<a name="File">type</a> File

go
type File struct {
    // contains filtered or unexported fields
}

File is our structure for a given file

<a name="File.ContentType">func</a> (File) ContentType

go
func (f File) ContentType() string

ContentType returns the content type of a file

<a name="File.ETag">func</a> (File) ETag

go
func (f File) ETag() string

ETag returns the ETag of a file

<a name="File.IsDir">func</a> (File) IsDir

go
func (f File) IsDir() bool

IsDir let us see if a given file is a directory or not

<a name="File.ModTime">func</a> (File) ModTime

go
func (f File) ModTime() time.Time

ModTime returns the modified time of a file

<a name="File.Mode">func</a> (File) Mode

go
func (f File) Mode() os.FileMode

Mode will return the mode of a given file

<a name="File.Name">func</a> (File) Name

go
func (f File) Name() string

Name returns the name of a file

<a name="File.Path">func</a> (File) Path

go
func (f File) Path() string

Path returns the full path of a file

<a name="File.Size">func</a> (File) Size

go
func (f File) Size() int64

Size returns the size of a file

<a name="File.String">func</a> (File) String

go
func (f File) String() string

String lets us see file information

<a name="File.Sys">func</a> (File) Sys

go
func (f File) Sys() interface{}

Sys ????

<a name="NoAuth">type</a> NoAuth

go
type NoAuth struct {
    // contains filtered or unexported fields
}

NoAuth structure holds our credentials

<a name="NoAuth.Authorize">func</a> (*NoAuth) Authorize

go
func (n *NoAuth) Authorize(req *http.Request, method string, path string)

Authorize the current request

<a name="NoAuth.Pass">func</a> (*NoAuth) Pass

go
func (n *NoAuth) Pass() string

Pass returns the current password

<a name="NoAuth.Type">func</a> (*NoAuth) Type

go
func (n *NoAuth) Type() string

Type identifies the authenticator

<a name="NoAuth.User">func</a> (*NoAuth) User

go
func (n *NoAuth) User() string

User returns the current user


Generated by godoc2md