Skip to content

Commit

Permalink
Handle redirects. Koenkk#114
Browse files Browse the repository at this point in the history
  • Loading branch information
Koenkk committed May 16, 2022
1 parent 19e73f7 commit 0cdb428
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions scripts/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,20 @@ const main = async () => {
const lib = url.toLowerCase().startsWith("https") ? require('https') : require('http');
const file = fs.createWriteStream(path);

return new Promise((resolve) => {
return new Promise((resolve, reject) => {
const request = lib.get(url, function(response) {
response.pipe(file);
file.on('finish', function() {
file.close(function() {
resolve();
});
});
if (response.statusCode >= 200 && response.statusCode < 300) {
response.pipe(file);
file.on('finish', function() {
file.close(function() {
resolve();
});
});
} else if (response.headers.location) {
resolve(downloadFile(response.headers.location, path));
} else {
reject(new Error(response.statusCode + ' ' + response.statusMessage));
}
});
});
}
Expand Down

0 comments on commit 0cdb428

Please sign in to comment.