Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions helpers/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,23 @@ func (t *Transport) roundTrip(req *http.Request) (resp *http.Response, err error
if req.Method != http.MethodHead && req.Body != nil && t.ProgressBar != nil {
req.Body = io.NopCloser(io.TeeReader(req.Body, t.ProgressBar))
}

resp, err = t.Base.RoundTrip(req)

if resp != nil && req.Method == http.MethodHead && err == nil && t.ProgressBar != nil {
if resp.ContentLength > 0 {
contentLength := int(resp.ContentLength)
b := make([]byte, contentLength)
_, _ = t.ProgressBar.Write(b)
// Write progress in chunks to avoid allocating a large byte slice
const chunkSize = 8192 // 8KB chunks
remaining := resp.ContentLength
buf := make([]byte, chunkSize)

for remaining > 0 {
writeSize := chunkSize
if remaining < int64(chunkSize) {
writeSize = int(remaining)
}
_, _ = t.ProgressBar.Write(buf[:writeSize])
remaining -= int64(writeSize)
}
}
}

return resp, err
}