Skip to content

Commit

Permalink
It just Werk™
Browse files Browse the repository at this point in the history
  • Loading branch information
ubtl committed Feb 15, 2024
1 parent 6bb1759 commit d634ef8
Show file tree
Hide file tree
Showing 26,438 changed files with 2,415,436 additions and 0 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
7 changes: 7 additions & 0 deletions app/action/badRequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const getResponse = require('../util/getResponse');

const badRequest = (req, res) => {
return res.json(getResponse(null, 400, 'bad request'));
};

module.exports = badRequest;
68 changes: 68 additions & 0 deletions app/action/category.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const ConnectDB = require('../util/connectDB');
const getResponse = require('../util/getResponse');
const { categoryMap } = require('../util/category');
const queryTags = require('../util/queryTags');
const queryTorrents = require('../util/queryTorrents');

const list = async (req, res) => {
let { category = '', page = 1, limit = 10 } = Object.assign({}, req.params, req.query);
[page, limit] = [page, limit].map(e => {
if (e <= 0) {
return 1;
}
return parseInt(e, 10);
});
if (limit > 25) {
return res.json(getResponse(null, 400, 'limit is too large'));
}

let cat = [];
if (!Number.isNaN(+category)) {
if (category < 0) {
category = -category ^ 2047;
}
Object.entries(categoryMap).forEach(([key, value]) => {
if (+key & +category) {
cat.push(value);
}
});
}
else {
cat = category.split(/\s*,\s*/).filter(e => e);
}
if (!cat.length) {
return res.json(getResponse(null, 400, 'category is not defined'));
}

const conn = await new ConnectDB().connect();

const result = await conn.query(
`SELECT * FROM gallery WHERE expunged = 0 AND category in (?)
ORDER BY posted DESC LIMIT ? OFFSET ?`,
[cat, limit, (page - 1) * limit]
);
const { total } = (await conn.query(
'SELECT COUNT(*) AS total FROM gallery WHERE expunged = 0 AND category in (?)',
[cat]
))[0];

if (!result.length) {
conn.destroy();
return res.json(getResponse([], 200, 'success', { total }));
}

const gids = result.map(e => e.gid);
const rootGids = result.map(e => e.root_gid).filter(e => e);
const gidTags = await queryTags(conn, gids);
const gidTorrents = await queryTorrents(conn, rootGids);

result.forEach(e => {
e.tags = gidTags[e.gid] || [];
e.torrents = gidTorrents[e.root_gid] || [];
});

conn.destroy();
return res.json(getResponse(result, 200, 'success', { total }));
};

module.exports = list;
35 changes: 35 additions & 0 deletions app/action/gallery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const ConnectDB = require('../util/connectDB');
const getResponse = require('../util/getResponse');
const queryTags = require('../util/queryTags');
const queryTorrents = require('../util/queryTorrents');

const gallery = async (req, res) => {
const { gid = '', token = '' } = Object.assign({}, req.params, req.query);
if (!/^\d+$/.test(gid) || !/^[0-9a-f]{10}$/.test(token)) {
return res.json(getResponse(null, 400, 'gid or token is invalid'));
}

const conn = await new ConnectDB().connect();

const result = (await conn.query('SELECT * FROM gallery WHERE gid = ? AND token = ?', [gid, token]))[0];
if (!result) {
conn.destroy();
return res.json(getResponse(null, 404, 'no gallery matches gid and token'));
}

const { root_gid } = result;
const tags = (await queryTags(conn, [gid]))[gid] || [];
let torrents = [];
if (result.root_gid) {
torrents = (await queryTorrents(conn, [root_gid]))[root_gid] || [];
}

conn.destroy();
return res.json(getResponse(
{ ...result, tags, torrents },
200,
'success'
));
};

module.exports = gallery;
45 changes: 45 additions & 0 deletions app/action/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const ConnectDB = require('../util/connectDB');
const getResponse = require('../util/getResponse');
const queryTags = require('../util/queryTags');
const queryTorrents = require('../util/queryTorrents');

const list = async (req, res) => {
let { page = 1, limit = 10 } = Object.assign({}, req.query);
[page, limit] = [page, limit].map(e => {
if (e <= 0) {
return 1;
}
return parseInt(e, 10);
});
if (limit > 25) {
return res.json(getResponse(null, 400, 'limit is too large'));
}

const conn = await new ConnectDB().connect();

const result = await conn.query(
'SELECT * FROM gallery WHERE expunged = 0 ORDER BY posted DESC LIMIT ? OFFSET ?',
[limit, (page - 1) * limit]
);
const { total } = (await conn.query('SELECT COUNT(*) AS total FROM gallery WHERE expunged = 0'))[0];

if (!result.length) {
conn.destroy();
return res.json(getResponse([], 200, 'success', { total }));
}

const gids = result.map(e => e.gid);
const rootGids = result.map(e => e.root_gid).filter(e => e);
const gidTags = await queryTags(conn, gids);
const gidTorrents = await queryTorrents(conn, rootGids);

result.forEach(e => {
e.tags = gidTags[e.gid] || [];
e.torrents = gidTorrents[e.root_gid] || [];
});

conn.destroy();
return res.json(getResponse(result, 200, 'success', { total }));
};

module.exports = list;
7 changes: 7 additions & 0 deletions app/action/notFound.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const getResponse = require('../util/getResponse');

const notFound = (req, res) => {
return res.status(404).json(getResponse(null, 404, 'not found'));
};

module.exports = notFound;
Loading

0 comments on commit d634ef8

Please sign in to comment.