uploads test

This commit is contained in:
biglyderv 2024-11-25 14:12:43 -05:00
parent 29bc2b8ddd
commit dabbc7d862
11 changed files with 121 additions and 44 deletions

3
.gitignore vendored
View file

@ -1,4 +1,5 @@
/node_modules
/db
/package-lock.json
/uploads
/uploads
/videos

19
client/player.js Normal file
View file

@ -0,0 +1,19 @@
import Route from "../route.js";
import auth from "../form/auth.js";
import initDb from "../db.js";
let db = await initDb();
let main = new Route([auth], async function (req, res, input) {
let { username } = input;
let videoData = await db.all('SELECT * FROM video WHERE id = ?', [
req.query.id
]);
videoData = videoData[0];
return res.render('player', {
username,
videoData
});
});
export default main;

19
client/upload.js Normal file
View file

@ -0,0 +1,19 @@
import Route from "../route.js";
import auth from "../form/auth.js";
// TODO: rewrite
let main = new Route([auth], async function (req, res, input) {
let { username } = input;
return res.render('form', {
data: [
{ label: "Video file", type: "file", name: "file" },
{ label: "Name", type: "text", name: "title" },
{ label: "Description", type: "text", name: "desc" }
],
'route': '/api/upload/upload',
'title': 'Upload Content',
username
});
});
export default main;

2
db.js
View file

@ -14,6 +14,8 @@ async function initDb() {
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);`);
return db;
}

38
form/upload.js Normal file
View file

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

View file

@ -11,6 +11,7 @@ const upload = multer({ dest: 'uploads/' });
app.use(cookieParser());
app.use('/static', express.static('static'));
app.use('/videos', express.static('videos'));
app.engine('.ejs', ejs.__express);
app.set('views', './views');
app.set('view engine', 'ejs');
@ -31,6 +32,10 @@ app.post('/api/form/:route', upload.none(), async (req, res) => {
res.send(await iterate(req, res, 'form'));
})
app.post('/api/upload/:route', upload.single('file'), async (req, res) => {
res.send(await iterate(req, res, 'form'));
})
app.listen(port, () => {
console.log(`App listening on port ${port}`)
})

View file

@ -2,25 +2,31 @@ import main from "./client/main.js";
import auth from "./form/auth.js";
import login from "./client/login.js";
import register from "./client/register.js";
import upload from "./client/upload.js";
import player from "./client/player.js";
import loginB from "./form/login.js";
import registerB from "./form/register.js";
import uploadB from "./form/upload.js";
const routes = {
get: {},
form: {},
client: {}
client: {},
};
routes.client = {
main,
login,
register
register,
upload,
video: player
}
routes.get = {
};
routes.form = {
login: loginB,
register: registerB,
upload: uploadB,
auth
};

View file

@ -27,10 +27,10 @@
inkscape:zoom="2.8284272"
inkscape:cx="123.39013"
inkscape:cy="141.77491"
inkscape:window-width="1860"
inkscape:window-height="1004"
inkscape:window-x="30"
inkscape:window-y="46"
inkscape:window-width="1499"
inkscape:window-height="1080"
inkscape:window-x="0"
inkscape:window-y="16"
inkscape:window-maximized="1"
inkscape:current-layer="layer1"
showgrid="false" />
@ -44,8 +44,8 @@
offset="0"
id="stop6" />
<stop
style="stop-color:#ff1111;stop-opacity:1;"
offset="0.72731298"
style="stop-color:#da0000;stop-opacity:1;"
offset="0.38542777"
id="stop7" />
<stop
style="stop-color:#4f0000;stop-opacity:1;"
@ -62,19 +62,6 @@
fy="140.8663"
r="108.59156"
gradientUnits="userSpaceOnUse" />
<filter
inkscape:collect="always"
style="color-interpolation-filters:sRGB"
id="filter11"
x="-0.12659915"
y="-0.08366658"
width="1.2531983"
height="1.1673332">
<feGaussianBlur
inkscape:collect="always"
stdDeviation="3.8961098"
id="feGaussianBlur11" />
</filter>
</defs>
<g
inkscape:label="Layer 1"
@ -108,26 +95,5 @@
d="M 80.567225,178.82227 H 98.435368 L 81.928509,219.7296 h 17.061348"
id="path6" />
</g>
<g
id="g11"
style="mix-blend-mode:normal;filter:url(#filter11)"
transform="translate(15.185,1.7031898)">
<path
style="fill:none;fill-opacity:1;stroke:#ffffff;stroke-width:20;stroke-linecap:round;stroke-linejoin:round;stroke-dasharray:none;stroke-opacity:1"
d="M 20.63569,98.589129 H 72.37674 L 24.577583,181.3797 h 49.4048"
id="path8" />
<path
style="fill:none;fill-opacity:1;stroke:#ffffff;stroke-width:15;stroke-linecap:round;stroke-linejoin:round;stroke-dasharray:none;stroke-opacity:1"
d="M 34.506057,124.20157 61.311986,151.0075"
id="path9" />
<path
style="fill:none;fill-opacity:1;stroke:#ffffff;stroke-width:8.26156;stroke-linecap:round;stroke-linejoin:round;stroke-dasharray:none;stroke-opacity:1"
d="M -7.4993527,58.596639 H 10.36879 L -6.1380687,99.503969 H 10.923279"
id="path10" />
<path
style="fill:none;fill-opacity:1;stroke:#ffffff;stroke-width:8.26156;stroke-linecap:round;stroke-linejoin:round;stroke-dasharray:none;stroke-opacity:1"
d="M 80.567225,178.82227 H 98.435368 L 81.928509,219.7296 h 17.061348"
id="path11" />
</g>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 4.9 KiB

After

Width:  |  Height:  |  Size: 3.4 KiB

View file

@ -109,4 +109,8 @@ form {
.header img {
height: 2em;
}
video {
width: 690px;
}

View file

@ -4,8 +4,11 @@
<%if (username=='!nobody' ) { %>
<div class="button"><a href="/client/login">Login</a></div>
<% } else { %>
<a href="/client/user?user=<%= username %>">
<a href="/client/user?id=<%= username %>">
<%= username %>
</a>
<a href="/client/upload">
Upload
</a>
<% } %>
</div>

14
views/player.ejs Normal file
View file

@ -0,0 +1,14 @@
<%- include('header.ejs') -%>
<div class='area'>
<h1>Video</h1>
<video controls src='/videos/<%= videoData.id %>.mp4'></video>
<div><b>
<%= videoData.title %>
</b>
</div>
<div>by <a href="/client/user?id=<%= videoData.username %>">
<%= videoData.username %>
</a></div>
<!-- todo: custom video player-->
</div>
<%- include('footer.ejs') -%>