+api, lol

This commit is contained in:
Flummi 2016-08-23 18:35:31 +00:00
parent 680ef78f07
commit 2f4bfd8269

View File

@ -183,6 +183,44 @@ function Websrv(tbot, tsql, tcfg, tlib) {
}
});
}
else if(filePath.match(/^\.\/api/i)) { // api
var url = filePath.split('/');
if(url[2] === undefined) { // Mainpage
res.writeHead(200, { 'Content-Type': contentType });
res.end('kommt noch');
}
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');