🧱 Chunk is a download manager for slow and unstable servers
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Modernizes Go syntax

+15 -19
+1 -4
downloader.go
··· 245 245 last := t - 1 246 246 var c []chunk 247 247 for { 248 - end := start + d.ChunkSize - 1 249 - if end > last { 250 - end = last 251 - } 248 + end := min(start+d.ChunkSize-1, last) 252 249 c = append(c, chunk{start, end}) 253 250 if end == last { 254 251 break
+8 -9
downloader_test.go
··· 4 4 "archive/zip" 5 5 "bytes" 6 6 "context" 7 - "fmt" 8 7 "io" 9 8 "math/rand" 10 9 "net/http" ··· 68 67 w.Header().Add("Content-Length", "2") 69 68 return 70 69 } 71 - if _, err := fmt.Fprint(w, "42"); err != nil { 70 + if _, err := io.WriteString(w, "42"); err != nil { 72 71 t.Errorf("failed to write response: %v", err) 73 72 } 74 73 }, ··· 117 116 tmp := t.TempDir() 118 117 pth := filepath.Join(tmp, "archive.zip") 119 118 expected := make([]byte, 1_000_000) 120 - for i := 0; i < 1_000_000; i++ { 119 + for i := range 1_000_000 { 121 120 expected[i] = byte(97 + rand.Intn(122-97)) 122 121 } 123 122 ··· 221 220 if atomic.CompareAndSwapInt32(&attempts, 0, 1) { 222 221 tc.proc(w) 223 222 } 224 - if _, err := fmt.Fprint(w, "42"); err != nil { 223 + if _, err := io.WriteString(w, "42"); err != nil { 225 224 t.Errorf("failed to write response: %v", err) 226 225 } 227 226 }, ··· 286 285 url, first := func() (string, DownloadStatus) { 287 286 s := httptest.NewServer(http.HandlerFunc( 288 287 func(w http.ResponseWriter, r *http.Request) { 289 - if _, err := fmt.Fprint(w, "42"); err != nil { 288 + if _, err := io.WriteString(w, "42"); err != nil { 290 289 t.Errorf("failed to write response: %v", err) 291 290 } 292 291 }, ··· 385 384 func TestGetDownloadSize_ContentLength(t *testing.T) { 386 385 s := httptest.NewServer(http.HandlerFunc( 387 386 func(w http.ResponseWriter, r *http.Request) { 388 - if _, err := fmt.Fprint(w, "Test"); err != nil { 387 + if _, err := io.WriteString(w, "Test"); err != nil { 389 388 t.Errorf("failed to write response: %v", err) 390 389 } 391 390 }, ··· 411 410 w.WriteHeader(http.StatusTooManyRequests) 412 411 return 413 412 } 414 - if _, err := fmt.Fprint(w, "Test"); err != nil { 413 + if _, err := io.WriteString(w, "Test"); err != nil { 415 414 t.Errorf("failed to write response: %v", err) 416 415 } 417 416 }, ··· 433 432 s := httptest.NewServer(http.HandlerFunc( 434 433 func(w http.ResponseWriter, r *http.Request) { 435 434 w.Header().Set("Content-Range", "bytes 1-10/123") 436 - if _, err := fmt.Fprint(w, ""); err != nil { 435 + if _, err := io.WriteString(w, ""); err != nil { 437 436 t.Errorf("failed to write response: %v", err) 438 437 } 439 438 }, ··· 466 465 func TestGetDownloadSize_NoContent(t *testing.T) { 467 466 s := httptest.NewServer(http.HandlerFunc( 468 467 func(w http.ResponseWriter, r *http.Request) { 469 - if _, err := fmt.Fprint(w, ""); err != nil { 468 + if _, err := io.WriteString(w, ""); err != nil { 470 469 t.Errorf("failed to write response: %v", err) 471 470 } 472 471 },
+1 -1
progress.go
··· 172 172 if err != nil { 173 173 return nil, fmt.Errorf("could not get the download absolute path: %w", err) 174 174 } 175 - hash := fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("%s|%s", url, abs)))) 175 + hash := fmt.Sprintf("%x", md5.Sum(fmt.Appendf(nil, "%s|%s", url, abs))) 176 176 177 177 // file name is a hash of the URL and local file path, plus the file name 178 178 // in an human-readable way for debugging purposes
+5 -5
progress_test.go
··· 17 17 if err := p.done(1, 3); err != nil { 18 18 t.Errorf("expected no error marking chunk as done, got %s", err) 19 19 } 20 - for i := 0; i < 3; i++ { 20 + for i := range 3 { 21 21 got, err := p.shouldDownload(i) 22 22 if err != nil { 23 23 t.Errorf("expected no error checking if chunk %d should be downloaded, got %s", i, err) ··· 49 49 } 50 50 var wg sync.WaitGroup 51 51 errs := make(chan error) 52 - for i := 0; i < 2048; i++ { 52 + for i := range 2048 { 53 53 wg.Add(1) 54 54 go func(i int) { 55 55 defer wg.Done() 56 56 errs <- p.done(i, 2048) 57 57 }(i) 58 58 } 59 - for i := 0; i < 2048; i++ { 59 + for range 2048 { 60 60 err := <-errs 61 61 if err != nil { 62 62 t.Errorf("expected no error marking chunk as done, got %s", err) ··· 89 89 if err != nil { 90 90 t.Errorf("expected no error creating the progress, got %s", err) 91 91 } 92 - for i := 0; i < 3; i++ { 92 + for i := range 3 { 93 93 got, err := p.shouldDownload(i) 94 94 if err != nil { 95 95 t.Errorf("expected no error checking if chunk %d should be downloaded, got %s", i, err) ··· 149 149 if err != nil { 150 150 t.Errorf("expected no error creating the progress, got %s", err) 151 151 } 152 - for i := 0; i < 3; i++ { 152 + for i := range 3 { 153 153 got, err := p.shouldDownload(i) 154 154 if err != nil { 155 155 t.Errorf("expected no error checking if chunk %d should be downloaded, got %s", i, err)