(dbx filesClient, arg *files.DownloadArg, dst string, errOut io.Writer)
| 435 | } |
| 436 | |
| 437 | func downloadFileOnce(dbx filesClient, arg *files.DownloadArg, dst string, errOut io.Writer) (*files.FileMetadata, error) { |
| 438 | res, contents, err := dbx.DownloadContext(currentContext(), arg) |
| 439 | if err != nil { |
| 440 | return nil, err |
| 441 | } |
| 442 | defer func() { _ = contents.Close() }() |
| 443 | |
| 444 | finalDst, err := downloadDestinationPath(dst) |
| 445 | if err != nil { |
| 446 | return nil, err |
| 447 | } |
| 448 | |
| 449 | f, tmp, err := createDownloadTemp(finalDst) |
| 450 | if err != nil { |
| 451 | return nil, err |
| 452 | } |
| 453 | removeTemp := true |
| 454 | defer func() { |
| 455 | if removeTemp { |
| 456 | _ = os.Remove(tmp) |
| 457 | } |
| 458 | }() |
| 459 | |
| 460 | progressbar := &ioprogress.Reader{ |
| 461 | Reader: contents, |
| 462 | DrawFunc: ioprogress.DrawTerminalf(errOut, func(progress, total int64) string { |
| 463 | return fmt.Sprintf("Downloading %s/%s", |
| 464 | humanize.IBytes(uint64(progress)), humanize.IBytes(uint64(total))) |
| 465 | }), |
| 466 | Size: downloadMetadataSize(res), |
| 467 | } |
| 468 | |
| 469 | _, copyErr := io.Copy(f, progressbar) |
| 470 | closeErr := f.Close() |
| 471 | if copyErr != nil { |
| 472 | return nil, copyErr |
| 473 | } |
| 474 | if closeErr != nil { |
| 475 | return nil, closeErr |
| 476 | } |
| 477 | if err := os.Rename(tmp, finalDst); err != nil { |
| 478 | return nil, err |
| 479 | } |
| 480 | removeTemp = false |
| 481 | return res, nil |
| 482 | } |
| 483 | |
| 484 | func downloadMetadataSize(metadata *files.FileMetadata) int64 { |
| 485 | if metadata == nil { |
no test coverage detected