From aa607db2a7464379fbad19ce22a808cdec05a7b9 Mon Sep 17 00:00:00 2001 From: Flummi Date: Mon, 15 Aug 2016 13:04:36 +0200 Subject: [PATCH] webserver --- src/main.js | 100 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 78 insertions(+), 22 deletions(-) diff --git a/src/main.js b/src/main.js index eedfb91..14c971b 100644 --- a/src/main.js +++ b/src/main.js @@ -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+"
\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+"
\n"; + switch(e.mime) { + case "image/png": + case "image/jpeg": + case "image/gif": + tmpres += ""; + break; + case "video/webm": + case "video/mp4": + tmpres += ""; + break; + case "audio/mpeg": + tmpres += ""; + break; + } }); + res.writeHead(200, { 'Content-Type': 'text/html' }); + res.end(tmpres, 'utf-8'); }); } - else { - tmpres = "404"; + 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 { + 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); -}); \ No newline at end of file + else { // errorpage + res.writeHead(404); + res.end('404 - not found', 'utf-8'); + } +}).listen(cfg.webserver.port); \ No newline at end of file