diff --git a/client/e404.js b/client/e404.js new file mode 100644 index 0000000..9f76fa6 --- /dev/null +++ b/client/e404.js @@ -0,0 +1,11 @@ +import Route from "../route.js"; +import auth from "../form/auth.js"; + +let main = new Route([auth], async function (req, res, input) { + let { username } = input; + return res.render('404', { + username + }); +}); + +export default main; \ No newline at end of file diff --git a/client/player.js b/client/player.js index 014525d..db86e0f 100644 --- a/client/player.js +++ b/client/player.js @@ -8,6 +8,7 @@ let main = new Route([comment], async function (req, res, input) { let videoData = await db.all('SELECT * FROM video WHERE id = ?', [ req.query.id ]); + if (videoData.length == 0) return; videoData = videoData[0]; return res.render('player', { ...input, diff --git a/client/user.js b/client/user.js index cceca23..85efa9c 100644 --- a/client/user.js +++ b/client/user.js @@ -22,6 +22,10 @@ let main = new Route([comment], async function (req, res, input) { req.query.id ]); + if (user.length == 0) { + return; + } + return res.render('user', { ...input, id, diff --git a/form/delete_video.js b/form/delete_video.js new file mode 100644 index 0000000..71d98e6 --- /dev/null +++ b/form/delete_video.js @@ -0,0 +1,23 @@ +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(); + +// 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 + + await db.run('DELETE FROM video WHERE id = ? AND username = ?', [ + target, + username + ]); + + return { 'message': 'Video deleted', 'success': true}; +}); + +export default main; \ No newline at end of file diff --git a/index.js b/index.js index 45b82c0..be99a57 100644 --- a/index.js +++ b/index.js @@ -41,6 +41,10 @@ app.get('/pfp/*', (req,res) => { res.redirect(301,'/static/img/logo.svg') }) +app.get('*', (req,res) => { + res.redirect(301,'/client/404'); +}) + app.listen(port, () => { console.log(`App listening on port ${port}`) }) \ No newline at end of file diff --git a/routes.js b/routes.js index 491a659..f900406 100644 --- a/routes.js +++ b/routes.js @@ -5,6 +5,7 @@ import upload from "./client/upload.js"; import player from "./client/player.js"; import user from "./client/user.js"; import settings from "./client/settings.js"; +import e404 from "./client/e404.js"; import loginB from "./form/login.js"; import registerB from "./form/register.js"; @@ -13,6 +14,7 @@ import commentB from "./form/comment.js"; import settingsB from "./form/settings.js"; import auth from "./form/auth.js"; import auth_api from "./form/auth_api.js"; +import delete_video from "./form/delete_video.js"; import follow from './form/follow.js'; const routes = { @@ -28,7 +30,8 @@ routes.client = { upload, video: player, user, - settings + settings, + e404 } routes.get = { @@ -42,7 +45,8 @@ routes.form = { comment: commentB, follow, settings: settingsB, - auth_api + auth_api, + delete_video }; async function iterate(req, res, index) { @@ -51,11 +55,18 @@ async function iterate(req, res, index) { let cmd = req.params.route; if (keys.indexOf(cmd) == -1) { - res.status(404).send('fail'); + routesI['e404'].run(req, res, {}); return; } - return await routesI[cmd].run(req, res, {}); + let dat = await routesI[cmd].run(req, res, {}); + + if (!dat) { + routesI['e404'].run(req, res, {}); + return; + } + + return dat; } export default iterate; \ No newline at end of file diff --git a/views/404.ejs b/views/404.ejs new file mode 100644 index 0000000..4b8c4cf --- /dev/null +++ b/views/404.ejs @@ -0,0 +1,6 @@ +<%- include('header.ejs') -%> +
+

Oops!

+

This page was not found.

+
+ <%- include('footer.ejs') -%> \ No newline at end of file diff --git a/views/player.ejs b/views/player.ejs index f1e4341..e867c70 100644 --- a/views/player.ejs +++ b/views/player.ejs @@ -1,5 +1,5 @@ <%- include('header.ejs') -%> -
+

Video

@@ -28,6 +28,8 @@
<%= videoData.desc %>
+ + <%- include('comments.ejs') -%> <%- include('footer.ejs') -%> \ No newline at end of file