Skip to content

Commit

Permalink
add file download retry
Browse files Browse the repository at this point in the history
  • Loading branch information
ngmachado committed Feb 11, 2024
1 parent 68b45b2 commit fdbd3b1
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,30 @@ function fileExist (path) {
return fs.existsSync(path);
}

// TODO: implement retry or remove retries param
async function downloadFile (url, dest, retries = 3) {
const writer = fs.createWriteStream(dest);
const response = await axios({
url,
method: "GET",
responseType: "stream"
});
response.data.pipe(writer);

return new Promise((resolve, reject) => {
writer.on("finish", resolve);
writer.on("error", reject);
});

for (let i = 0; i < retries; i++) {
try {
const response = await axios({
url,
method: "GET",
responseType: "stream"
});

response.data.pipe(writer);

await new Promise((resolve, reject) => {
writer.on("finish", resolve);
writer.on("error", reject);
});
break;
} catch (error) {
if (i === retries - 1) {
throw new Error(`Failed to download file from ${url} to ${dest} after ${retries} attempts`);
}
}
}
}

function unzip (file) {
Expand Down

0 comments on commit fdbd3b1

Please sign in to comment.