···245245 last := t - 1
246246 var c []chunk
247247 for {
248248- end := start + d.ChunkSize - 1
249249- if end > last {
250250- end = last
251251- }
248248+ end := min(start+d.ChunkSize-1, last)
252249 c = append(c, chunk{start, end})
253250 if end == last {
254251 break
+8-9
downloader_test.go
···44 "archive/zip"
55 "bytes"
66 "context"
77- "fmt"
87 "io"
98 "math/rand"
109 "net/http"
···6867 w.Header().Add("Content-Length", "2")
6968 return
7069 }
7171- if _, err := fmt.Fprint(w, "42"); err != nil {
7070+ if _, err := io.WriteString(w, "42"); err != nil {
7271 t.Errorf("failed to write response: %v", err)
7372 }
7473 },
···117116 tmp := t.TempDir()
118117 pth := filepath.Join(tmp, "archive.zip")
119118 expected := make([]byte, 1_000_000)
120120- for i := 0; i < 1_000_000; i++ {
119119+ for i := range 1_000_000 {
121120 expected[i] = byte(97 + rand.Intn(122-97))
122121 }
123122···221220 if atomic.CompareAndSwapInt32(&attempts, 0, 1) {
222221 tc.proc(w)
223222 }
224224- if _, err := fmt.Fprint(w, "42"); err != nil {
223223+ if _, err := io.WriteString(w, "42"); err != nil {
225224 t.Errorf("failed to write response: %v", err)
226225 }
227226 },
···286285 url, first := func() (string, DownloadStatus) {
287286 s := httptest.NewServer(http.HandlerFunc(
288287 func(w http.ResponseWriter, r *http.Request) {
289289- if _, err := fmt.Fprint(w, "42"); err != nil {
288288+ if _, err := io.WriteString(w, "42"); err != nil {
290289 t.Errorf("failed to write response: %v", err)
291290 }
292291 },
···385384func TestGetDownloadSize_ContentLength(t *testing.T) {
386385 s := httptest.NewServer(http.HandlerFunc(
387386 func(w http.ResponseWriter, r *http.Request) {
388388- if _, err := fmt.Fprint(w, "Test"); err != nil {
387387+ if _, err := io.WriteString(w, "Test"); err != nil {
389388 t.Errorf("failed to write response: %v", err)
390389 }
391390 },
···411410 w.WriteHeader(http.StatusTooManyRequests)
412411 return
413412 }
414414- if _, err := fmt.Fprint(w, "Test"); err != nil {
413413+ if _, err := io.WriteString(w, "Test"); err != nil {
415414 t.Errorf("failed to write response: %v", err)
416415 }
417416 },
···433432 s := httptest.NewServer(http.HandlerFunc(
434433 func(w http.ResponseWriter, r *http.Request) {
435434 w.Header().Set("Content-Range", "bytes 1-10/123")
436436- if _, err := fmt.Fprint(w, ""); err != nil {
435435+ if _, err := io.WriteString(w, ""); err != nil {
437436 t.Errorf("failed to write response: %v", err)
438437 }
439438 },
···466465func TestGetDownloadSize_NoContent(t *testing.T) {
467466 s := httptest.NewServer(http.HandlerFunc(
468467 func(w http.ResponseWriter, r *http.Request) {
469469- if _, err := fmt.Fprint(w, ""); err != nil {
468468+ if _, err := io.WriteString(w, ""); err != nil {
470469 t.Errorf("failed to write response: %v", err)
471470 }
472471 },
+1-1
progress.go
···172172 if err != nil {
173173 return nil, fmt.Errorf("could not get the download absolute path: %w", err)
174174 }
175175- hash := fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("%s|%s", url, abs))))
175175+ hash := fmt.Sprintf("%x", md5.Sum(fmt.Appendf(nil, "%s|%s", url, abs)))
176176177177 // file name is a hash of the URL and local file path, plus the file name
178178 // in an human-readable way for debugging purposes
+5-5
progress_test.go
···1717 if err := p.done(1, 3); err != nil {
1818 t.Errorf("expected no error marking chunk as done, got %s", err)
1919 }
2020- for i := 0; i < 3; i++ {
2020+ for i := range 3 {
2121 got, err := p.shouldDownload(i)
2222 if err != nil {
2323 t.Errorf("expected no error checking if chunk %d should be downloaded, got %s", i, err)
···4949 }
5050 var wg sync.WaitGroup
5151 errs := make(chan error)
5252- for i := 0; i < 2048; i++ {
5252+ for i := range 2048 {
5353 wg.Add(1)
5454 go func(i int) {
5555 defer wg.Done()
5656 errs <- p.done(i, 2048)
5757 }(i)
5858 }
5959- for i := 0; i < 2048; i++ {
5959+ for range 2048 {
6060 err := <-errs
6161 if err != nil {
6262 t.Errorf("expected no error marking chunk as done, got %s", err)
···8989 if err != nil {
9090 t.Errorf("expected no error creating the progress, got %s", err)
9191 }
9292- for i := 0; i < 3; i++ {
9292+ for i := range 3 {
9393 got, err := p.shouldDownload(i)
9494 if err != nil {
9595 t.Errorf("expected no error checking if chunk %d should be downloaded, got %s", i, err)
···149149 if err != nil {
150150 t.Errorf("expected no error creating the progress, got %s", err)
151151 }
152152- for i := 0; i < 3; i++ {
152152+ for i := range 3 {
153153 got, err := p.shouldDownload(i)
154154 if err != nil {
155155 t.Errorf("expected no error checking if chunk %d should be downloaded, got %s", i, err)