webdav: add Client.Create

This commit is contained in:
Simon Ser 2020-01-21 21:32:43 +01:00
parent 7d6de88179
commit 9db481fa51
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48

View File

@ -158,3 +158,37 @@ func (c *Client) Readdir(name string) ([]os.FileInfo, error) {
return l, nil
}
type fileWriter struct {
pw *io.PipeWriter
done <-chan error
}
func (fw *fileWriter) Write(b []byte) (int, error) {
return fw.pw.Write(b)
}
func (fw *fileWriter) Close() error {
if err := fw.pw.Close(); err != nil {
return err
}
return <-fw.done
}
func (c *Client) Create(name string) (io.WriteCloser, error) {
pr, pw := io.Pipe()
req, err := c.ic.NewRequest(http.MethodPut, name, pr)
if err != nil {
pw.Close()
return nil, err
}
done := make(chan error, 1)
go func() {
_, err := c.ic.Do(req)
done <- err
}()
return &fileWriter{pw, done}, nil
}