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 cfg = require('../../cfg.json');
var fs = require('fs'); var fs = require('fs');
var http = require("http");
var https = require("https");
var mysql = require('mysql'); var mysql = require('mysql');
var bot = require('coffea')(); var bot = require('coffea')();
const path = require('path'); const path = require('path');
@ -54,42 +52,32 @@ var getUser = (u, n) => {
var dl = (url, dest, cb) => { var dl = (url, dest, cb) => {
var file = fs.createWriteStream(dest); var file = fs.createWriteStream(dest);
if(url.match(/https/)) { var request = adapterFor(url).get(url, (response) => {
var request = https.get(url, (response) => { try {
try { response.pipe(file);
response.pipe(file); file.on('finish', () => {
file.on('finish', () => { file.close();
file.close(); cb('downloaded '+dest);
cb('(https) downloaded '+dest); });
}); file.on('error', (err) => {
file.on('error', (err) => { fs.unlink(dest);
fs.unlink(dest); file.close();
file.close(); cb(err.message);
cb(err.message); });
}); }
} catch(ex) {
catch(ex) { e.reply(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]
} }
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);
}
});
}
}