api: random, bitches

This commit is contained in:
Flummi 2018-05-26 13:14:17 +02:00
parent 17a726fe2b
commit 9fe601d0fa

View File

@ -239,7 +239,7 @@ function Websrv(tlib) {
else if(filePath.match(/^\.\/api/i)) { // api
var url = filePath.split('/');
if(url[2] === undefined) { // Mainpage
var query = "select * from `f0ck`.`items`";
/*var query = "select * from `f0ck`.`items`";
lib.sql.query(query, (err, rows, fields) => {
var items = [];
rows.forEach((e,i,a) => {
@ -250,7 +250,9 @@ function Websrv(tlib) {
});
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end(JSON.stringify(items), 'utf-8');
});
});*/
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('nope', 'utf-8');
}
else if(url[2] == "p" && Number.isInteger(parseInt(url[3]))) { // pagination
var eps = 50;
@ -303,6 +305,36 @@ function Websrv(tlib) {
res.end(JSON.stringify(data), 'utf-8');
});
}
else if(url[2] === "random") {
res.writeHead(200, { 'Content-Type': 'text/html' });
let mimes = [];
let query = "";
switch(url[3]) {
case "image":
mimes = ["image/png", "image/gif", "image/jpeg"];
query = "where ("+mimes.map(mime => "`mime` = '"+mime+"'").join(" or ")+") and rand() limit 1";
break;
case "video":
mimes = ["video/webm", "video/mp4", "video/quicktime"];
query = "where ("+mimes.map(mime => "`mime` = '"+mime+"'").join(" or ")+") and rand() limit 1";
break;
case "audio":
mimes = ["audio/mpeg", "audio/flac", "audio/x-flac", "audio/ogg"];
query = "where ("+mimes.map(mime => "`mime` = '"+mime+"'").join(" or ")+") and rand() limit 1";
break;
default:
query = "order by rand() limit 1";
break;
}
if(query.length === 0)
return res.end('type not found', 'utf-8');
lib.sql.query("select `id`, `mime`, `size`, `username`, `userchannel`, `usernetwork`, `stamp` from `f0ck`.`items` " + query, (err, rows, fields) => {
if(err || rows.length === 0)
return res.end('no results', 'utf-8');
res.end(JSON.stringify( rows[0] ), 'utf-8');
});
}
}
else { // errorpage
res.writeHead(404);