fix: webdav client implement, close #49

This commit is contained in:
krau
2025-04-27 08:31:50 +08:00
parent 6ecee6d561
commit 838dfc35a1

View File

@@ -17,6 +17,14 @@ type Client struct {
httpClient *http.Client httpClient *http.Client
} }
type WebdavMethod string
const (
WebdavMethodMkcol WebdavMethod = "MKCOL"
WebdavMethodPropfind WebdavMethod = "PROPFIND"
WebdavMethodPut WebdavMethod = "PUT"
)
func NewClient(baseURL, username, password string, httpClient *http.Client) *Client { func NewClient(baseURL, username, password string, httpClient *http.Client) *Client {
if !strings.HasSuffix(baseURL, "/") { if !strings.HasSuffix(baseURL, "/") {
baseURL += "/" baseURL += "/"
@@ -32,17 +40,22 @@ func NewClient(baseURL, username, password string, httpClient *http.Client) *Cli
} }
} }
func (c *Client) doRequest(ctx context.Context, method, url string, body io.Reader) (*http.Response, error) { func (c *Client) doRequest(ctx context.Context, method WebdavMethod, url string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequestWithContext(ctx, method, url, body) req, err := http.NewRequestWithContext(ctx, string(method), url, body)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if c.Username != "" && c.Password != "" { if c.Username != "" && c.Password != "" {
req.SetBasicAuth(c.Username, c.Password) req.SetBasicAuth(c.Username, c.Password)
} }
if length := ctx.Value(types.ContextKeyContentLength); length != nil { if method == WebdavMethodPropfind {
if l, ok := length.(int64); ok { req.Header.Set("Depth", "1")
req.ContentLength = l }
if method == WebdavMethodPut && ctx != nil {
if length := ctx.Value(types.ContextKeyContentLength); length != nil {
if l, ok := length.(int64); ok {
req.ContentLength = l
}
} }
} }
return c.httpClient.Do(req) return c.httpClient.Do(req)
@@ -50,7 +63,7 @@ func (c *Client) doRequest(ctx context.Context, method, url string, body io.Read
func (c *Client) Exists(ctx context.Context, remotePath string) (bool, error) { func (c *Client) Exists(ctx context.Context, remotePath string) (bool, error) {
url := c.BaseURL + remotePath url := c.BaseURL + remotePath
resp, err := c.doRequest(ctx, "PROPFIND", url, nil) resp, err := c.doRequest(ctx, WebdavMethodPropfind, url, nil)
if err != nil { if err != nil {
return false, err return false, err
} }
@@ -86,7 +99,7 @@ func (c *Client) MkDir(ctx context.Context, dirPath string) error {
continue continue
} }
url := c.BaseURL + currentPath url := c.BaseURL + currentPath
resp, err := c.doRequest(ctx, "MKCOL", url, nil) resp, err := c.doRequest(ctx, WebdavMethodMkcol, url, nil)
if err != nil { if err != nil {
return err return err
} }
@@ -101,7 +114,7 @@ func (c *Client) MkDir(ctx context.Context, dirPath string) error {
func (c *Client) WriteFile(ctx context.Context, remotePath string, content io.Reader) error { func (c *Client) WriteFile(ctx context.Context, remotePath string, content io.Reader) error {
url := c.BaseURL + remotePath url := c.BaseURL + remotePath
resp, err := c.doRequest(ctx, "PUT", url, content) resp, err := c.doRequest(ctx, WebdavMethodPut, url, content)
if err != nil { if err != nil {
return err return err
} }