diff --git a/src/backups.js b/src/backups.js index 66e551a5e..6c954804f 100644 --- a/src/backups.js +++ b/src/backups.js @@ -718,27 +718,31 @@ function downloadDir(backupConfig, backupFilePath, dataLayout, progressCallback, if (error) return done(new BoxError(BoxError.FS_ERROR, error.message)); async.retry({ times: 5, interval: 20000 }, function (retryCallback) { - let destStream = createWriteStream(destFilePath, backupConfig.encryption); - - destStream.on('progress', function (progress) { - const transferred = Math.round(progress.transferred/1024/1024), speed = Math.round(progress.speed/1024/1024); - if (!transferred && !speed) return progressCallback({ message: `Downloading ${entry.fullPath}` }); // 0M@0Mbps looks wrong - progressCallback({ message: `Downloading ${entry.fullPath}: ${transferred}M@${speed}Mbps` }); - }); - - // protect against multiple errors. must destroy the write stream so that a previous retry does not write - let closeAndRetry = once((error) => { - if (error) progressCallback({ message: `Download ${entry.fullPath} to ${destFilePath} errored: ${error.message}` }); - else progressCallback({ message: `Download ${entry.fullPath} to ${destFilePath} finished` }); - destStream.destroy(); - retryCallback(error); - }); - api(backupConfig.provider).download(backupConfig, entry.fullPath, function (error, sourceStream) { - if (error) return closeAndRetry(error); + if (error) { + progressCallback({ message: `Download ${entry.fullPath} to ${destFilePath} errored: ${error.message}` }); + return retryCallback(error); + } + + let destStream = createWriteStream(destFilePath, backupConfig.encryption); + + // protect against multiple errors. must destroy the write stream so that a previous retry does not write + let closeAndRetry = once((error) => { + if (error) progressCallback({ message: `Download ${entry.fullPath} to ${destFilePath} errored: ${error.message}` }); + else progressCallback({ message: `Download ${entry.fullPath} to ${destFilePath} finished` }); + sourceStream.destroy(); + destStream.destroy(); + retryCallback(error); + }); + + destStream.on('progress', function (progress) { + const transferred = Math.round(progress.transferred/1024/1024), speed = Math.round(progress.speed/1024/1024); + if (!transferred && !speed) return progressCallback({ message: `Downloading ${entry.fullPath}` }); // 0M@0Mbps looks wrong + progressCallback({ message: `Downloading ${entry.fullPath}: ${transferred}M@${speed}Mbps` }); + }); + destStream.on('error', closeAndRetry); sourceStream.on('error', closeAndRetry); - destStream.on('error', closeAndRetry); // already emits BoxError progressCallback({ message: `Downloading ${entry.fullPath} to ${destFilePath}` });