feat: provide content length for put method

Signed-off-by: Ben Tam <master@murasakiakari.moe>
This commit is contained in:
Ben Tam 2023-10-29 21:53:10 +08:00
parent f9157dbec1
commit c6155fbcae
No known key found for this signature in database
GPG Key ID: 1CC081A2EC8A14FB
2 changed files with 25 additions and 5 deletions

View File

@ -387,7 +387,7 @@ func (c *Client) ReadStreamRange(path string, offset, length int64) (io.ReadClos
// 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))
s, err := c.put(path, bytes.NewReader(data), int64(len(data)))
if err != nil {
return
}
@ -403,7 +403,7 @@ func (c *Client) Write(path string, data []byte, _ os.FileMode) (err error) {
return
}
s, err = c.put(path, bytes.NewReader(data))
s, err = c.put(path, bytes.NewReader(data), int64(len(data)))
if err != nil {
return
}
@ -423,7 +423,25 @@ func (c *Client) WriteStream(path string, stream io.Reader, _ os.FileMode) (err
return err
}
s, err := c.put(path, stream)
contentLength := int64(0)
if seeker, ok := stream.(io.Seeker); ok {
contentLength, err = seeker.Seek(0, io.SeekEnd)
if err != nil {
return err
}
_, err = seeker.Seek(0, io.SeekStart)
if err != nil {
return err
}
} else {
contentLength, err = io.Copy(io.Discard, stream)
if err != nil {
return err
}
}
s, err := c.put(path, stream, contentLength)
if err != nil {
return err
}

View File

@ -160,8 +160,10 @@ func (c *Client) copymove(method string, oldpath string, newpath string, overwri
return NewPathError(method, oldpath, s)
}
func (c *Client) put(path string, stream io.Reader) (status int, err error) {
rs, err := c.req("PUT", path, stream, nil)
func (c *Client) put(path string, stream io.Reader, contentLength int64) (status int, err error) {
rs, err := c.req("PUT", path, stream, func(r *http.Request) {
r.ContentLength = contentLength
})
if err != nil {
return
}