2018-05-16 13:52:24 +02:00
# GoWebDAV
2018-05-17 00:05:28 +02:00
[![Build Status ](https://travis-ci.org/studio-b12/gowebdav.svg?branch=master )](https://travis-ci.org/studio-b12/gowebdav)
2018-05-25 23:26:18 +02:00
[![GoDoc ](https://godoc.org/github.com/studio-b12/gowebdav?status.svg )](https://godoc.org/github.com/studio-b12/gowebdav)
2018-05-16 13:52:24 +02:00
[![Go Report Card ](https://goreportcard.com/badge/github.com/studio-b12/gowebdav )](https://goreportcard.com/report/github.com/studio-b12/gowebdav)
2018-10-21 20:49:28 +02:00
A golang WebDAV client library.
## Main features
`gowebdav` library allows to perform following actions on the remote WebDAV server:
* [create path ](#create-path-on-a-webdav-server )
* [get files list ](#get-files-list )
* [download file ](#download-file-to-byte-array )
* [upload file ](#upload-file-from-byte-array )
* [get information about specified file/folder ](#get-information-about-specified-filefolder )
* [move file to another location ](#move-file-to-another-location )
* [copy file to another location ](#copy-file-to-another-location )
* [delete file ](#delete-file )
2018-05-16 13:52:24 +02:00
## Usage
2018-10-21 20:49:28 +02:00
First of all you should create `Client` instance using `NewClient()` function:
2017-10-05 17:07:51 +02:00
2018-10-21 20:49:28 +02:00
```go
root := "https://webdav.mydomain.me"
user := "user"
password := "password"
2018-05-25 23:26:18 +02:00
2018-10-21 20:49:28 +02:00
c := gowebdav.NewClient(root, user, password)
2018-05-25 23:26:18 +02:00
```
2018-10-21 20:49:28 +02:00
After you can use this `Client` to perform actions, described below.
2018-05-25 23:26:18 +02:00
2018-10-21 20:49:28 +02:00
**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!
2018-05-25 23:26:18 +02:00
2018-10-21 20:49:28 +02:00
### Create path on a WebDAV server
```go
err := c.Mkdir("folder", 0644)
2018-05-25 23:26:18 +02:00
```
2018-10-21 20:49:28 +02:00
In case you want to create several folders you can use `c.MkdirAll()` :
```go
err := c.MkdirAll("folder/subfolder/subfolder2", 0644)
2018-05-25 23:26:18 +02:00
```
2018-10-21 20:49:28 +02:00
### Get files list
```go
files, _ := c.ReadDir("folder/subfolder")
for _, file := range files {
//notice that [file] has os.FileInfo type
fmt.Println(file.Name())
2018-05-16 13:52:24 +02:00
}
```
2018-10-21 20:49:28 +02:00
### Download file to byte array
```go
webdavFilePath := "folder/subfolder/file.txt"
localFilePath := "/tmp/webdav/file.txt"
2018-05-16 13:52:24 +02:00
2018-10-21 20:49:28 +02:00
bytes, _ := c.Read(webdavFilePath)
ioutil.WriteFile(localFilePath, bytes, 0644)
2018-05-16 13:52:24 +02:00
```
2018-10-21 20:49:28 +02:00
### Download file via reader
Also you can use `c.ReadStream()` method:
```go
webdavFilePath := "folder/subfolder/file.txt"
localFilePath := "/tmp/webdav/file.txt"
2018-05-16 13:52:24 +02:00
2018-10-21 20:49:28 +02:00
reader, _ := c.ReadStream(webdavFilePath)
2018-05-16 13:52:24 +02:00
2018-10-21 20:49:28 +02:00
file, _ := os.Create(localFilePath)
defer file.Close()
2018-05-25 23:26:18 +02:00
2018-10-21 20:49:28 +02:00
io.Copy(file, reader)
2018-05-25 23:26:18 +02:00
```
2018-10-21 20:49:28 +02:00
### Upload file from byte array
```go
webdavFilePath := "folder/subfolder/file.txt"
localFilePath := "/tmp/webdav/file.txt"
2018-05-25 23:26:18 +02:00
2018-10-21 20:49:28 +02:00
bytes, _ := ioutil.ReadFile(localFilePath)
2018-05-25 23:26:18 +02:00
2018-10-21 20:49:28 +02:00
c.Write(webdavFilePath, bytes, 0644)
2018-05-25 23:26:18 +02:00
```
2018-10-21 20:49:28 +02:00
### Upload file via writer
```go
webdavFilePath := "folder/subfolder/file.txt"
localFilePath := "/tmp/webdav/file.txt"
2018-05-16 13:52:24 +02:00
2018-10-21 20:49:28 +02:00
file, _ := os.Open(localFilePath)
defer file.Close()
2017-11-26 17:23:14 +01:00
2018-10-21 20:49:28 +02:00
c.WriteStream(webdavFilePath, file, 0644)
2017-11-26 17:23:14 +01:00
```
2018-10-21 20:49:28 +02:00
### Get information about specified file/folder
```go
webdavFilePath := "folder/subfolder/file.txt"
2018-05-16 13:52:24 +02:00
2018-10-21 20:49:28 +02:00
info := c.Stat(webdavFilePath)
//notice that [info] has os.FileInfo type
fmt.Println(info)
2018-05-16 13:52:24 +02:00
```
2018-10-21 20:49:28 +02:00
### Move file to another location
```go
oldPath := "folder/subfolder/file.txt"
newPath := "folder/subfolder/moved.txt"
isOverwrite := true
2018-05-16 13:52:24 +02:00
2018-10-21 20:49:28 +02:00
c.Rename(oldPath, newPath, isOverwrite)
2018-05-16 13:52:24 +02:00
```
2018-10-21 20:49:28 +02:00
### Copy file to another location
```go
oldPath := "folder/subfolder/file.txt"
newPath := "folder/subfolder/file-copy.txt"
isOverwrite := true
2014-10-28 07:53:14 +01:00
2018-10-21 20:49:28 +02:00
c.Copy(oldPath, newPath, isOverwrite)
2018-05-16 13:52:24 +02:00
```
2014-10-28 07:53:14 +01:00
2018-10-21 20:49:28 +02:00
### Delete file
```go
webdavFilePath := "folder/subfolder/file.txt"
2018-05-16 14:35:38 +02:00
2018-10-21 20:49:28 +02:00
c.Remove(webdavFilePath)
2018-05-25 23:26:18 +02:00
```
2018-10-21 20:49:28 +02:00
## Links
2018-05-25 23:26:18 +02:00
2018-10-21 20:49:28 +02:00
More details about WebDAV server you can read from following resources:
2018-05-25 23:26:18 +02:00
2018-10-24 13:05:51 +02:00
* [RFC 4918 - HTTP Extensions for Web Distributed Authoring and Versioning (WebDAV) ](https://tools.ietf.org/html/rfc4918 )
* [RFC 5689 - Extended MKCOL for Web Distributed Authoring and Versioning (WebDAV) ](https://tools.ietf.org/html/rfc5689 )
2018-10-21 20:49:28 +02:00
* [RFC 2616 - HTTP/1.1 Status Code Definitions ](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html "HTTP/1.1 Status Code Definitions" )
* [WebDav: Next Generation Collaborative Web Authoring By Lisa Dusseaul ](https://books.google.de/books?isbn=0130652083 "WebDav: Next Generation Collaborative Web Authoring By Lisa Dusseault" )
2018-05-25 23:26:18 +02:00
2018-10-24 13:05:51 +02:00
**NOTICE**: RFC 2518 is obsoleted by RFC 4918 in June 2007
2018-10-21 20:49:28 +02:00
## 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!
2018-05-25 23:26:18 +02:00
2018-10-21 20:49:28 +02:00
## License
This library is distributed under the BSD 3-Clause license found in the [LICENSE ](https://github.com/studio-b12/gowebdav/blob/master/LICENSE ) file.