downloadFiles downloads the given files from the remote server and writes the contents into the given writer
(writer io.WriteCloser, changes []*remote.Change)
| 310 | |
| 311 | // downloadFiles downloads the given files from the remote server and writes the contents into the given writer |
| 312 | func (d *downstream) downloadFiles(writer io.WriteCloser, changes []*remote.Change) error { |
| 313 | defer writer.Close() |
| 314 | |
| 315 | // cancel after 1 hour |
| 316 | ctx, cancel := context.WithTimeout(d.sync.ctx, time.Hour) |
| 317 | defer cancel() |
| 318 | |
| 319 | // Print log message |
| 320 | if len(changes) <= 3 || d.sync.Options.Verbose { |
| 321 | for _, element := range changes { |
| 322 | d.sync.log.Infof("Downstream - Download file '.%s', uncompressed: ~%0.2f KB", element.Path, float64(element.Size)/1024.0) |
| 323 | } |
| 324 | } else if len(changes) > 3 { |
| 325 | filesize := int64(0) |
| 326 | for _, v := range changes { |
| 327 | filesize += v.Size |
| 328 | } |
| 329 | |
| 330 | d.sync.log.Infof("Downstream - Download %d file(s) (Uncompressed: ~%0.2f KB)", len(changes), float64(filesize)/1024.0) |
| 331 | } |
| 332 | |
| 333 | // Create new download client |
| 334 | downloadClient, err := d.client.Download(ctx) |
| 335 | if err != nil { |
| 336 | return errors.Wrap(err, "download files") |
| 337 | } |
| 338 | |
| 339 | // Send files to download |
| 340 | downloadFiles := make([]string, 0, downloadFilesBufferSize) |
| 341 | for _, change := range changes { |
| 342 | downloadFiles = append(downloadFiles, change.Path) |
| 343 | |
| 344 | if len(downloadFiles) >= downloadFilesBufferSize { |
| 345 | err = downloadClient.Send(&remote.Paths{ |
| 346 | Paths: downloadFiles, |
| 347 | }) |
| 348 | if err != nil { |
| 349 | return errors.Wrap(err, "send path") |
| 350 | } |
| 351 | |
| 352 | downloadFiles = make([]string, 0, downloadFilesBufferSize) |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | if len(downloadFiles) >= 0 { |
| 357 | err = downloadClient.Send(&remote.Paths{ |
| 358 | Paths: downloadFiles, |
| 359 | }) |
| 360 | if err != nil { |
| 361 | return errors.Wrap(err, "send path") |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | // We finish sending and start receiving the tar |
| 366 | err = downloadClient.CloseSend() |
| 367 | if err != nil { |
| 368 | return errors.Wrap(err, "close send") |
| 369 | } |
no test coverage detected