bigly-chat/form/upload.js
2024-10-28 04:54:58 -04:00

44 lines
No EOL
1.3 KiB
JavaScript

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";
import captcha from "./captcha.js";
const execP = promisify(exec);
let db = await initDb();
// TODO: rewrite
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
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'}
}
await db.run('INSERT INTO video (id, title, desc, username, date) VALUES (?, ?, ?, ?, ?)', [
id,
title,
desc,
username,
+new Date()
]);
return { 'message': 'Video created', 'success': true, 'redirect': '/client/video?id='+id };
});
export default main;