This commit is contained in:
biglyderv 2024-11-25 14:12:44 -05:00
parent 77f21692cf
commit 4273369736
20 changed files with 56 additions and 53 deletions

View file

@ -7,7 +7,6 @@ import crypto from "node:crypto";
let db = await initDb(); let db = await initDb();
// TODO: rewrite
let main = new Route([], async function (req, res, input) { let main = new Route([], async function (req, res, input) {
const canvas = new Canvas.Canvas(250, 100) const canvas = new Canvas.Canvas(250, 100)
const ctx = canvas.getContext('2d') const ctx = canvas.getContext('2d')
@ -47,6 +46,7 @@ let main = new Route([], async function (req, res, input) {
res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0') res.header('Cache-Control', 'no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0')
res.header('Expires', 'Sun, 19 May 1984 02:00:00 GMT') res.header('Expires', 'Sun, 19 May 1984 02:00:00 GMT')
canvas.jpegStream().pipe(res) canvas.jpegStream().pipe(res)
return true;
}); });
export default main; export default main;

View file

@ -2,9 +2,10 @@ import Route from "../route.js";
import auth from "../form/auth.js"; import auth from "../form/auth.js";
let main = new Route([auth], async function (req, res, input) { let main = new Route([auth], async function (req, res, input) {
return res.render('404', { res.render('404', {
...input ...input
}); });
return true;
}); });
export default main; export default main;

View file

@ -3,7 +3,7 @@ import auth from "../form/auth.js";
// TODO: rewrite // TODO: rewrite
let main = new Route([auth], async function (req, res, input) { let main = new Route([auth], async function (req, res, input) {
return res.render('form', { res.render('form', {
data: [ data: [
{ label: "Username", type: "text", name: "user" }, { label: "Username", type: "text", name: "user" },
{ label: "Password", type: "password", name: "pass" }, { label: "Password", type: "password", name: "pass" },
@ -12,6 +12,7 @@ let main = new Route([auth], async function (req, res, input) {
'title': 'Login', 'title': 'Login',
...input ...input
}); });
return true;
}); });
export default main; export default main;

View file

@ -11,11 +11,12 @@ let main = new Route([auth], async function (req, res, input) {
let videosFollow = await db.all('SELECT * FROM video WHERE username IN (SELECT target FROM follow WHERE username = ?) ORDER BY date DESC LIMIT 20', [ let videosFollow = await db.all('SELECT * FROM video WHERE username IN (SELECT target FROM follow WHERE username = ?) ORDER BY date DESC LIMIT 20', [
username username
]); ]);
return res.render('main', { res.render('main', {
...input, ...input,
videos, videos,
videosFollow videosFollow
}); });
return true;
}); });
export default main; export default main;

View file

@ -16,10 +16,11 @@ let main = new Route([auth], async function (req, res, input) {
username username
]); ]);
return res.render('messages', { res.render('messages', {
...input, ...input,
msgs msgs
}); });
return true;
}); });
export default main; export default main;

View file

@ -10,11 +10,12 @@ let main = new Route([comment], async function (req, res, input) {
]); ]);
if (videoData.length == 0) return; if (videoData.length == 0) return;
videoData = videoData[0]; videoData = videoData[0];
return res.render('player', { res.render('player', {
...input, ...input,
videoData, videoData,
ogType: 'video' ogType: 'video'
}); });
return true;
}); });
export default main; export default main;

View file

@ -3,7 +3,7 @@ import auth from "../form/auth.js";
// TODO: rewrite // TODO: rewrite
let main = new Route([auth], async function (req, res, input) { let main = new Route([auth], async function (req, res, input) {
return res.render('form', { res.render('form', {
data: [ data: [
{ label: "Username", type: "text", name: "user" }, { label: "Username", type: "text", name: "user" },
{ label: "Password", type: "password", name: "pass" }, { label: "Password", type: "password", name: "pass" },
@ -13,6 +13,7 @@ let main = new Route([auth], async function (req, res, input) {
'title': 'Join', 'title': 'Join',
...input ...input
}); });
return true;
}); });
export default main; export default main;

View file

@ -3,7 +3,7 @@ import auth from "../form/auth.js";
// TODO: rewrite // TODO: rewrite
let main = new Route([auth], async function (req, res, input) { let main = new Route([auth], async function (req, res, input) {
return res.render('form', { res.render('form', {
data: [ data: [
{ label: "Description", type: "textarea", name: "desc" }, { label: "Description", type: "textarea", name: "desc" },
{ label: "Avatar", type: "file", name: "file" } { label: "Avatar", type: "file", name: "file" }
@ -11,7 +11,8 @@ let main = new Route([auth], async function (req, res, input) {
'route': '/api/upload/settings', 'route': '/api/upload/settings',
'title': 'User Settings', 'title': 'User Settings',
...input ...input
}); /* todo: form in not a file */ });
return true;
}); });
export default main; export default main;

View file

@ -2,9 +2,10 @@ import Route from "../route.js";
import auth from "../form/auth.js"; import auth from "../form/auth.js";
let main = new Route([auth], async function (req, res, input) { let main = new Route([auth], async function (req, res, input) {
return res.render('guidelines', { res.render('guidelines', {
...input ...input
}); });
return true;
}); });
export default main; export default main;

View file

@ -3,7 +3,7 @@ import auth from "../form/auth.js";
// TODO: rewrite // TODO: rewrite
let main = new Route([auth], async function (req, res, input) { let main = new Route([auth], async function (req, res, input) {
return res.render('form', { res.render('form', {
data: [ data: [
{ label: "Video file", type: "file", name: "file" }, { label: "Video file", type: "file", name: "file" },
{ label: "Name", type: "text", name: "title" }, { label: "Name", type: "text", name: "title" },
@ -13,6 +13,7 @@ let main = new Route([auth], async function (req, res, input) {
'title': 'Upload Content', 'title': 'Upload Content',
...input ...input
}); });
return true;
}); });
export default main; export default main;

View file

@ -22,7 +22,7 @@ let main = new Route([comment], async function (req, res, input) {
req.query.id req.query.id
]); ]);
return res.render('user', { res.render('user', {
...input, ...input,
id, id,
videos, videos,
@ -31,6 +31,7 @@ let main = new Route([comment], async function (req, res, input) {
user, user,
icon: `${process.env.URL}/pfp/${id}.png` icon: `${process.env.URL}/pfp/${id}.png`
}); });
return true;
}); });
export default main; export default main;

26
db.js
View file

@ -3,6 +3,15 @@ import { open } from 'sqlite'
let db; let db;
const sql = `CREATE TABLE IF NOT EXISTS auth (username TEXT, password TEXT);
CREATE TABLE IF NOT EXISTS token (username TEXT, token TEXT);
CREATE TABLE IF NOT EXISTS video (id TEXT, title TEXT, desc TEXT, username TEXT, date REAL);
CREATE TABLE IF NOT EXISTS comment (username TEXT, targetType TEXT, targetId TEXT, date REAL, content TEXT, id TEXT);
CREATE TABLE IF NOT EXISTS follow (username TEXT, target TEXT);
CREATE TABLE IF NOT EXISTS user (username TEXT, bio TEXT);
CREATE TABLE IF NOT EXISTS captcha (key TEXT, solution TEXT);
CREATE TABLE IF NOT EXISTS message (username TEXT, targetType TEXT, targetId TEXT, date REAL, content TEXT, read TEXT);`
async function initDb() { async function initDb() {
if (db) return db; if (db) return db;
@ -11,19 +20,12 @@ async function initDb() {
driver: sqlite3.Database driver: sqlite3.Database
}); });
await db.run(`CREATE TABLE IF NOT EXISTS auth (username TEXT, password TEXT);`); let sqlCmds = sql.split('\n');
await db.run(`CREATE TABLE IF NOT EXISTS token (username TEXT, token TEXT);`);
await db.run(`CREATE TABLE IF NOT EXISTS video (id TEXT, title TEXT, desc TEXT, username TEXT, date REAL);`);
await db.run('CREATE TABLE IF NOT EXISTS comment (username TEXT, targetType TEXT, targetId TEXT, date REAL, content TEXT, id TEXT);');
await db.run('CREATE TABLE IF NOT EXISTS follow (username TEXT, target TEXT);');
await db.run(`CREATE TABLE IF NOT EXISTS user (username TEXT, bio TEXT);`);
await db.run(`CREATE TABLE IF NOT EXISTS captcha (key TEXT, solution TEXT);`);
await db.run(`CREATE TABLE IF NOT EXISTS message (username TEXT, targetType TEXT, targetId TEXT, date REAL, content TEXT, read TEXT);`);
for (let cmd of sqlCmds) {
await db.run(cmd);
}
return db; return db;
} }

View file

@ -5,14 +5,13 @@ import auth from "../form/auth.js";
let db = await initDb(); let db = await initDb();
// TODO: rewrite
let main = new Route([auth], async function (req, res, input) { let main = new Route([auth], async function (req, res, input) {
let { username } = input; let { username } = input;
let id = randomUUID(); let id = randomUUID();
let { targetType, targetId, content } = req.body; let { targetType, targetId, content } = req.body;
if (!targetType || !targetId || !content || username == '!nobody') if (!targetType || !targetId || !content || username == '!nobody')
return { 'success': false, 'message': 'Some fields are missing' }; // probably should not re-use these strings return { 'success': false, 'message': 'Some fields are missing' };
if (content.length > 8192) if (content.length > 8192)
return { 'success': false, 'message': 'Comment is too long' }; return { 'success': false, 'message': 'Comment is too long' };

View file

@ -4,19 +4,18 @@ import auth from "../form/auth.js";
let db = await initDb(); let db = await initDb();
// TODO: rewrite
let main = new Route([auth], async function (req, res, input) { let main = new Route([auth], async function (req, res, input) {
let { username } = input; let { username } = input;
let { target } = req.body; let { target } = req.body;
if (!target ) return { 'success': false, 'message': 'Some fields are missing' }; // probably should not re-use these strings if (!target) return { 'success': false, 'message': 'Some fields are missing' };
await db.run('DELETE FROM video WHERE id = ? AND username = ?', [ await db.run('DELETE FROM video WHERE id = ? AND username = ?', [
target, target,
username username
]); ]);
return { 'message': 'Video deleted', 'success': true}; return { 'message': 'Video deleted', 'success': true };
}); });
export default main; export default main;

View file

@ -1,20 +1,18 @@
import Route from "../route.js"; import Route from "../route.js";
import initDb from "../db.js"; import initDb from "../db.js";
import { randomUUID } from 'node:crypto';
import auth from "../form/auth.js"; import auth from "../form/auth.js";
let db = await initDb(); let db = await initDb();
function isValid(user) { function isValid(user) {
return user.search(/[^A-Za-z0-9\-\_]/g) == -1; return user.search(/[^A-Za-z0-9\-\_]/g) == -1;
} }
// TODO: rewrite
let main = new Route([auth], async function (req, res, input) { let main = new Route([auth], async function (req, res, input) {
let { username } = input; let { username } = input;
let id = randomUUID();
let { target } = req.body; let { target } = req.body;
if (!isValid(target) || username == '!nobody') if (!isValid(target) || username == '!nobody')
return { 'success': false, 'message': 'Some fields are missing' }; // probably should not re-use these strings return { 'success': false, 'message': 'Some fields are missing' };
let isFollowing = await db.all('SELECT * FROM follow WHERE username = ? AND target = ?', [ let isFollowing = await db.all('SELECT * FROM follow WHERE username = ? AND target = ?', [
username, username,
@ -26,13 +24,13 @@ let main = new Route([auth], async function (req, res, input) {
username, username,
target target
]); ]);
res.send({'success': true, 'message': 'User unfollowed'}); res.send({ 'success': true, 'message': 'User unfollowed' });
} else { } else {
await db.run('INSERT INTO follow (username,target) VALUES (?,?)', [ await db.run('INSERT INTO follow (username,target) VALUES (?,?)', [
username, username,
target target
]); ]);
res.send({'success': true, 'message': 'User followed'}); res.send({ 'success': true, 'message': 'User followed' });
} }
}); });

View file

@ -1,4 +1,3 @@
// from an old project: https://git.zenoverse.net/bigly-archive/auth-thing/raw/branch/main/src/routes/login/+page.server.js
import Route from "../route.js"; import Route from "../route.js";
import initDb from "../db.js"; import initDb from "../db.js";
import { compare } from "bcrypt"; import { compare } from "bcrypt";
@ -7,10 +6,9 @@ import captcha from "./captcha.js";
let db = await initDb(); let db = await initDb();
// TODO: rewrite
let main = new Route([captcha], async function (req, res, input) { let main = new Route([captcha], async function (req, res, input) {
let { user, pass } = req.body; let { user, pass } = req.body;
let { captchaMatch} = input; let { captchaMatch } = input;
if (!captchaMatch) return { 'success': false, 'message': 'Captcha is incorrect' }; if (!captchaMatch) return { 'success': false, 'message': 'Captcha is incorrect' };

View file

@ -1,4 +1,3 @@
// from an old project: https://git.zenoverse.net/bigly-archive/auth-thing/raw/branch/main/src/routes/register/+page.server.js
import Route from "../route.js"; import Route from "../route.js";
import initDb from "../db.js"; import initDb from "../db.js";
import { hash } from "bcrypt"; import { hash } from "bcrypt";
@ -13,10 +12,9 @@ function isValid(user) {
return user.search(/[^A-Za-z0-9\-\_]/g) == -1; return user.search(/[^A-Za-z0-9\-\_]/g) == -1;
} }
// TODO: rewrite
let main = new Route([captcha], async function (req, res, input) { let main = new Route([captcha], async function (req, res, input) {
let { user, pass, pass2 } = req.body; let { user, pass, pass2 } = req.body;
let { captchaMatch} = input; let { captchaMatch } = input;
if (!captchaMatch) return { 'success': false, 'message': 'Captcha is incorrect' }; if (!captchaMatch) return { 'success': false, 'message': 'Captcha is incorrect' };

View file

@ -1,7 +1,7 @@
import Route from "../route.js"; import Route from "../route.js";
import initDb from "../db.js"; import initDb from "../db.js";
import auth from "../form/auth.js"; import auth from "../form/auth.js";
import {exec} from 'node:child_process'; import { exec } from 'node:child_process';
import { promisify } from "node:util"; import { promisify } from "node:util";
import captcha from "./captcha.js"; import captcha from "./captcha.js";
@ -9,9 +9,8 @@ const execP = promisify(exec);
let db = await initDb(); let db = await initDb();
// TODO: rewrite let main = new Route([auth, captcha], async function (req, res, input) {
let main = new Route([auth,captcha], async function (req, res, input) { let { captchaMatch } = input;
let { captchaMatch} = input;
if (!captchaMatch) return { 'success': false, 'message': 'Captcha is incorrect' }; if (!captchaMatch) return { 'success': false, 'message': 'Captcha is incorrect' };
let { path } = req.file; let { path } = req.file;

View file

@ -1,7 +1,7 @@
import Route from "../route.js"; import Route from "../route.js";
import initDb from "../db.js"; import initDb from "../db.js";
import { randomUUID } from 'node:crypto'; import { randomUUID } from 'node:crypto';
import {exec} from 'node:child_process'; import { exec } from 'node:child_process';
import { promisify } from "node:util"; import { promisify } from "node:util";
import auth from "../form/auth.js"; import auth from "../form/auth.js";
import captcha from "./captcha.js"; import captcha from "./captcha.js";
@ -10,24 +10,23 @@ const execP = promisify(exec);
let db = await initDb(); let db = await initDb();
// TODO: rewrite let main = new Route([auth, captcha], async function (req, res, input) {
let main = new Route([auth,captcha], async function (req, res, input) { let { captchaMatch } = input;
let { captchaMatch} = input;
if (!captchaMatch) return { 'success': false, 'message': 'Captcha is incorrect' }; if (!captchaMatch) return { 'success': false, 'message': 'Captcha is incorrect' };
let { username } = input; let { username } = input;
let id = randomUUID(); let id = randomUUID();
let { title, desc } = req.body; let { title, desc } = req.body;
if (!title || !desc || !req.file || username == '!nobody') return { 'success': false, 'message': 'Some fields are missing' }; // probably should not re-use these strings if (!title || !desc || !req.file || username == '!nobody') return { 'success': false, 'message': 'Some fields are missing' };
let {path} = req.file; let { path } = req.file;
try { try {
await execP(`ffmpeg -i ${path} videos/${id}.webm`); await execP(`ffmpeg -i ${path} videos/${id}.webm`);
await execP(`ffmpeg -i videos/${id}.webm -frames:v 1 videos/${id}.png`); await execP(`ffmpeg -i videos/${id}.webm -frames:v 1 videos/${id}.png`);
} catch (err) { } catch (err) {
return { 'success': false, 'message': 'Video is invalid'} return { 'success': false, 'message': 'Video is invalid' }
} }
await db.run('INSERT INTO video (id, title, desc, username, date) VALUES (?, ?, ?, ?, ?)', [ await db.run('INSERT INTO video (id, title, desc, username, date) VALUES (?, ?, ?, ?, ?)', [
@ -38,7 +37,7 @@ let main = new Route([auth,captcha], async function (req, res, input) {
+new Date() +new Date()
]); ]);
return { 'message': 'Video created', 'success': true, 'redirect': '/client/video?id='+id }; return { 'message': 'Video created', 'success': true, 'redirect': '/client/video?id=' + id };
}); });
export default main; export default main;

View file

@ -66,6 +66,7 @@ async function iterate(req, res, index) {
let dat = await routesI[cmd].run(req, res, {}); let dat = await routesI[cmd].run(req, res, {});
if (!dat && !res.headersSent) { if (!dat && !res.headersSent) {
console.log(res)
return await routes.client['e404'].run(req, res, {}); return await routes.client['e404'].run(req, res, {});
} }