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

Adds target directory

Closes #13

+20 -4
+11 -2
downloader.go
··· 54 54 // handled, including retries, amoutn of requets, and size of each request, for 55 55 // example. 56 56 type Downloader struct { 57 - // Client is the HTTP client used for every request needed to download all 57 + // OutputDir is where the downloaded files will be saved. If not set, 58 + // defaults to the current working directory. 59 + OutputDir string 60 + 61 + // client is the HTTP client used for every request needed to download all 58 62 // the files. 59 63 client *http.Client 60 64 ··· 222 226 } 223 227 224 228 func (d *Downloader) prepareAndStartDownload(ctx context.Context, url string, ch chan<- DownloadStatus) { 225 - path := filepath.Join(os.TempDir(), filepath.Base(url)) 229 + path := filepath.Join(d.OutputDir, filepath.Base(url)) 226 230 s := DownloadStatus{URL: url, DownloadedFilePath: path} 227 231 t, err := d.getDownloadSize(ctx, url) 228 232 if err != nil { ··· 302 306 // NewDownloader creates a downloader with the defalt configuration. Check 303 307 // the constants in this package for their values. 304 308 func DefaultDownloader() *Downloader { 309 + dir, err := os.Getwd() 310 + if err != nil { 311 + dir = "" 312 + } 305 313 return &Downloader{ 314 + OutputDir: dir, 306 315 Timeout: DefaultTimeout, 307 316 ConcurrencyPerServer: DefaultConcurrencyPerServer, 308 317 MaxRetries: DefaultMaxRetries,
+9 -2
downloader_test.go
··· 38 38 )) 39 39 defer s.Close() 40 40 d := Downloader{ 41 + OutputDir: t.TempDir(), 41 42 Timeout: timeout, 42 43 MaxRetries: 4, 43 44 ConcurrencyPerServer: 1, ··· 72 73 )) 73 74 defer s.Close() 74 75 75 - ch := DefaultDownloader().Download(s.URL) 76 + d := DefaultDownloader() 77 + d.OutputDir = t.TempDir() 78 + ch := d.Download(s.URL) 76 79 <-ch // discard the first status (just the file size) 77 80 got := <-ch 78 81 defer os.Remove(got.DownloadedFilePath) ··· 138 141 // download 139 142 var got string 140 143 defer os.Remove(got) 141 - for g := range DefaultDownloader().Download(s.URL + "/archive.zip") { 144 + d := DefaultDownloader() 145 + d.OutputDir = t.TempDir() 146 + for g := range d.Download(s.URL + "/archive.zip") { 142 147 got = g.DownloadedFilePath 143 148 if g.Error != nil { 144 149 t.Errorf("expected no error during the download of the zip archive, got %s", g.Error) ··· 191 196 defer s.Close() 192 197 193 198 d := Downloader{ 199 + OutputDir: t.TempDir(), 194 200 Timeout: timeout, 195 201 MaxRetries: 4, 196 202 ConcurrencyPerServer: 1, ··· 243 249 )) 244 250 defer s.Close() 245 251 d := Downloader{ 252 + OutputDir: t.TempDir(), 246 253 Timeout: timeout, 247 254 MaxRetries: 4, 248 255 ConcurrencyPerServer: 1,