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 uuid = require('uuid');
var crypto = require('crypto');
const path = require('path');
var path = require('path');
var sql;
var haDC = () => {
@ -69,8 +69,10 @@ bot.on('message', (e) => {
e.reply(msg);
});
}
else
else {
fs.unlink("./b/"+tmpdest+"."+cb.infos.ext); // delete repost
e.reply("repost motherf0cker");
}
});
});
}
@ -156,35 +158,89 @@ var getCheckSum = (file, cbcs) => {
// Webserver
var handleRequest = (request, response) => {
var tmpres = "";
var url = request.url.split("/")[1];
if(request.url == "/") {
http.createServer((req, res) => {
var filePath = '.' + req.url;
var url = req.url.split("/")[1];
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) => {
var tmpres = "";
rows.forEach((e,i,a) => {
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) => {
var tmpres = "";
rows.forEach((e,i,a) => {
tmpres += "ID: "+e.id+"\n";
tmpres += "dest: "+e.dest+"\n";
tmpres += "mime: "+e.mime+"\n";
tmpres += "nick: "+e.username+"\n";
tmpres += "channel: "+e.userchannel+"\n";
tmpres += "network: "+e.usernetwork;
tmpres += "ID: "+e.id+"<br />\n";
tmpres += "dest: "+e.dest+"<br />\n";
tmpres += "mime: "+e.mime+"<br />\n";
tmpres += "nick: "+e.username+"<br />\n";
tmpres += "channel: "+e.userchannel+"<br />\n";
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 if(filePath.match(/^\.\/b\/.*/)) { // file
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 {
tmpres = "404";
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);
};
var server = http.createServer(handleRequest);
server.listen(cfg.webserver.port, () => {
console.log("Server listening on: http://localhost:%s", cfg.webserver.port);
});
}
else { // errorpage
res.writeHead(404);
res.end('404 - not found', 'utf-8');
}
}).listen(cfg.webserver.port);