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) {
let file = fs.createWriteStream(dest);
var partiallength = 0 //sets partiallength to 0
if(new URL(url).protocol == "https:") {
let request = https.get(url, function(response) {
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.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);
});
} else {
let request = http.get(url, function(response) {
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.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);
});
}
//Replaced code double up: https://stackoverflow.com/a/38465918
var protocol = (function() {
var url = require('url'),
adapters = {
'http:': http,
'https:': https,
};
return function(inputUrl) {
return adapters[url.parse(inputUrl).protocol]
}
}());
let request = protocol(url).get(url, function(response) {
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.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