File tree Expand file tree Collapse file tree 1 file changed +20
-6
lines changed
Expand file tree Collapse file tree 1 file changed +20
-6
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ package internal
55import (
66 "context"
77 "encoding/json"
8+ "errors"
89 "fmt"
910 "io"
1011 "log/slog"
@@ -139,17 +140,28 @@ func (w *DefaultChunkUtils) writeChunks(
139140 results chan uint64 ,
140141 errs chan error ,
141142) {
142- // TODO: Handle this error
143- defer r .Close ()
143+ defer func (r * io.PipeReader ) {
144+ closeErr := r .Close ()
145+ if closeErr != nil {
146+ errs <- closeErr
147+ }
148+ }(r )
144149 defer close (results )
145150 defer close (errs )
146151 for i := uint64 (1 ); i <= numChunks ; i ++ {
147152 err := func () error {
153+ var copiedBytes uint64
148154 resolve := func (writer io.Writer ) (dir , address string , err error ) {
149- _ , err = io .CopyN (writer , r , int64 (w .ChunkSize ))
150- if err != nil && err == io .EOF {
151- err = nil
155+ written , err := io .CopyN (writer , r , int64 (w .ChunkSize ))
156+ if err != nil {
157+ // an End of File error should be considered a critical error if it is
158+ // returned before the last chunk
159+ if errors .Is (err , io .EOF ) && i == numChunks {
160+ err = nil
161+ }
152162 }
163+ // record the number of bytes written
164+ copiedBytes = uint64 (written )
153165 return
154166 }
155167
@@ -159,7 +171,9 @@ func (w *DefaultChunkUtils) writeChunks(
159171 return err
160172 }
161173
162- results <- i
174+ // if no error was encountered, report the number of bytes copied so it can be
175+ // computed to ensure the download was successful
176+ results <- copiedBytes
163177 return nil
164178 }()
165179 if err != nil {
You can’t perform that action at this time.
0 commit comments