2014-10-23 10:39:55 +02:00
|
|
|
package gowebdav
|
|
|
|
|
|
|
|
import (
|
2014-10-27 14:32:16 +01:00
|
|
|
"bytes"
|
2014-10-23 10:39:55 +02:00
|
|
|
"encoding/xml"
|
2021-11-05 07:24:19 +01:00
|
|
|
"fmt"
|
2014-10-27 15:17:54 +01:00
|
|
|
"io"
|
2024-01-20 11:59:18 +01:00
|
|
|
"log"
|
2014-10-23 10:39:55 +02:00
|
|
|
"net/http"
|
2021-02-03 21:16:30 +01:00
|
|
|
"net/url"
|
2014-10-23 13:15:02 +02:00
|
|
|
"os"
|
2021-02-03 21:16:30 +01:00
|
|
|
pathpkg "path"
|
2014-10-23 10:39:55 +02:00
|
|
|
"strings"
|
2014-10-23 13:15:02 +02:00
|
|
|
"time"
|
2014-10-23 10:39:55 +02:00
|
|
|
)
|
|
|
|
|
2023-02-03 10:18:35 +01:00
|
|
|
const XInhibitRedirect = "X-Gowebdav-Inhibit-Redirect"
|
|
|
|
|
2017-10-05 16:07:43 +02:00
|
|
|
// Client defines our structure
|
2014-10-23 10:39:55 +02:00
|
|
|
type Client struct {
|
2021-04-27 20:16:33 +02:00
|
|
|
root string
|
|
|
|
headers http.Header
|
|
|
|
interceptor func(method string, rq *http.Request)
|
|
|
|
c *http.Client
|
2023-02-03 10:18:35 +01:00
|
|
|
auth Authorizer
|
2018-04-07 15:37:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient creates a new instance of client
|
|
|
|
func NewClient(uri, user, pw string) *Client {
|
2024-01-20 11:59:18 +01:00
|
|
|
log.Printf("NewClient(%s, %s, %s)", uri, user, pw)
|
2023-02-03 10:18:35 +01:00
|
|
|
return NewAuthClient(uri, NewAutoAuth(user, pw))
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewAuthClient creates a new client instance with a custom Authorizer
|
|
|
|
func NewAuthClient(uri string, auth Authorizer) *Client {
|
2024-01-20 11:59:18 +01:00
|
|
|
log.Printf("NewAuthClient(%s)", uri)
|
2023-02-03 10:18:35 +01:00
|
|
|
c := &http.Client{
|
|
|
|
CheckRedirect: func(rq *http.Request, via []*http.Request) error {
|
|
|
|
if len(via) >= 10 {
|
|
|
|
return ErrTooManyRedirects
|
|
|
|
}
|
|
|
|
if via[0].Header.Get(XInhibitRedirect) != "" {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2023-06-07 17:23:35 +02:00
|
|
|
return &Client{root: FixSlash(uri), headers: make(http.Header), interceptor: nil, c: c, auth: auth}
|
2014-10-23 10:39:55 +02:00
|
|
|
}
|
|
|
|
|
2018-05-16 12:35:57 +02:00
|
|
|
// SetHeader lets us set arbitrary headers for a given client
|
2016-07-27 14:36:21 +02:00
|
|
|
func (c *Client) SetHeader(key, value string) {
|
2024-01-20 11:59:18 +01:00
|
|
|
log.Printf("Client.SetHeader(%s, %s)", key, value)
|
2016-07-27 14:36:21 +02:00
|
|
|
c.headers.Add(key, value)
|
|
|
|
}
|
|
|
|
|
2021-04-27 20:16:33 +02:00
|
|
|
// SetInterceptor lets us set an arbitrary interceptor for a given client
|
|
|
|
func (c *Client) SetInterceptor(interceptor func(method string, rq *http.Request)) {
|
2024-01-20 11:59:18 +01:00
|
|
|
log.Printf("Client.SetInterceptor()")
|
2021-04-27 20:16:33 +02:00
|
|
|
c.interceptor = interceptor
|
|
|
|
}
|
|
|
|
|
2017-10-05 16:07:43 +02:00
|
|
|
// SetTimeout exposes the ability to set a time limit for requests
|
2018-05-15 11:51:53 +02:00
|
|
|
func (c *Client) SetTimeout(timeout time.Duration) {
|
2024-01-20 11:59:18 +01:00
|
|
|
log.Printf("Client.SetTimeout()")
|
2018-05-15 11:51:53 +02:00
|
|
|
c.c.Timeout = timeout
|
|
|
|
}
|
|
|
|
|
2017-10-05 16:07:43 +02:00
|
|
|
// SetTransport exposes the ability to define custom transports
|
2017-08-07 12:13:13 +02:00
|
|
|
func (c *Client) SetTransport(transport http.RoundTripper) {
|
2024-01-20 11:59:18 +01:00
|
|
|
log.Printf("Client.SetTransport()")
|
2017-08-07 12:13:13 +02:00
|
|
|
c.c.Transport = transport
|
|
|
|
}
|
|
|
|
|
2022-11-09 16:48:38 +01:00
|
|
|
// SetJar exposes the ability to set a cookie jar to the client.
|
|
|
|
func (c *Client) SetJar(jar http.CookieJar) {
|
2024-01-20 11:59:18 +01:00
|
|
|
log.Printf("Client.SetJar()")
|
2022-11-09 16:48:38 +01:00
|
|
|
c.c.Jar = jar
|
|
|
|
}
|
|
|
|
|
2017-10-05 16:07:43 +02:00
|
|
|
// Connect connects to our dav server
|
2014-10-23 10:39:55 +02:00
|
|
|
func (c *Client) Connect() error {
|
2024-01-20 11:59:18 +01:00
|
|
|
log.Printf("Client.Connect()")
|
2014-10-24 12:40:49 +02:00
|
|
|
rs, err := c.options("/")
|
2017-10-05 16:07:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-10-23 10:39:55 +02:00
|
|
|
|
2017-10-05 16:07:43 +02:00
|
|
|
err = rs.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-20 02:21:30 +02:00
|
|
|
if rs.StatusCode != 200 {
|
2023-02-03 10:18:35 +01:00
|
|
|
return NewPathError("Connect", c.root, rs.StatusCode)
|
2017-10-05 16:07:43 +02:00
|
|
|
}
|
2014-10-23 10:39:55 +02:00
|
|
|
|
2017-10-05 16:07:43 +02:00
|
|
|
return nil
|
2014-10-23 10:39:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type props struct {
|
2017-11-26 17:23:14 +01:00
|
|
|
Status string `xml:"DAV: status"`
|
|
|
|
Name string `xml:"DAV: prop>displayname,omitempty"`
|
|
|
|
Type xml.Name `xml:"DAV: prop>resourcetype>collection,omitempty"`
|
|
|
|
Size string `xml:"DAV: prop>getcontentlength,omitempty"`
|
|
|
|
ContentType string `xml:"DAV: prop>getcontenttype,omitempty"`
|
|
|
|
ETag string `xml:"DAV: prop>getetag,omitempty"`
|
|
|
|
Modified string `xml:"DAV: prop>getlastmodified,omitempty"`
|
2014-10-23 10:39:55 +02:00
|
|
|
}
|
2016-10-21 10:34:34 +02:00
|
|
|
|
2014-10-23 10:39:55 +02:00
|
|
|
type response struct {
|
|
|
|
Href string `xml:"DAV: href"`
|
|
|
|
Props []props `xml:"DAV: propstat"`
|
|
|
|
}
|
|
|
|
|
2014-10-23 11:28:46 +02:00
|
|
|
func getProps(r *response, status string) *props {
|
2024-01-20 12:11:08 +01:00
|
|
|
log.Printf("getProps()")
|
2014-10-23 10:39:55 +02:00
|
|
|
for _, prop := range r.Props {
|
2017-10-05 16:07:43 +02:00
|
|
|
if strings.Contains(prop.Status, status) {
|
2014-10-23 10:39:55 +02:00
|
|
|
return &prop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-10-05 16:07:43 +02:00
|
|
|
// ReadDir reads the contents of a remote directory
|
2014-10-23 13:15:02 +02:00
|
|
|
func (c *Client) ReadDir(path string) ([]os.FileInfo, error) {
|
2024-01-20 11:59:18 +01:00
|
|
|
log.Printf("Client.ReadDir(%s)", path)
|
2014-10-23 14:26:08 +02:00
|
|
|
path = FixSlashes(path)
|
2014-10-23 13:15:02 +02:00
|
|
|
files := make([]os.FileInfo, 0)
|
2014-10-23 13:38:49 +02:00
|
|
|
skipSelf := true
|
2014-10-24 12:39:35 +02:00
|
|
|
parse := func(resp interface{}) error {
|
2014-10-23 10:39:55 +02:00
|
|
|
r := resp.(*response)
|
2014-10-23 13:38:49 +02:00
|
|
|
|
|
|
|
if skipSelf {
|
|
|
|
skipSelf = false
|
2014-10-24 12:39:35 +02:00
|
|
|
if p := getProps(r, "200"); p != nil && p.Type.Local == "collection" {
|
|
|
|
r.Props = nil
|
|
|
|
return nil
|
|
|
|
}
|
2023-02-03 10:18:35 +01:00
|
|
|
return NewPathError("ReadDir", path, 405)
|
2014-10-23 13:38:49 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 10:39:55 +02:00
|
|
|
if p := getProps(r, "200"); p != nil {
|
2014-10-23 13:15:02 +02:00
|
|
|
f := new(File)
|
2021-02-03 21:16:30 +01:00
|
|
|
if ps, err := url.PathUnescape(r.Href); err == nil {
|
|
|
|
f.name = pathpkg.Base(ps)
|
|
|
|
} else {
|
|
|
|
f.name = p.Name
|
|
|
|
}
|
2014-10-23 13:38:16 +02:00
|
|
|
f.path = path + f.name
|
2015-12-09 10:02:37 +01:00
|
|
|
f.modified = parseModified(&p.Modified)
|
2017-11-26 17:23:14 +01:00
|
|
|
f.etag = p.ETag
|
|
|
|
f.contentType = p.ContentType
|
2014-10-23 13:15:02 +02:00
|
|
|
|
2014-10-23 10:39:55 +02:00
|
|
|
if p.Type.Local == "collection" {
|
2014-10-23 13:38:16 +02:00
|
|
|
f.path += "/"
|
2014-10-23 13:15:02 +02:00
|
|
|
f.size = 0
|
|
|
|
f.isdir = true
|
2014-10-23 10:39:55 +02:00
|
|
|
} else {
|
2014-10-23 13:15:02 +02:00
|
|
|
f.size = parseInt64(&p.Size)
|
|
|
|
f.isdir = false
|
2014-10-23 10:39:55 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 13:15:02 +02:00
|
|
|
files = append(files, *f)
|
2014-10-23 10:39:55 +02:00
|
|
|
}
|
2014-10-23 13:38:16 +02:00
|
|
|
|
|
|
|
r.Props = nil
|
2014-10-24 12:39:35 +02:00
|
|
|
return nil
|
2014-10-23 10:39:55 +02:00
|
|
|
}
|
|
|
|
|
2014-10-23 15:31:34 +02:00
|
|
|
err := c.propfind(path, false,
|
2014-10-23 10:39:55 +02:00
|
|
|
`<d:propfind xmlns:d='DAV:'>
|
|
|
|
<d:prop>
|
|
|
|
<d:displayname/>
|
|
|
|
<d:resourcetype/>
|
|
|
|
<d:getcontentlength/>
|
2017-11-26 17:23:14 +01:00
|
|
|
<d:getcontenttype/>
|
|
|
|
<d:getetag/>
|
2014-10-23 10:39:55 +02:00
|
|
|
<d:getlastmodified/>
|
|
|
|
</d:prop>
|
|
|
|
</d:propfind>`,
|
|
|
|
&response{},
|
|
|
|
parse)
|
2014-10-24 12:39:35 +02:00
|
|
|
|
2014-10-24 11:38:11 +02:00
|
|
|
if err != nil {
|
2014-10-24 12:39:35 +02:00
|
|
|
if _, ok := err.(*os.PathError); !ok {
|
2023-02-03 10:18:35 +01:00
|
|
|
err = NewPathErrorErr("ReadDir", path, err)
|
2014-10-24 12:39:35 +02:00
|
|
|
}
|
2014-10-24 11:38:11 +02:00
|
|
|
}
|
2014-10-23 13:15:02 +02:00
|
|
|
return files, err
|
2014-10-23 10:39:55 +02:00
|
|
|
}
|
|
|
|
|
2017-10-05 16:07:43 +02:00
|
|
|
// Stat returns the file stats for a specified path
|
2014-10-27 17:02:28 +01:00
|
|
|
func (c *Client) Stat(path string) (os.FileInfo, error) {
|
2024-01-20 11:59:18 +01:00
|
|
|
log.Printf("Client.Stat(%s)", path)
|
2017-10-05 16:07:43 +02:00
|
|
|
var f *File
|
2014-10-27 17:02:28 +01:00
|
|
|
parse := func(resp interface{}) error {
|
|
|
|
r := resp.(*response)
|
|
|
|
if p := getProps(r, "200"); p != nil && f == nil {
|
|
|
|
f = new(File)
|
|
|
|
f.name = p.Name
|
|
|
|
f.path = path
|
2017-11-26 17:23:14 +01:00
|
|
|
f.etag = p.ETag
|
|
|
|
f.contentType = p.ContentType
|
2014-10-27 17:02:28 +01:00
|
|
|
|
|
|
|
if p.Type.Local == "collection" {
|
|
|
|
if !strings.HasSuffix(f.path, "/") {
|
|
|
|
f.path += "/"
|
|
|
|
}
|
|
|
|
f.size = 0
|
2023-06-14 11:14:44 +02:00
|
|
|
f.modified = parseModified(&p.Modified)
|
2014-10-27 17:02:28 +01:00
|
|
|
f.isdir = true
|
|
|
|
} else {
|
|
|
|
f.size = parseInt64(&p.Size)
|
|
|
|
f.modified = parseModified(&p.Modified)
|
|
|
|
f.isdir = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r.Props = nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
err := c.propfind(path, true,
|
|
|
|
`<d:propfind xmlns:d='DAV:'>
|
|
|
|
<d:prop>
|
|
|
|
<d:displayname/>
|
|
|
|
<d:resourcetype/>
|
|
|
|
<d:getcontentlength/>
|
2017-11-26 17:23:14 +01:00
|
|
|
<d:getcontenttype/>
|
|
|
|
<d:getetag/>
|
2014-10-27 17:02:28 +01:00
|
|
|
<d:getlastmodified/>
|
|
|
|
</d:prop>
|
|
|
|
</d:propfind>`,
|
|
|
|
&response{},
|
|
|
|
parse)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(*os.PathError); !ok {
|
2023-02-03 10:18:35 +01:00
|
|
|
err = NewPathErrorErr("ReadDir", path, err)
|
2014-10-27 17:02:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return f, err
|
|
|
|
}
|
|
|
|
|
2017-10-05 16:07:43 +02:00
|
|
|
// Remove removes a remote file
|
2014-10-23 14:10:31 +02:00
|
|
|
func (c *Client) Remove(path string) error {
|
2024-01-20 11:59:18 +01:00
|
|
|
log.Printf("Client.Remove(%s)", path)
|
2014-10-24 12:01:54 +02:00
|
|
|
return c.RemoveAll(path)
|
|
|
|
}
|
|
|
|
|
2017-10-05 16:07:43 +02:00
|
|
|
// RemoveAll removes remote files
|
2014-10-24 12:01:54 +02:00
|
|
|
func (c *Client) RemoveAll(path string) error {
|
2024-01-20 11:59:18 +01:00
|
|
|
log.Printf("Client.RemoveAll(%s)", path)
|
2014-10-27 16:19:17 +01:00
|
|
|
rs, err := c.req("DELETE", path, nil, nil)
|
2014-10-23 14:10:31 +02:00
|
|
|
if err != nil {
|
2023-02-03 10:18:35 +01:00
|
|
|
return NewPathError("Remove", path, 400)
|
2014-10-23 14:10:31 +02:00
|
|
|
}
|
2017-10-05 16:07:43 +02:00
|
|
|
err = rs.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-10-23 14:10:31 +02:00
|
|
|
|
2018-05-15 11:51:59 +02:00
|
|
|
if rs.StatusCode == 200 || rs.StatusCode == 204 || rs.StatusCode == 404 {
|
2014-10-23 14:10:31 +02:00
|
|
|
return nil
|
|
|
|
}
|
2017-10-05 16:07:43 +02:00
|
|
|
|
2023-02-03 10:18:35 +01:00
|
|
|
return NewPathError("Remove", path, rs.StatusCode)
|
2014-10-23 14:10:31 +02:00
|
|
|
}
|
|
|
|
|
2017-10-05 16:07:43 +02:00
|
|
|
// Mkdir makes a directory
|
2022-01-28 17:20:35 +01:00
|
|
|
func (c *Client) Mkdir(path string, _ os.FileMode) (err error) {
|
2024-01-20 11:59:18 +01:00
|
|
|
log.Printf("Client.Mkdir(%s)", path)
|
2014-10-23 15:00:20 +02:00
|
|
|
path = FixSlashes(path)
|
2022-01-28 17:20:35 +01:00
|
|
|
status, err := c.mkcol(path)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-10-23 15:00:20 +02:00
|
|
|
if status == 201 {
|
|
|
|
return nil
|
|
|
|
}
|
2017-10-05 16:07:43 +02:00
|
|
|
|
2023-02-03 10:18:35 +01:00
|
|
|
return NewPathError("Mkdir", path, status)
|
2014-10-23 15:00:20 +02:00
|
|
|
}
|
|
|
|
|
2017-10-05 16:07:43 +02:00
|
|
|
// MkdirAll like mkdir -p, but for webdav
|
2022-01-28 17:20:35 +01:00
|
|
|
func (c *Client) MkdirAll(path string, _ os.FileMode) (err error) {
|
2024-01-20 11:59:18 +01:00
|
|
|
log.Printf("Client.MkdirAll(%s)", path)
|
2014-10-24 11:31:16 +02:00
|
|
|
path = FixSlashes(path)
|
2022-01-28 17:20:35 +01:00
|
|
|
status, err := c.mkcol(path)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-10-24 11:31:16 +02:00
|
|
|
if status == 201 {
|
|
|
|
return nil
|
2022-01-28 17:20:35 +01:00
|
|
|
}
|
|
|
|
if status == 409 {
|
2014-10-24 11:31:16 +02:00
|
|
|
paths := strings.Split(path, "/")
|
|
|
|
sub := "/"
|
|
|
|
for _, e := range paths {
|
|
|
|
if e == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
sub += e + "/"
|
2022-01-28 17:20:35 +01:00
|
|
|
status, err = c.mkcol(sub)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-10-24 11:31:16 +02:00
|
|
|
if status != 201 {
|
2023-02-03 10:18:35 +01:00
|
|
|
return NewPathError("MkdirAll", sub, status)
|
2014-10-24 11:31:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-03 10:18:35 +01:00
|
|
|
return NewPathError("MkdirAll", path, status)
|
2014-10-24 11:31:16 +02:00
|
|
|
}
|
2014-10-24 11:30:08 +02:00
|
|
|
|
2017-10-05 16:07:43 +02:00
|
|
|
// Rename moves a file from A to B
|
2018-05-23 14:15:46 +02:00
|
|
|
func (c *Client) Rename(oldpath, newpath string, overwrite bool) error {
|
2024-01-20 11:59:18 +01:00
|
|
|
log.Printf("Client.Rename(%s, %s)", oldpath, newpath)
|
2014-10-24 14:08:42 +02:00
|
|
|
return c.copymove("MOVE", oldpath, newpath, overwrite)
|
|
|
|
}
|
|
|
|
|
2017-10-05 16:07:43 +02:00
|
|
|
// Copy copies a file from A to B
|
2018-05-23 14:15:46 +02:00
|
|
|
func (c *Client) Copy(oldpath, newpath string, overwrite bool) error {
|
2024-01-20 11:59:18 +01:00
|
|
|
log.Printf("Client.Copy(%s, %s)", oldpath, newpath)
|
2014-10-24 14:08:42 +02:00
|
|
|
return c.copymove("COPY", oldpath, newpath, overwrite)
|
|
|
|
}
|
|
|
|
|
2017-10-05 16:07:43 +02:00
|
|
|
// Read reads the contents of a remote file
|
2014-10-27 14:32:16 +01:00
|
|
|
func (c *Client) Read(path string) ([]byte, error) {
|
2024-01-20 11:59:18 +01:00
|
|
|
log.Printf("Client.Read(%s)", path)
|
2017-10-05 16:07:43 +02:00
|
|
|
var stream io.ReadCloser
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if stream, err = c.ReadStream(path); err != nil {
|
2014-10-27 15:17:54 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-05-17 01:17:14 +02:00
|
|
|
defer stream.Close()
|
2017-10-05 16:07:43 +02:00
|
|
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
_, err = buf.ReadFrom(stream)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
2014-10-27 15:17:54 +01:00
|
|
|
}
|
|
|
|
|
2017-10-05 16:07:43 +02:00
|
|
|
// ReadStream reads the stream for a given path
|
2014-10-27 15:17:54 +01:00
|
|
|
func (c *Client) ReadStream(path string) (io.ReadCloser, error) {
|
2024-01-20 11:59:18 +01:00
|
|
|
log.Printf("Client.ReadStream(%s)", path)
|
2014-10-27 16:19:17 +01:00
|
|
|
rs, err := c.req("GET", path, nil, nil)
|
2014-10-27 14:32:16 +01:00
|
|
|
if err != nil {
|
2023-02-03 10:18:35 +01:00
|
|
|
return nil, NewPathErrorErr("ReadStream", path, err)
|
2014-10-27 14:32:16 +01:00
|
|
|
}
|
2018-05-16 12:35:57 +02:00
|
|
|
|
2017-12-20 10:53:49 +01:00
|
|
|
if rs.StatusCode == 200 {
|
|
|
|
return rs.Body, nil
|
|
|
|
}
|
2018-05-16 12:35:57 +02:00
|
|
|
|
|
|
|
rs.Body.Close()
|
2023-02-03 10:18:35 +01:00
|
|
|
return nil, NewPathError("ReadStream", path, rs.StatusCode)
|
2014-10-27 14:32:16 +01:00
|
|
|
}
|
|
|
|
|
2021-11-05 07:24:19 +01:00
|
|
|
// 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`.
|
|
|
|
func (c *Client) ReadStreamRange(path string, offset, length int64) (io.ReadCloser, error) {
|
2024-01-20 11:59:18 +01:00
|
|
|
log.Printf("Client.ReadStreamRange(%s)", path)
|
2021-11-05 07:24:19 +01:00
|
|
|
rs, err := c.req("GET", path, nil, func(r *http.Request) {
|
2022-10-12 18:09:28 +02:00
|
|
|
if length > 0 {
|
|
|
|
r.Header.Add("Range", fmt.Sprintf("bytes=%d-%d", offset, offset+length-1))
|
|
|
|
} else {
|
|
|
|
r.Header.Add("Range", fmt.Sprintf("bytes=%d-", offset))
|
|
|
|
}
|
2021-11-05 07:24:19 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2023-02-03 10:18:35 +01:00
|
|
|
return nil, NewPathErrorErr("ReadStreamRange", path, err)
|
2021-11-05 07:24:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if rs.StatusCode == http.StatusPartialContent {
|
|
|
|
// server supported partial content, return as-is.
|
|
|
|
return rs.Body, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// server returned success, but did not support partial content, so we have the whole
|
|
|
|
// stream in rs.Body
|
|
|
|
if rs.StatusCode == 200 {
|
|
|
|
// discard first 'offset' bytes.
|
|
|
|
if _, err := io.Copy(io.Discard, io.LimitReader(rs.Body, offset)); err != nil {
|
2023-02-03 10:18:35 +01:00
|
|
|
return nil, NewPathErrorErr("ReadStreamRange", path, err)
|
2021-11-05 07:24:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// return a io.ReadCloser that is limited to `length` bytes.
|
2023-06-07 17:23:35 +02:00
|
|
|
return &limitedReadCloser{rc: rs.Body, remaining: int(length)}, nil
|
2021-11-05 07:24:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rs.Body.Close()
|
2023-02-03 10:18:35 +01:00
|
|
|
return nil, NewPathError("ReadStream", path, rs.StatusCode)
|
2021-11-05 07:24:19 +01:00
|
|
|
}
|
|
|
|
|
2017-10-05 16:07:43 +02:00
|
|
|
// Write writes data to a given path
|
2022-01-28 17:20:35 +01:00
|
|
|
func (c *Client) Write(path string, data []byte, _ os.FileMode) (err error) {
|
2024-01-20 11:59:18 +01:00
|
|
|
log.Printf("Client.Write(%s)", path)
|
2022-01-28 17:20:35 +01:00
|
|
|
s, err := c.put(path, bytes.NewReader(data))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-10-27 14:32:16 +01:00
|
|
|
switch s {
|
|
|
|
|
2016-10-21 10:34:34 +02:00
|
|
|
case 200, 201, 204:
|
2014-10-27 14:32:16 +01:00
|
|
|
return nil
|
|
|
|
|
2022-10-16 00:42:21 +02:00
|
|
|
case 404, 409:
|
2022-01-28 17:20:35 +01:00
|
|
|
err = c.createParentCollection(path)
|
2018-07-14 01:48:30 +02:00
|
|
|
if err != nil {
|
2022-01-28 17:20:35 +01:00
|
|
|
return
|
2018-07-14 01:48:30 +02:00
|
|
|
}
|
|
|
|
|
2022-01-28 17:20:35 +01:00
|
|
|
s, err = c.put(path, bytes.NewReader(data))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-07-14 01:48:30 +02:00
|
|
|
if s == 200 || s == 201 || s == 204 {
|
2022-01-28 17:20:35 +01:00
|
|
|
return
|
2014-10-27 14:32:16 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-05 16:07:43 +02:00
|
|
|
|
2023-02-03 10:18:35 +01:00
|
|
|
return NewPathError("Write", path, s)
|
2014-10-23 10:39:55 +02:00
|
|
|
}
|
2014-10-27 15:17:54 +01:00
|
|
|
|
2017-10-05 16:07:43 +02:00
|
|
|
// WriteStream writes a stream
|
2022-01-28 17:20:35 +01:00
|
|
|
func (c *Client) WriteStream(path string, stream io.Reader, _ os.FileMode) (err error) {
|
2024-01-20 11:59:18 +01:00
|
|
|
log.Printf("Client.WriteStream(%s)", path)
|
2018-07-14 01:48:30 +02:00
|
|
|
|
2022-01-28 17:20:35 +01:00
|
|
|
err = c.createParentCollection(path)
|
2018-07-14 01:48:30 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-28 17:20:35 +01:00
|
|
|
s, err := c.put(path, stream)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-07-14 01:48:30 +02:00
|
|
|
|
2014-10-27 15:17:54 +01:00
|
|
|
switch s {
|
2016-10-21 10:34:34 +02:00
|
|
|
case 200, 201, 204:
|
2014-10-27 15:17:54 +01:00
|
|
|
return nil
|
|
|
|
|
|
|
|
default:
|
2023-02-03 10:18:35 +01:00
|
|
|
return NewPathError("WriteStream", path, s)
|
2014-10-27 15:17:54 +01:00
|
|
|
}
|
|
|
|
}
|