This commit is contained in:
oxd 2024-11-25 08:24:18 -05:00
parent 55a9211b9a
commit ab49cd5b9c
20 changed files with 56 additions and 53 deletions

View file

@ -7,7 +7,6 @@ import crypto from "node:crypto";
let db = await initDb();
// TODO: rewrite
let main = new Route([], async function (req, res, input) {
const canvas = new Canvas.Canvas(250, 100)
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('Expires', 'Sun, 19 May 1984 02:00:00 GMT')
canvas.jpegStream().pipe(res)
return true;
});
export default main;

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

26
db.js
View file

@ -3,6 +3,15 @@ import { open } from 'sqlite'
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() {
if (db) return db;
@ -11,19 +20,12 @@ async function initDb() {
driver: sqlite3.Database
});
await db.run(`CREATE TABLE IF NOT EXISTS auth (username TEXT, password TEXT);`);
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);`);
let sqlCmds = sql.split('\n');
for (let cmd of sqlCmds) {
await db.run(cmd);
}
return db;
}

View file

@ -5,14 +5,13 @@ import auth from "../form/auth.js";
let db = await initDb();
// TODO: rewrite
let main = new Route([auth], async function (req, res, input) {
let { username } = input;
let id = randomUUID();
let { targetType, targetId, content } = req.body;
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)
return { 'success': false, 'message': 'Comment is too long' };

View file

@ -4,19 +4,18 @@ import auth from "../form/auth.js";
let db = await initDb();
// TODO: rewrite
let main = new Route([auth], async function (req, res, input) {
let { username } = input;
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 = ?', [
target,
username
]);
return { 'message': 'Video deleted', 'success': true};
return { 'message': 'Video deleted', 'success': true };
});
export default main;

View file

@ -1,20 +1,18 @@
import Route from "../route.js";
import initDb from "../db.js";
import { randomUUID } from 'node:crypto';
import auth from "../form/auth.js";
let db = await initDb();
function isValid(user) {
return user.search(/[^A-Za-z0-9\-\_]/g) == -1;
}
// TODO: rewrite
let main = new Route([auth], async function (req, res, input) {
let { username } = input;
let id = randomUUID();
let { target } = req.body;
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 = ?', [
username,
@ -26,13 +24,13 @@ let main = new Route([auth], async function (req, res, input) {
username,
target
]);
res.send({'success': true, 'message': 'User unfollowed'});
res.send({ 'success': true, 'message': 'User unfollowed' });
} else {
await db.run('INSERT INTO follow (username,target) VALUES (?,?)', [
username,
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 initDb from "../db.js";
import { compare } from "bcrypt";
@ -7,10 +6,9 @@ import captcha from "./captcha.js";
let db = await initDb();
// TODO: rewrite
let main = new Route([captcha], async function (req, res, input) {
let { user, pass } = req.body;
let { captchaMatch} = input;
let { captchaMatch } = input;
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 initDb from "../db.js";
import { hash } from "bcrypt";
@ -13,10 +12,9 @@ function isValid(user) {
return user.search(/[^A-Za-z0-9\-\_]/g) == -1;
}
// TODO: rewrite
let main = new Route([captcha], async function (req, res, input) {
let { user, pass, pass2 } = req.body;
let { captchaMatch} = input;
let { captchaMatch } = input;
if (!captchaMatch) return { 'success': false, 'message': 'Captcha is incorrect' };

View file

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

View file

@ -1,7 +1,7 @@
import Route from "../route.js";
import initDb from "../db.js";
import { randomUUID } from 'node:crypto';
import {exec} from 'node:child_process';
import { exec } from 'node:child_process';
import { promisify } from "node:util";
import auth from "../form/auth.js";
import captcha from "./captcha.js";
@ -10,24 +10,23 @@ const execP = promisify(exec);
let db = await initDb();
// TODO: rewrite
let main = new Route([auth,captcha], async function (req, res, input) {
let { captchaMatch} = input;
let main = new Route([auth, captcha], async function (req, res, input) {
let { captchaMatch } = input;
if (!captchaMatch) return { 'success': false, 'message': 'Captcha is incorrect' };
let { username } = input;
let id = randomUUID();
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 {
await execP(`ffmpeg -i ${path} videos/${id}.webm`);
await execP(`ffmpeg -i videos/${id}.webm -frames:v 1 videos/${id}.png`);
} 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 (?, ?, ?, ?, ?)', [
@ -38,7 +37,7 @@ let main = new Route([auth,captcha], async function (req, res, input) {
+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;

View file

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