commit 4fc0a77565702f9bf498485d42336502f2ee9776 Author: Andy Hsu <i@nn.ci> Date: Fri Oct 20 21:06:25 2023 +0800 fix(baidu_netdisk): upload file > 4GB (close #5392) commit aaffaee2b54fc067d240ea0c20ea3c2f39615d6e Author: gmugu <94156510@qq.com> Date: Thu Oct 19 19:17:53 2023 +0800 perf(webdav): support request with cookies (#5391) commit 8ef8023c20bfeee97ec82155b52eae0d80b1410e Author: NewbieOrange <NewbieOrange@users.noreply.github.com> Date: Thu Oct 19 19:17:09 2023 +0800 fix(aliyundrive_open): upload progress for normal upload (#5398) commit cdfbe6dcf2b361e4c93c2703c2f8c9bddeac0ee6 Author: foxxorcat <95907542+foxxorcat@users.noreply.github.com> Date: Wed Oct 18 16:27:07 2023 +0800 fix: hash gcid empty file (#5394) commit 94d028743abf8e0d736f80c0ec4fb294a1cc064c Author: Andy Hsu <i@nn.ci> Date: Sat Oct 14 13:17:51 2023 +0800 ci: remove `pr-welcome` label when close issue [skip ci] commit 7f7335435c2f32a3eef76fac4c4f783d9d8624fd Author: itsHenry <2671230065@qq.com> Date: Sat Oct 14 13:12:46 2023 +0800 feat(cloudreve): support thumbnail (#5373 close #5348) * feat(cloudreve): support thumbnail * chore: remove unnecessary code commit b9e192b29cffddf14a0dfb2d3885def57a56ce16 Author: foxxorcat <95907542+foxxorcat@users.noreply.github.com> Date: Thu Oct 12 20:57:12 2023 +0800 fix(115): limit request rate (#5367 close #5275) * fix(115):limit request rate * chore(115): fix unit of `limit_rate` --------- Co-authored-by: Andy Hsu <i@nn.ci> commit 69a98eaef612b58596e5c26c341b6d7cedecdf19 Author: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Date: Wed Oct 11 22:01:55 2023 +0800 fix(deps): update module github.com/aliyun/aliyun-oss-go-sdk to v2.2.9+incompatible (#5141) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> commit 1ebc96a4e5220c979fd581bb3b5640e9436f6665 Author: Andy Hsu <i@nn.ci> Date: Tue Oct 10 18:32:00 2023 +0800 fix(wopan): fatal error concurrent map writes (close #5352) commit 66e2324cac75cb3ef05af45dbdd10b124d534aff Author: Andy Hsu <i@nn.ci> Date: Tue Oct 10 18:23:11 2023 +0800 chore(deps): upgrade dependencies commit 7600dc28df137c439e538b4257731c33a63db9b5 Author: Andy Hsu <i@nn.ci> Date: Tue Oct 10 18:13:58 2023 +0800 fix(aliyundrive_open): change default api to raw server (close #5358) commit 8ef89ad0a496d5acc398794c0afa4f77c67ad371 Author: foxxorcat <95907542+foxxorcat@users.noreply.github.com> Date: Tue Oct 10 18:08:27 2023 +0800 fix(baidu_netdisk): hash and `error 2` (#5356) * fix(baidu):hash and error:2 * fix:invalid memory address commit 35d672217dde69e65b41b1fcd9786c1cfebcdc45 Author: jeffmingup <1960588251@qq.com> Date: Sun Oct 8 19:29:45 2023 +0800 fix(onedrive_app): incorrect api on `_accessToken` (#5346) commit 1a283bb2720eff6d1b0c1dd6f1667a6449905a9b Author: foxxorcat <95907542+foxxorcat@users.noreply.github.com> Date: Fri Oct 6 16:04:39 2023 +0800 feat(google_drive): add `hash_info`, `ctime`, `thumbnail` (#5334) commit a008f54f4d5eda5738abfd54bf1abf1e18c08430 Author: nkh0472 <67589323+nkh0472@users.noreply.github.com> Date: Thu Oct 5 13:10:51 2023 +0800 docs: minor language improvements (#5329) [skip ci]
485 lines
11 KiB
Go
485 lines
11 KiB
Go
package gowebdav
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/xml"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
pathpkg "path"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
// Client defines our structure
|
|
type Client struct {
|
|
root string
|
|
headers http.Header
|
|
interceptor func(method string, rq *http.Request)
|
|
c *http.Client
|
|
|
|
authMutex sync.Mutex
|
|
auth Authenticator
|
|
}
|
|
|
|
// Authenticator stub
|
|
type Authenticator interface {
|
|
Type() string
|
|
User() string
|
|
Pass() string
|
|
Authorize(*http.Request, string, string)
|
|
}
|
|
|
|
// NoAuth structure holds our credentials
|
|
type NoAuth struct {
|
|
user string
|
|
pw string
|
|
}
|
|
|
|
// Type identifies the authenticator
|
|
func (n *NoAuth) Type() string {
|
|
return "NoAuth"
|
|
}
|
|
|
|
// User returns the current user
|
|
func (n *NoAuth) User() string {
|
|
return n.user
|
|
}
|
|
|
|
// Pass returns the current password
|
|
func (n *NoAuth) Pass() string {
|
|
return n.pw
|
|
}
|
|
|
|
// Authorize the current request
|
|
func (n *NoAuth) Authorize(req *http.Request, method string, path string) {
|
|
}
|
|
|
|
// NewClient creates a new instance of client
|
|
func NewClient(uri, user, pw string) *Client {
|
|
return &Client{FixSlash(uri), make(http.Header), nil, &http.Client{}, sync.Mutex{}, &NoAuth{user, pw}}
|
|
}
|
|
|
|
// SetHeader lets us set arbitrary headers for a given client
|
|
func (c *Client) SetHeader(key, value string) {
|
|
c.headers.Add(key, value)
|
|
}
|
|
|
|
// SetInterceptor lets us set an arbitrary interceptor for a given client
|
|
func (c *Client) SetInterceptor(interceptor func(method string, rq *http.Request)) {
|
|
c.interceptor = interceptor
|
|
}
|
|
|
|
// SetTimeout exposes the ability to set a time limit for requests
|
|
func (c *Client) SetTimeout(timeout time.Duration) {
|
|
c.c.Timeout = timeout
|
|
}
|
|
|
|
// SetTransport exposes the ability to define custom transports
|
|
func (c *Client) SetTransport(transport http.RoundTripper) {
|
|
c.c.Transport = transport
|
|
}
|
|
|
|
// SetJar exposes the ability to set a cookie jar to the client.
|
|
func (c *Client) SetJar(jar http.CookieJar) {
|
|
c.c.Jar = jar
|
|
}
|
|
|
|
// Connect connects to our dav server
|
|
func (c *Client) Connect() error {
|
|
rs, err := c.options("/")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = rs.Body.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if rs.StatusCode != 200 {
|
|
return newPathError("Connect", c.root, rs.StatusCode)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type props struct {
|
|
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"`
|
|
}
|
|
|
|
type response struct {
|
|
Href string `xml:"DAV: href"`
|
|
Props []props `xml:"DAV: propstat"`
|
|
}
|
|
|
|
func getProps(r *response, status string) *props {
|
|
for _, prop := range r.Props {
|
|
if strings.Contains(prop.Status, status) {
|
|
return &prop
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ReadDir reads the contents of a remote directory
|
|
func (c *Client) ReadDir(path string) ([]os.FileInfo, error) {
|
|
path = FixSlashes(path)
|
|
files := make([]os.FileInfo, 0)
|
|
skipSelf := true
|
|
parse := func(resp interface{}) error {
|
|
r := resp.(*response)
|
|
|
|
if skipSelf {
|
|
skipSelf = false
|
|
if p := getProps(r, "200"); p != nil && p.Type.Local == "collection" {
|
|
r.Props = nil
|
|
return nil
|
|
}
|
|
return newPathError("ReadDir", path, 405)
|
|
}
|
|
|
|
if p := getProps(r, "200"); p != nil {
|
|
f := new(File)
|
|
if ps, err := url.PathUnescape(r.Href); err == nil {
|
|
f.name = pathpkg.Base(ps)
|
|
} else {
|
|
f.name = p.Name
|
|
}
|
|
f.path = path + f.name
|
|
f.modified = parseModified(&p.Modified)
|
|
f.etag = p.ETag
|
|
f.contentType = p.ContentType
|
|
|
|
if p.Type.Local == "collection" {
|
|
f.path += "/"
|
|
f.size = 0
|
|
f.isdir = true
|
|
} else {
|
|
f.size = parseInt64(&p.Size)
|
|
f.isdir = false
|
|
}
|
|
|
|
files = append(files, *f)
|
|
}
|
|
|
|
r.Props = nil
|
|
return nil
|
|
}
|
|
|
|
err := c.propfind(path, false,
|
|
`<d:propfind xmlns:d='DAV:'>
|
|
<d:prop>
|
|
<d:displayname/>
|
|
<d:resourcetype/>
|
|
<d:getcontentlength/>
|
|
<d:getcontenttype/>
|
|
<d:getetag/>
|
|
<d:getlastmodified/>
|
|
</d:prop>
|
|
</d:propfind>`,
|
|
&response{},
|
|
parse)
|
|
|
|
if err != nil {
|
|
if _, ok := err.(*os.PathError); !ok {
|
|
err = newPathErrorErr("ReadDir", path, err)
|
|
}
|
|
}
|
|
return files, err
|
|
}
|
|
|
|
// Stat returns the file stats for a specified path
|
|
func (c *Client) Stat(path string) (os.FileInfo, error) {
|
|
var f *File
|
|
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
|
|
f.etag = p.ETag
|
|
f.contentType = p.ContentType
|
|
|
|
if p.Type.Local == "collection" {
|
|
if !strings.HasSuffix(f.path, "/") {
|
|
f.path += "/"
|
|
}
|
|
f.size = 0
|
|
f.modified = time.Unix(0, 0)
|
|
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/>
|
|
<d:getcontenttype/>
|
|
<d:getetag/>
|
|
<d:getlastmodified/>
|
|
</d:prop>
|
|
</d:propfind>`,
|
|
&response{},
|
|
parse)
|
|
|
|
if err != nil {
|
|
if _, ok := err.(*os.PathError); !ok {
|
|
err = newPathErrorErr("ReadDir", path, err)
|
|
}
|
|
}
|
|
return f, err
|
|
}
|
|
|
|
// Remove removes a remote file
|
|
func (c *Client) Remove(path string) error {
|
|
return c.RemoveAll(path)
|
|
}
|
|
|
|
// RemoveAll removes remote files
|
|
func (c *Client) RemoveAll(path string) error {
|
|
rs, err := c.req("DELETE", path, nil, nil)
|
|
if err != nil {
|
|
return newPathError("Remove", path, 400)
|
|
}
|
|
err = rs.Body.Close()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if rs.StatusCode == 200 || rs.StatusCode == 204 || rs.StatusCode == 404 {
|
|
return nil
|
|
}
|
|
|
|
return newPathError("Remove", path, rs.StatusCode)
|
|
}
|
|
|
|
// Mkdir makes a directory
|
|
func (c *Client) Mkdir(path string, _ os.FileMode) (err error) {
|
|
path = FixSlashes(path)
|
|
status, err := c.mkcol(path)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if status == 201 {
|
|
return nil
|
|
}
|
|
|
|
return newPathError("Mkdir", path, status)
|
|
}
|
|
|
|
// MkdirAll like mkdir -p, but for webdav
|
|
func (c *Client) MkdirAll(path string, _ os.FileMode) (err error) {
|
|
path = FixSlashes(path)
|
|
status, err := c.mkcol(path)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if status == 201 {
|
|
return nil
|
|
}
|
|
if status == 409 {
|
|
paths := strings.Split(path, "/")
|
|
sub := "/"
|
|
for _, e := range paths {
|
|
if e == "" {
|
|
continue
|
|
}
|
|
sub += e + "/"
|
|
status, err = c.mkcol(sub)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if status != 201 {
|
|
return newPathError("MkdirAll", sub, status)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
return newPathError("MkdirAll", path, status)
|
|
}
|
|
|
|
// Rename moves a file from A to B
|
|
func (c *Client) Rename(oldpath, newpath string, overwrite bool) error {
|
|
return c.copymove("MOVE", oldpath, newpath, overwrite)
|
|
}
|
|
|
|
// Copy copies a file from A to B
|
|
func (c *Client) Copy(oldpath, newpath string, overwrite bool) error {
|
|
return c.copymove("COPY", oldpath, newpath, overwrite)
|
|
}
|
|
|
|
// Read reads the contents of a remote file
|
|
func (c *Client) Read(path string) ([]byte, error) {
|
|
var stream io.ReadCloser
|
|
var err error
|
|
|
|
if stream, _, err = c.ReadStream(path, nil); err != nil {
|
|
return nil, err
|
|
}
|
|
defer stream.Close()
|
|
|
|
buf := new(bytes.Buffer)
|
|
_, err = buf.ReadFrom(stream)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return buf.Bytes(), nil
|
|
}
|
|
|
|
func (c *Client) Link(path string) (string, http.Header, error) {
|
|
method := "GET"
|
|
u := PathEscape(Join(c.root, path))
|
|
r, err := http.NewRequest(method, u, nil)
|
|
|
|
if err != nil {
|
|
return "", nil, newPathErrorErr("Link", path, err)
|
|
}
|
|
|
|
if c.c.Jar != nil {
|
|
for _, cookie := range c.c.Jar.Cookies(r.URL) {
|
|
r.AddCookie(cookie)
|
|
}
|
|
}
|
|
for k, vals := range c.headers {
|
|
for _, v := range vals {
|
|
r.Header.Add(k, v)
|
|
}
|
|
}
|
|
|
|
c.authMutex.Lock()
|
|
auth := c.auth
|
|
c.authMutex.Unlock()
|
|
|
|
auth.Authorize(r, method, path)
|
|
|
|
if c.interceptor != nil {
|
|
c.interceptor(method, r)
|
|
}
|
|
return r.URL.String(), r.Header, nil
|
|
}
|
|
|
|
// ReadStream reads the stream for a given path
|
|
func (c *Client) ReadStream(path string, callback func(rq *http.Request)) (io.ReadCloser, http.Header, error) {
|
|
rs, err := c.req("GET", path, nil, callback)
|
|
if err != nil {
|
|
return nil, nil, newPathErrorErr("ReadStream", path, err)
|
|
}
|
|
|
|
if rs.StatusCode < 400 {
|
|
return rs.Body, rs.Header, nil
|
|
}
|
|
|
|
rs.Body.Close()
|
|
return nil, nil, newPathError("ReadStream", path, rs.StatusCode)
|
|
}
|
|
|
|
// 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) {
|
|
rs, err := c.req("GET", path, nil, func(r *http.Request) {
|
|
r.Header.Add("Range", fmt.Sprintf("bytes=%v-%v", offset, offset+length-1))
|
|
})
|
|
if err != nil {
|
|
return nil, newPathErrorErr("ReadStreamRange", path, err)
|
|
}
|
|
|
|
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 {
|
|
return nil, newPathErrorErr("ReadStreamRange", path, err)
|
|
}
|
|
|
|
// return a io.ReadCloser that is limited to `length` bytes.
|
|
return &limitedReadCloser{rs.Body, int(length)}, nil
|
|
}
|
|
|
|
rs.Body.Close()
|
|
return nil, newPathError("ReadStream", path, rs.StatusCode)
|
|
}
|
|
|
|
// Write writes data to a given path
|
|
func (c *Client) Write(path string, data []byte, _ os.FileMode) (err error) {
|
|
s, err := c.put(path, bytes.NewReader(data), nil)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
switch s {
|
|
|
|
case 200, 201, 204:
|
|
return nil
|
|
|
|
case 409:
|
|
err = c.createParentCollection(path)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
s, err = c.put(path, bytes.NewReader(data), nil)
|
|
if err != nil {
|
|
return
|
|
}
|
|
if s == 200 || s == 201 || s == 204 {
|
|
return
|
|
}
|
|
}
|
|
|
|
return newPathError("Write", path, s)
|
|
}
|
|
|
|
// WriteStream writes a stream
|
|
func (c *Client) WriteStream(path string, stream io.Reader, _ os.FileMode, callback func(r *http.Request)) (err error) {
|
|
|
|
err = c.createParentCollection(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
s, err := c.put(path, stream, callback)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
switch s {
|
|
case 200, 201, 204:
|
|
return nil
|
|
|
|
default:
|
|
return newPathError("WriteStream", path, s)
|
|
}
|
|
}
|