modified: src/main.js

This commit is contained in:
Flummi 2016-08-14 02:15:53 +02:00
parent 039ca5dd80
commit 015c94d22a

View File

@ -1,7 +1,5 @@
var cfg = require('../../cfg.json');
var fs = require('fs');
var http = require("http");
var https = require("https");
var mysql = require('mysql');
var bot = require('coffea')();
const path = require('path');
@ -54,13 +52,12 @@ var getUser = (u, n) => {
var dl = (url, dest, cb) => {
var file = fs.createWriteStream(dest);
if(url.match(/https/)) {
var request = https.get(url, (response) => {
var request = adapterFor(url).get(url, (response) => {
try {
response.pipe(file);
file.on('finish', () => {
file.close();
cb('(https) downloaded '+dest);
cb('downloaded '+dest);
});
file.on('error', (err) => {
fs.unlink(dest);
@ -72,24 +69,15 @@ var dl = (url, dest, cb) => {
e.reply(ex);
}
});
}
else {
var request = http.get(url, (response) => {
try {
response.pipe(file);
file.on('finish', () => {
file.close();
cb('(http) downloaded '+dest);
});
file.on('error', (err) => {
fs.unlink(dest);
file.close();
cb(err.message);
});
}
catch(ex) {
e.reply(ex);
}
});
}
}
var adapterFor = (function() {
var url = require('url'),
adapters = {
'http:': require('http'),
'https:': require('https'),
};
return (inputUrl) => {
return adapters[url.parse(inputUrl).protocol]
}
}());