Commit f20404cc authored by Amy Yan's avatar Amy Yan

fixed background.js

parent ae49df63
...@@ -161,9 +161,26 @@ ipcMain.on("getDeeplink", (event) => { ...@@ -161,9 +161,26 @@ ipcMain.on("getDeeplink", (event) => {
function download(url, fileName, cb) { function download(url, fileName, cb) {
let dest = path.join(app.getPath('temp'), fileName + ".zip"); let dest = path.join(app.getPath('temp'), fileName + ".zip");
let file = fs.createWriteStream(dest); let file = fs.createWriteStream(dest);
var partiallength = 0 //sets partiallength to 0
if(new URL(url).protocol == "https:") { if(new URL(url).protocol == "https:") {
let request = https.get(url, function(response) { let request = https.get(url, function(response) {
response.pipe(file); response.pipe(file);
var totallength = parseInt(response.headers['content-length'], 10); //sets totallength of file
response.on("data", function(chunk) {
partiallength += chunk.length //adds each time
let decimallength = partiallength / totallength
if (decimallength != 1) {
win.setProgressBar(decimallength) //sets progress bar to decimal
win.webContents.send("downloadProgress", decimallength)
}
else {
win.setProgressBar(0) //sets progress bar to blank when done
win.webContents.send("downloadProgress", 0)
}
});
file.on('finish', function() { file.on('finish', function() {
file.close(cb(null, dest)); // async call of the callback file.close(cb(null, dest)); // async call of the callback
}); });
...@@ -171,23 +188,12 @@ function download(url, fileName, cb) { ...@@ -171,23 +188,12 @@ function download(url, fileName, cb) {
fs.unlink(dest); // Delete the file async. (But we don't check the result) fs.unlink(dest); // Delete the file async. (But we don't check the result)
if (cb) cb(err.message, dest); if (cb) cb(err.message, dest);
}); });
} else { } else {
let request = http.get(url, function(response) { let request = http.get(url, function(response) {
response.pipe(file); response.pipe(file);
file.on('finish', function() {
file.close(cb(null, dest)); // async call of the callback
});
}).on('error', function(err) { // Handle errors
fs.unlink(dest); // Delete the file async. (But we don't check the result)
if (cb) cb(err.message, dest);
});
}
let request = https.get(url, function(response) {
var partiallength = 0 //sets partiallength to 0
var totallength = parseInt(response.headers['content-length'], 10); //sets totallength of file var totallength = parseInt(response.headers['content-length'], 10); //sets totallength of file
response.on("data", function(chunk) { response.on("data", function(chunk) {
partiallength += chunk.length //adds each time partiallength += chunk.length //adds each time
let decimallength = partiallength / totallength let decimallength = partiallength / totallength
...@@ -199,10 +205,8 @@ function download(url, fileName, cb) { ...@@ -199,10 +205,8 @@ function download(url, fileName, cb) {
win.setProgressBar(0) //sets progress bar to blank when done win.setProgressBar(0) //sets progress bar to blank when done
win.webContents.send("downloadProgress", 0) win.webContents.send("downloadProgress", 0)
} }
}); });
response.pipe(file);
file.on('finish', function() { file.on('finish', function() {
file.close(cb(null, dest)); // async call of the callback file.close(cb(null, dest)); // async call of the callback
}); });
...@@ -210,4 +214,5 @@ function download(url, fileName, cb) { ...@@ -210,4 +214,5 @@ function download(url, fileName, cb) {
fs.unlink(dest); // Delete the file async. (But we don't check the result) fs.unlink(dest); // Delete the file async. (But we don't check the result)
if (cb) cb(err.message, dest); if (cb) cb(err.message, dest);
}); });
}
}; };
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment