···5454// handled, including retries, amoutn of requets, and size of each request, for
5555// example.
5656type Downloader struct {
5757- // Client is the HTTP client used for every request needed to download all
5757+ // OutputDir is where the downloaded files will be saved. If not set,
5858+ // defaults to the current working directory.
5959+ OutputDir string
6060+6161+ // client is the HTTP client used for every request needed to download all
5862 // the files.
5963 client *http.Client
6064···222226}
223227224228func (d *Downloader) prepareAndStartDownload(ctx context.Context, url string, ch chan<- DownloadStatus) {
225225- path := filepath.Join(os.TempDir(), filepath.Base(url))
229229+ path := filepath.Join(d.OutputDir, filepath.Base(url))
226230 s := DownloadStatus{URL: url, DownloadedFilePath: path}
227231 t, err := d.getDownloadSize(ctx, url)
228232 if err != nil {
···302306// NewDownloader creates a downloader with the defalt configuration. Check
303307// the constants in this package for their values.
304308func DefaultDownloader() *Downloader {
309309+ dir, err := os.Getwd()
310310+ if err != nil {
311311+ dir = ""
312312+ }
305313 return &Downloader{
314314+ OutputDir: dir,
306315 Timeout: DefaultTimeout,
307316 ConcurrencyPerServer: DefaultConcurrencyPerServer,
308317 MaxRetries: DefaultMaxRetries,
+9-2
downloader_test.go
···3838 ))
3939 defer s.Close()
4040 d := Downloader{
4141+ OutputDir: t.TempDir(),
4142 Timeout: timeout,
4243 MaxRetries: 4,
4344 ConcurrencyPerServer: 1,
···7273 ))
7374 defer s.Close()
74757575- ch := DefaultDownloader().Download(s.URL)
7676+ d := DefaultDownloader()
7777+ d.OutputDir = t.TempDir()
7878+ ch := d.Download(s.URL)
7679 <-ch // discard the first status (just the file size)
7780 got := <-ch
7881 defer os.Remove(got.DownloadedFilePath)
···138141 // download
139142 var got string
140143 defer os.Remove(got)
141141- for g := range DefaultDownloader().Download(s.URL + "/archive.zip") {
144144+ d := DefaultDownloader()
145145+ d.OutputDir = t.TempDir()
146146+ for g := range d.Download(s.URL + "/archive.zip") {
142147 got = g.DownloadedFilePath
143148 if g.Error != nil {
144149 t.Errorf("expected no error during the download of the zip archive, got %s", g.Error)
···191196 defer s.Close()
192197193198 d := Downloader{
199199+ OutputDir: t.TempDir(),
194200 Timeout: timeout,
195201 MaxRetries: 4,
196202 ConcurrencyPerServer: 1,
···243249 ))
244250 defer s.Close()
245251 d := Downloader{
252252+ OutputDir: t.TempDir(),
246253 Timeout: timeout,
247254 MaxRetries: 4,
248255 ConcurrencyPerServer: 1,