small changes

This commit is contained in:
Flummi 2016-09-10 17:53:13 +02:00
parent b99f4bb53f
commit a886d46064
2 changed files with 249 additions and 231 deletions

View File

@ -30,6 +30,8 @@ function Lib(tbot, tsql) {
Lib.prototype.sql = tsql; Lib.prototype.sql = tsql;
this.cfg = cfg = require('../cfg/main.json'); this.cfg = cfg = require('../cfg/main.json');
Lib.prototype.debug = cfg.debug;
Lib.prototype.loadUser((cb) => { Lib.prototype.loadUser((cb) => {
console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden"); console.log((cb)?"Admins wurden geladen":"Admins konnten nicht geladen werden");
}); });
@ -122,7 +124,7 @@ Lib.prototype.generateThumbs = () => {
}; };
Lib.prototype.log = (msg) => { Lib.prototype.log = (msg) => {
if(Lib.prototype.debug) if(Lib.prototype.debug)
bot.send("#f0ck", msg, 'n0xy'); bot.send(cfg.debugchannel, msg, 'n0xy');
}; };
Lib.prototype.loadTrigger = () => { Lib.prototype.loadTrigger = () => {

View File

@ -22,258 +22,274 @@ function Websrv(tbot, tsql, tlib) {
var url = req.url.split("/")[1]; var url = req.url.split("/")[1];
if(filePath == './') if(filePath == './')
filePath = './index.html'; filePath = './index.html';
var extname = String(path.extname(filePath)).toLowerCase(); if(req.method == 'POST') {
var contentType = 'text/html'; if(filePath == './git') {
var mimeTypes = { var body = '';
'.html': 'text/html', req.on('data', (data) => {
'.js': 'text/javascript', body += data;
'.css': 'text/css', if(body.length > 1e6) {
'.png': 'image/png', req.connection.destroy();
'.jpg': 'image/jpg', }
'.gif': 'image/gif', });
'.mp3': 'audio/mpeg', req.on('end', () => {
'.mp4': 'video/mp4', console.log(body);
'.webm': 'video/webm',
'.css': 'text/css', //var eventname = body.event_name;
'.ogg': 'audio/ogg' //var autor = body.user_name;
}; //var commit = body.commits[body.commits[total_commits_count]-1].message;
if(filePath == "./index.html") { // mainpage //bot.log( eventname + ' from ' + autor + ' ('+commit+')' );
var tpl = swig.compile(templates.index); });
var data = { res.writeHead(200, { 'Content-Type': 'text/html' });
items: [], res.end('muh', 'utf-8');
last: 10000 }
}
else {
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',
'.css': 'text/css',
'.ogg': 'audio/ogg'
}; };
sql.query("select `id`,`mime` from `f0ck`.`items` order by `id` desc limit 100", (err, rows, fields) => { if(filePath == "./index.html") { // mainpage
rows.forEach((e,i,a) => { var tpl = swig.compile(templates.index);
data.items.push({ "id": e.id, "mime": e.mime });
data.last = e.id;
});
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(tpl(data), 'utf-8');
});
}
else if(Number.isInteger(parseInt(url))) { // itempage
var query = "select * from `f0ck`.`items` where `id` = ? limit 1; " // get item
+ "select `id` from `f0ck`.`items` where `id` = (select min(`id`) from `f0ck`.`items` where `id` > ?); " // get previous item
+ "select `id` from `f0ck`.`items` where `id` = (select max(`id`) from `f0ck`.`items` where `id` < ?)"; // get next item
sql.query(query, [url, url, url], (err, rows, fields) => {
var tpl = swig.compile(templates.item);
var data = { var data = {
id: '', items: [],
username: '', last: 10000
item: '',
src: '',
dest: '',
mime: '',
size: '',
userchannel: '',
usernetwork: '',
next: null,
prev: null
}; };
if(rows[0].length) { sql.query("select `id`,`mime` from `f0ck`.`items` order by `id` desc limit 100", (err, rows, fields) => {
var e = rows[0][0];
switch(e.mime) {
case "image/png":
case "image/jpeg":
case "image/gif":
data.item = 'image';
break;
case "video/webm":
case "video/mp4":
data.item = 'video';
break;
case "audio/mpeg":
case "audio/ogg":
data.item = 'audio';
break;
}
data.id = e.id;
data.username = e.username;
data.srcurl = e.src;
data.src = urlm.parse(e.src).hostname;
data.dest = e.dest;
data.mime = e.mime;
data.size = lib.formatSize(e.size);
data.userchannel = e.userchannel;
data.usernetwork = e.usernetwork;
if(rows[1].length)
data.next = rows[1][0].id;
if(rows[2].length)
data.prev = rows[2][0].id;
}
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(tpl(data), 'utf-8');
});
}
else if(filePath == "./random") {
sql.query("select `id` from `f0ck`.`items` order by rand() limit 1", (err, rows, fields) => {
res.writeHead(301, {
'Cache-Control': 'no-cache, public',
'Location': '/' + rows[0].id
});
res.end();
});
}
else if(filePath == "./how") {
var tpl = swig.compile(templates.how);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(tpl(), 'utf-8');
}
else if(filePath == "./contact") {
var tpl = swig.compile(templates.contact);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(tpl(), 'utf-8');
}
else if(filePath == "./scripts") {
var tpl = swig.compile(templates.scripts);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(tpl(), 'utf-8');
}
else if(filePath == './git') {
console.log(filePath);
console.log(res);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('muh', 'utf-8');
}
else if(filePath.match(/^\.\/(b|s|t)\/.*/)) { // file
contentType = mimeTypes[extname];
switch(contentType) {
case "video/webm":
case "video/mp4":
case "audio/mpeg":
case "audio/ogg":
var start = 0;
var end = 0;
var range = req.headers['range'];
var stat = fs.statSync(filePath);
if(range != null) {
start = parseInt(range.slice(range.indexOf('bytes=')+6, range.indexOf('-')));
end = parseInt(range.slice(range.indexOf('-')+1, range.length));
}
if(isNaN(end) || end == 0) end = stat.size-1;
if(start > end) return;
res.writeHead(206, {
'Connection':'close',
'Content-Type':contentType,
'Content-Length':end - start,
'Content-Range':'bytes '+start+'-'+end+'/'+stat.size,
'Transfer-Encoding':'chunked'
});
var stream = fs.createReadStream(filePath, { flags: 'r', start: start, end: end});
stream.pipe(res);
break;
default:
fs.readFile(filePath, (error, content) => {
if(error) {
if(error.code == 'ENOENT') {
res.writeHead(200, { 'Content-Type': contentType });
res.end('404 - f0ck you', '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, 'Cache-Control': 'max-age=2592000, public' });
res.end(content, 'utf-8');
}
});
break;
}
fs.readFile(filePath, (error, content) => {
if(error) {
if(error.code == 'ENOENT') {
res.writeHead(200, { 'Content-Type': contentType });
res.end('404 - f0ck you', 'utf-8');
}
else {
res.writeHead(500);
res.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
res.end();
}
}
});
}
else if(filePath.match(/^\.\/api/i)) { // api
var url = filePath.split('/');
if(url[2] === undefined) { // Mainpage
var query = "select * from `f0ck`.`items`";
sql.query(query, (err, rows, fields) => {
var items = [];
rows.forEach((e,i,a) => { rows.forEach((e,i,a) => {
items.push({ data.items.push({ "id": e.id, "mime": e.mime });
'id': e.id, data.last = e.id;
'mime': e.mime
});
}); });
res.writeHead(200, { 'Content-Type': 'text/html' }); res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(JSON.stringify(items), 'utf-8'); res.end(tpl(data), 'utf-8');
}); });
} }
else if(url[2] == "p" && Number.isInteger(parseInt(url[3]))) { // pagination else if(Number.isInteger(parseInt(url))) { // itempage
var eps = 50;
var id = url[3];
sql.query("select * from `f0ck`.`items` where `id` < ? order by `id` desc limit ?", [id, eps], (err, rows, fields) => {
var items = {
"items": [],
"last": id
};
rows.forEach((e,i,a) => {
items.items.push({
'id': e.id,
'mime': e.mime
});
items.last = e.id;
});
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(JSON.stringify(items), 'utf-8');
});
}
else if(Number.isInteger(parseInt(url[2]))) { // Item
var query = "select * from `f0ck`.`items` where `id` = ? limit 1; " // get item var query = "select * from `f0ck`.`items` where `id` = ? limit 1; " // get item
+ "select `id` from `f0ck`.`items` where `id` = (select min(`id`) from `f0ck`.`items` where `id` > ?); " // get previous item + "select `id` from `f0ck`.`items` where `id` = (select min(`id`) from `f0ck`.`items` where `id` > ?); " // get previous item
+ "select `id` from `f0ck`.`items` where `id` = (select max(`id`) from `f0ck`.`items` where `id` < ?)"; // get next item + "select `id` from `f0ck`.`items` where `id` = (select max(`id`) from `f0ck`.`items` where `id` < ?)"; // get next item
sql.query(query, [url[2], url[2], url[2]], (err, rows, fields) => { sql.query(query, [url, url, url], (err, rows, fields) => {
var data; var tpl = swig.compile(templates.item);
var data = {
id: '',
username: '',
item: '',
src: '',
dest: '',
mime: '',
size: '',
userchannel: '',
usernetwork: '',
next: null,
prev: null
};
if(rows[0].length) { if(rows[0].length) {
var e = rows[0][0]; var e = rows[0][0];
data = { switch(e.mime) {
id: e.id, case "image/png":
username: e.username, case "image/jpeg":
src: e.src, case "image/gif":
dest: e.dest, data.item = 'image';
mime: e.mime, break;
size: e.size, case "video/webm":
userchannel: e.userchannel, case "video/mp4":
usernetwork: e.usernetwork, data.item = 'video';
next: null, break;
prev: null case "audio/mpeg":
}; case "audio/ogg":
data.item = 'audio';
break;
}
data.id = e.id;
data.username = e.username;
data.srcurl = e.src;
data.src = urlm.parse(e.src).hostname;
data.dest = e.dest;
data.mime = e.mime;
data.size = lib.formatSize(e.size);
data.userchannel = e.userchannel;
data.usernetwork = e.usernetwork;
if(rows[1].length) if(rows[1].length)
data.next = rows[1][0].id; data.next = rows[1][0].id;
if(rows[2].length) if(rows[2].length)
data.prev = rows[2][0].id; data.prev = rows[2][0].id;
} }
else
data = { error: 'nope' };
res.writeHead(200, { 'Content-Type': 'text/html' }); res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(JSON.stringify(data), 'utf-8'); res.end(tpl(data), 'utf-8');
}); });
} }
} else if(filePath == "./random") {
else { // errorpage sql.query("select `id` from `f0ck`.`items` order by rand() limit 1", (err, rows, fields) => {
res.writeHead(404); res.writeHead(301, {
res.end('404 - f0ck you', 'utf-8'); 'Cache-Control': 'no-cache, public',
'Location': '/' + rows[0].id
});
res.end();
});
}
else if(filePath == "./how") {
var tpl = swig.compile(templates.how);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(tpl(), 'utf-8');
}
else if(filePath == "./contact") {
var tpl = swig.compile(templates.contact);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(tpl(), 'utf-8');
}
else if(filePath == "./scripts") {
var tpl = swig.compile(templates.scripts);
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(tpl(), 'utf-8');
}
else if(filePath.match(/^\.\/(b|s|t)\/.*/)) { // file
contentType = mimeTypes[extname];
switch(contentType) {
case "video/webm":
case "video/mp4":
case "audio/mpeg":
case "audio/ogg":
var start = 0;
var end = 0;
var range = req.headers['range'];
var stat = fs.statSync(filePath);
if(range != null) {
start = parseInt(range.slice(range.indexOf('bytes=')+6, range.indexOf('-')));
end = parseInt(range.slice(range.indexOf('-')+1, range.length));
}
if(isNaN(end) || end == 0) end = stat.size-1;
if(start > end) return;
res.writeHead(206, {
'Connection':'close',
'Content-Type':contentType,
'Content-Length':end - start,
'Content-Range':'bytes '+start+'-'+end+'/'+stat.size,
'Transfer-Encoding':'chunked'
});
var stream = fs.createReadStream(filePath, { flags: 'r', start: start, end: end});
stream.pipe(res);
break;
default:
fs.readFile(filePath, (error, content) => {
if(error) {
if(error.code == 'ENOENT') {
res.writeHead(200, { 'Content-Type': contentType });
res.end('404 - f0ck you', '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, 'Cache-Control': 'max-age=2592000, public' });
res.end(content, 'utf-8');
}
});
break;
}
fs.readFile(filePath, (error, content) => {
if(error) {
if(error.code == 'ENOENT') {
res.writeHead(200, { 'Content-Type': contentType });
res.end('404 - f0ck you', 'utf-8');
}
else {
res.writeHead(500);
res.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
res.end();
}
}
});
}
else if(filePath.match(/^\.\/api/i)) { // api
var url = filePath.split('/');
if(url[2] === undefined) { // Mainpage
var query = "select * from `f0ck`.`items`";
sql.query(query, (err, rows, fields) => {
var items = [];
rows.forEach((e,i,a) => {
items.push({
'id': e.id,
'mime': e.mime
});
});
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(JSON.stringify(items), 'utf-8');
});
}
else if(url[2] == "p" && Number.isInteger(parseInt(url[3]))) { // pagination
var eps = 50;
var id = url[3];
sql.query("select * from `f0ck`.`items` where `id` < ? order by `id` desc limit ?", [id, eps], (err, rows, fields) => {
var items = {
"items": [],
"last": id
};
rows.forEach((e,i,a) => {
items.items.push({
'id': e.id,
'mime': e.mime
});
items.last = e.id;
});
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(JSON.stringify(items), 'utf-8');
});
}
else if(Number.isInteger(parseInt(url[2]))) { // Item
var query = "select * from `f0ck`.`items` where `id` = ? limit 1; " // get item
+ "select `id` from `f0ck`.`items` where `id` = (select min(`id`) from `f0ck`.`items` where `id` > ?); " // get previous item
+ "select `id` from `f0ck`.`items` where `id` = (select max(`id`) from `f0ck`.`items` where `id` < ?)"; // get next item
sql.query(query, [url[2], url[2], url[2]], (err, rows, fields) => {
var data;
if(rows[0].length) {
var e = rows[0][0];
data = {
id: e.id,
username: e.username,
src: e.src,
dest: e.dest,
mime: e.mime,
size: e.size,
userchannel: e.userchannel,
usernetwork: e.usernetwork,
next: null,
prev: null
};
if(rows[1].length)
data.next = rows[1][0].id;
if(rows[2].length)
data.prev = rows[2][0].id;
}
else
data = { error: 'nope' };
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(JSON.stringify(data), 'utf-8');
});
}
}
else { // errorpage
res.writeHead(404);
res.end('404 - f0ck you', 'utf-8');
}
} }
} }
else { else {
console.log(req);
res.writeHead(403); res.writeHead(403);
res.end('403 - forbidden'); res.end('403 - forbidden');
} }