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,32 +52,12 @@ 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('(https) downloaded '+dest); cb('downloaded '+dest);
});
file.on('error', (err) => {
fs.unlink(dest);
file.close();
cb(err.message);
});
}
catch(ex) {
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) => { file.on('error', (err) => {
fs.unlink(dest); fs.unlink(dest);
@ -92,4 +70,14 @@ var dl = (url, dest, cb) => {
} }
}); });
} }
var adapterFor = (function() {
var url = require('url'),
adapters = {
'http:': require('http'),
'https:': require('https'),
};
return (inputUrl) => {
return adapters[url.parse(inputUrl).protocol]
} }
}());