From 0cdb428afba2ddf765f8263d26a1ab3fda49242b Mon Sep 17 00:00:00 2001 From: Koen Kanters Date: Mon, 16 May 2022 16:45:19 +0200 Subject: [PATCH] Handle redirects. https://github.com/Koenkk/zigbee-OTA/issues/114 --- scripts/add.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/scripts/add.js b/scripts/add.js index 1e50b5bf..ba023836 100644 --- a/scripts/add.js +++ b/scripts/add.js @@ -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)); + } }); }); }