Commit 97f50101 authored by thatanimeweirdo's avatar thatanimeweirdo

Merge branch 'remove-double-up-code-for-http' into 'vue-rewrite'

replaced code double up for http

See merge request !33
parents d0d5a353 9f1a17ca
...@@ -167,56 +167,40 @@ function download(url, fileName, cb) { ...@@ -167,56 +167,40 @@ function download(url, fileName, cb) {
let file = fs.createWriteStream(dest); let file = fs.createWriteStream(dest);
var partiallength = 0 //sets partiallength to 0 var partiallength = 0 //sets partiallength to 0
if(new URL(url).protocol == "https:") { //Replaced code double up: https://stackoverflow.com/a/38465918
let request = https.get(url, function(response) { var protocol = (function() {
response.pipe(file); var url = require('url'),
adapters = {
var totallength = parseInt(response.headers['content-length'], 10); //sets totallength of file 'http:': http,
response.on("data", function(chunk) { 'https:': https,
partiallength += chunk.length //adds each time };
let decimallength = partiallength / totallength return function(inputUrl) {
if (decimallength != 1) { return adapters[url.parse(inputUrl).protocol]
win.setProgressBar(decimallength) //sets progress bar to decimal }
win.webContents.send("downloadProgress", decimallength) }());
}
else { let request = protocol(url).get(url, function(response) {
win.setProgressBar(0) //sets progress bar to blank when done response.pipe(file);
win.webContents.send("downloadProgress", 0)
} var totallength = parseInt(response.headers['content-length'], 10); //sets totallength of file
}); response.on("data", function(chunk) {
partiallength += chunk.length //adds each time
file.on('finish', function() { let decimallength = partiallength / totallength
file.close(cb(null, dest)); // async call of the callback if (decimallength != 1) {
}); win.setProgressBar(decimallength) //sets progress bar to decimal
}).on('error', function(err) { // Handle errors win.webContents.send("downloadProgress", decimallength)
fs.unlink(dest); // Delete the file async. (But we don't check the result) }
if (cb) cb(err.message, dest); else {
}); win.setProgressBar(0) //sets progress bar to blank when done
win.webContents.send("downloadProgress", 0)
} else { }
let request = http.get(url, function(response) { });
response.pipe(file);
file.on('finish', function() {
var totallength = parseInt(response.headers['content-length'], 10); //sets totallength of file file.close(cb(null, dest)); // async call of the callback
response.on("data", function(chunk) { });
partiallength += chunk.length //adds each time }).on('error', function(err) { // Handle errors
let decimallength = partiallength / totallength fs.unlink(dest); // Delete the file async. (But we don't check the result)
if (decimallength != 1) { if (cb) cb(err.message, dest);
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.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);
});
}
}; };
\ 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