2024-11-25 14:12:43 -05:00
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" ;
2024-11-25 14:12:44 -05:00
import captcha from "./captcha.js" ;
2024-11-25 14:12:43 -05:00
const execP = promisify ( exec ) ;
let db = await initDb ( ) ;
// TODO: rewrite
2024-11-25 14:12:44 -05:00
let main = new Route ( [ auth , captcha ] , async function ( req , res , input ) {
let { captchaMatch } = input ;
if ( ! captchaMatch ) return { 'success' : false , 'message' : 'Captcha is incorrect' } ;
2024-11-25 14:12:43 -05:00
let { username } = input ;
let id = randomUUID ( ) ;
let { title , desc } = req . body ;
2024-11-25 14:12:43 -05:00
if ( ! title || ! desc || ! req . file || username == '!nobody' ) return { 'success' : false , 'message' : 'Some fields are missing' } ; // probably should not re-use these strings
2024-11-25 14:12:43 -05:00
let { path } = req . file ;
try {
2024-11-25 14:12:43 -05:00
await execP ( ` ffmpeg -i ${ path } videos/ ${ id } .webm ` ) ;
await execP ( ` ffmpeg -i videos/ ${ id } .webm -frames:v 1 videos/ ${ id } .png ` ) ;
2024-11-25 14:12:43 -05:00
} catch ( err ) {
return { 'success' : false , 'message' : 'Video is invalid' }
}
2024-11-25 14:12:43 -05:00
await db . run ( 'INSERT INTO video (id, title, desc, username, date) VALUES (?, ?, ?, ?, ?)' , [
2024-11-25 14:12:43 -05:00
id ,
title ,
desc ,
2024-11-25 14:12:43 -05:00
username ,
+ new Date ( )
2024-11-25 14:12:43 -05:00
] ) ;
return { 'message' : 'Video created' , 'success' : true , 'redirect' : '/client/video?id=' + id } ;
} ) ;
export default main ;