🧱 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.

Handles URL-encoded characters

+16 -8
+10 -5
downloader.go
··· 5 5 "context" 6 6 "fmt" 7 7 "net/http" 8 + "net/url" 8 9 "os" 9 10 "path/filepath" 10 11 "strings" ··· 246 247 return c 247 248 } 248 249 249 - func (d *Downloader) prepareAndStartDownload(ctx context.Context, url string, ch chan<- DownloadStatus) { 250 - path := filepath.Join(d.OutputDir, filepath.Base(url)) 251 - s := DownloadStatus{URL: url, DownloadedFilePath: path} 252 - t, err := d.getDownloadSize(ctx, url) 250 + func (d *Downloader) prepareAndStartDownload(ctx context.Context, u string, ch chan<- DownloadStatus) { 251 + n, err := url.PathUnescape(filepath.Base(u)) 252 + if err != nil { 253 + n = filepath.Base(u) 254 + } 255 + path := filepath.Join(d.OutputDir, n) 256 + s := DownloadStatus{URL: u, DownloadedFilePath: path} 257 + t, err := d.getDownloadSize(ctx, u) 253 258 if err != nil { 254 259 s.Error = fmt.Errorf("error getting file size: %w", err) 255 260 ch <- s ··· 297 302 urlDownload.Add(1) 298 303 go func(c chunk, idx int, s DownloadStatus) { 299 304 defer urlDownload.Done() 300 - b, err := d.downloadChunk(ctx, url, c) 305 + b, err := d.downloadChunk(ctx, u, c) 301 306 if err != nil { 302 307 s.Error = fmt.Errorf("error downloading chunk #%d: %w", idx+1, err) 303 308 ch <- s
+6 -3
downloader_test.go
··· 75 75 76 76 d := DefaultDownloader() 77 77 d.OutputDir = t.TempDir() 78 - ch := d.Download(s.URL) 78 + ch := d.Download(s.URL + "/My%20File.txt") 79 79 <-ch // discard the first status (just the file size) 80 80 got := <-ch 81 81 defer os.Remove(got.DownloadedFilePath) ··· 83 83 if got.Error != nil { 84 84 t.Errorf("invalid error. want:nil got:%q", got.Error) 85 85 } 86 - if got.URL != s.URL { 87 - t.Errorf("invalid URL. want:%s got:%s", s.URL, got.URL) 86 + if got.URL != s.URL+"/My%20File.txt" { 87 + t.Errorf("invalid URL. want:%s got:%s", s.URL+"/My%20File.txt", got.URL) 88 88 } 89 89 if got.DownloadedFileBytes != 2 { 90 90 t.Errorf("invalid DownloadedFileBytes. want:2 got:%d", got.DownloadedFileBytes) 91 + } 92 + if !strings.HasSuffix(got.DownloadedFilePath, "My File.txt") { 93 + t.Errorf("expected DownloadedFilePath to enfd with My File.txt, got %s", got.DownloadedFilePath) 91 94 } 92 95 if got.FileSizeBytes != 2 { 93 96 t.Errorf("invalid FileSizeBytes. want:2 got:%d", got.FileSizeBytes)