Skip to content

Commit

Permalink
Fix downloadChunked for larger files
Browse files Browse the repository at this point in the history
closes #298
  • Loading branch information
corny committed Sep 4, 2023
1 parent 14b47a0 commit 9cc13ba
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,6 @@ func (c *Client) GetStreamContext(ctx context.Context, video *Video, format *For
} else {
// we have length information, let's download by chunks!
c.downloadChunked(ctx, req, w, format)

}

return r, contentLength, nil
Expand Down Expand Up @@ -402,13 +401,13 @@ func (c *Client) downloadChunked(ctx context.Context, req *http.Request, w *io.P
currentChunk := atomic.Uint32{}
for i := 0; i < maxRoutines; i++ {
go func() {
i := int(currentChunk.Add(1)) - 1
if i > len(chunks) {
chunkIndex := int(currentChunk.Add(1)) - 1
if chunkIndex >= len(chunks) {
// no more chunks
return
}

chunk := &chunks[i]
chunk := &chunks[chunkIndex]
err := c.downloadChunk(req.Clone(cancelCtx), chunk)
close(chunk.data)

Expand All @@ -424,6 +423,7 @@ func (c *Client) downloadChunked(ctx context.Context, req *http.Request, w *io.P
for i := 0; i < len(chunks); i++ {
select {
case <-cancelCtx.Done():
abort(context.Canceled)
return
case data := <-chunks[i].data:
_, err := io.Copy(w, bytes.NewBuffer(data))
Expand Down

0 comments on commit 9cc13ba

Please sign in to comment.