webserver

This commit is contained in:
Flummi 2016-08-15 13:04:36 +02:00
parent eeb2b04e5b
commit aa607db2a7

View File

@ -6,7 +6,7 @@ var mysql = require('mysql');
var bot = require('coffea')(); var bot = require('coffea')();
var uuid = require('uuid'); var uuid = require('uuid');
var crypto = require('crypto'); var crypto = require('crypto');
const path = require('path'); var path = require('path');
var sql; var sql;
var haDC = () => { var haDC = () => {
@ -69,8 +69,10 @@ bot.on('message', (e) => {
e.reply(msg); e.reply(msg);
}); });
} }
else else {
fs.unlink("./b/"+tmpdest+"."+cb.infos.ext); // delete repost
e.reply("repost motherf0cker"); e.reply("repost motherf0cker");
}
}); });
}); });
} }
@ -156,35 +158,89 @@ var getCheckSum = (file, cbcs) => {
// Webserver // Webserver
var handleRequest = (request, response) => { http.createServer((req, res) => {
var tmpres = ""; var filePath = '.' + req.url;
var url = request.url.split("/")[1]; var url = req.url.split("/")[1];
if(request.url == "/") { if(filePath == './')
filePath = './index.html';
console.log('request ', filePath);
var extname = String(path.extname(filePath)).toLowerCase();
var contentType = 'text/html';
var mimeTypes = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.png': 'image/png',
'.jpg': 'image/jpg',
'.gif': 'image/gif',
'.mp3': 'audio/mpeg',
'.mp4': 'video/mp4',
'.webm': 'video/webm'
};
if(filePath == "./index.html") { // mainpage
sql.query("select * from `f0ck`.`items`", (err, rows, fields) => { sql.query("select * from `f0ck`.`items`", (err, rows, fields) => {
var tmpres = "";
rows.forEach((e,i,a) => { rows.forEach((e,i,a) => {
tmpres += e.src+"\n"; tmpres += e.src+"\n";
}); });
res.writeHead(200);
res.end(tmpres, 'utf-8');
}); });
} }
else if(Number.isInteger(parseInt(url))) { else if(Number.isInteger(parseInt(url))) { // itempage
sql.query("select * from `f0ck`.`items` where `id` = ? limit 1", url, (err, rows, fields) => { sql.query("select * from `f0ck`.`items` where `id` = ? limit 1", url, (err, rows, fields) => {
var tmpres = "";
rows.forEach((e,i,a) => { rows.forEach((e,i,a) => {
tmpres += "ID: "+e.id+"\n"; tmpres += "ID: "+e.id+"<br />\n";
tmpres += "dest: "+e.dest+"\n"; tmpres += "dest: "+e.dest+"<br />\n";
tmpres += "mime: "+e.mime+"\n"; tmpres += "mime: "+e.mime+"<br />\n";
tmpres += "nick: "+e.username+"\n"; tmpres += "nick: "+e.username+"<br />\n";
tmpres += "channel: "+e.userchannel+"\n"; tmpres += "channel: "+e.userchannel+"<br />\n";
tmpres += "network: "+e.usernetwork; tmpres += "network: "+e.usernetwork+"<br />\n";
switch(e.mime) {
case "image/png":
case "image/jpeg":
case "image/gif":
tmpres += "<img src='"+e.dest+"' />";
break;
case "video/webm":
case "video/mp4":
tmpres += "<video src='"+e.dest+"' autoplay controls loop></video>";
break;
case "audio/mpeg":
tmpres += "<audio controls src='"+e.dest+"' type='audio/mp3' autoplay></audio>";
break;
}
}); });
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(tmpres, 'utf-8');
}); });
} }
else { else if(filePath.match(/^\.\/b\/.*/)) { // file
tmpres = "404"; contentType = mimeTypes[extname] || 'application/octect-stream';
fs.readFile(filePath, (error, content) => {
if(error) {
if(error.code == 'ENOENT') {
res.writeHead(200, { 'Content-Type': contentType });
res.end('404 - file not found', 'utf-8');
}
else {
res.writeHead(500);
res.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
res.end();
}
}
else {
res.writeHead(200, { 'Content-Type': contentType, 'Content-Length': content.length });
res.end(content, 'utf-8');
}
});
} }
setTimeout(()=>{response.end(tmpres);}, 500); else { // errorpage
}; res.writeHead(404);
res.end('404 - not found', 'utf-8');
var server = http.createServer(handleRequest); }
server.listen(cfg.webserver.port, () => { }).listen(cfg.webserver.port);
console.log("Server listening on: http://localhost:%s", cfg.webserver.port);
});