Skip to content

Commit

Permalink
Merge pull request #794 from SiaFoundation/chris/seeker-contentlength
Browse files Browse the repository at this point in the history
Client refactor
  • Loading branch information
ChrisSchinnerl authored Dec 6, 2023
2 parents 2e65351 + 4271830 commit ca2d363
Showing 1 changed file with 20 additions and 24 deletions.
44 changes: 20 additions & 24 deletions worker/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,18 +196,8 @@ func (c *Client) UploadMultipartUploadPart(ctx context.Context, r io.Reader, buc
req.SetBasicAuth("", c.c.WithContext(ctx).Password)
if opts.ContentLength != 0 {
req.ContentLength = opts.ContentLength
} else {
if s, ok := r.(io.Seeker); ok {
length, err := s.Seek(0, io.SeekEnd)
if err != nil {
return nil, err
}
_, err = s.Seek(0, io.SeekStart)
if err != nil {
return nil, err
}
req.ContentLength = length
}
} else if req.ContentLength, err = sizeFromSeeker(r); err != nil {
return nil, fmt.Errorf("failed to get content length from seeker: %w", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
Expand Down Expand Up @@ -242,18 +232,8 @@ func (c *Client) UploadObject(ctx context.Context, r io.Reader, bucket, path str
req.SetBasicAuth("", c.c.WithContext(ctx).Password)
if opts.ContentLength != 0 {
req.ContentLength = opts.ContentLength
} else {
if s, ok := r.(io.Seeker); ok {
length, err := s.Seek(0, io.SeekEnd)
if err != nil {
return nil, err
}
_, err = s.Seek(0, io.SeekStart)
if err != nil {
return nil, err
}
req.ContentLength = length
}
} else if req.ContentLength, err = sizeFromSeeker(r); err != nil {
return nil, fmt.Errorf("failed to get content length from seeker: %w", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
Expand Down Expand Up @@ -299,3 +279,19 @@ func (c *Client) object(ctx context.Context, bucket, path string, opts api.Downl
}
return resp.Body, resp.Header, err
}

func sizeFromSeeker(r io.Reader) (int64, error) {
s, ok := r.(io.Seeker)
if !ok {
return 0, nil
}
size, err := s.Seek(0, io.SeekEnd)
if err != nil {
return 0, err
}
_, err = s.Seek(0, io.SeekStart)
if err != nil {
return 0, err
}
return size, nil
}

0 comments on commit ca2d363

Please sign in to comment.