76 lines
2 KiB
JavaScript
76 lines
2 KiB
JavaScript
import fs from 'fs'
|
|
import express from 'express'
|
|
import {
|
|
randomInputFile, checkUserAgent,
|
|
setupInputs, setupThumbnail,
|
|
setupThumbnailVideo,
|
|
getInputs
|
|
} from './src/utils.js'
|
|
import { addTextOverlay } from './src/render.js'
|
|
const app = express()
|
|
const PORT = process.env.PORT ?? 25825
|
|
|
|
const buildPath = await fs.promises.realpath('build')
|
|
|
|
let botPage
|
|
|
|
app.get('/thumb.png', async (req, res) => {
|
|
res.setHeader('Content-Disposition', 'inline')
|
|
if (!fs.existsSync(`build/thumb-${req.hostname}.png`)) {
|
|
await setupThumbnail(req.hostname)
|
|
}
|
|
res.sendFile(`${buildPath}/thumb-${req.hostname}.png`)
|
|
})
|
|
|
|
app.get('/videos', (req, res) => {
|
|
res.json(getInputs().map(e => e.name))
|
|
})
|
|
|
|
async function handleVideoReq(req, res, file) {
|
|
let text
|
|
const userAgentType = checkUserAgent(req.headers['user-agent']??'')
|
|
switch (userAgentType) {
|
|
case 'bot':
|
|
if (!botPage) {
|
|
botPage = (await fs.promises.readFile('bot.html', 'utf-8')).replace(/%HOSTNAME%/gm, req.hostname.toLowerCase())
|
|
}
|
|
res.send(botPage)
|
|
break
|
|
case 'discord':
|
|
res.setHeader('Content-Disposition', 'inline')
|
|
if (!fs.existsSync(`build/thumb-${req.hostname}.mp4`)) {
|
|
await setupThumbnailVideo(req.hostname)
|
|
}
|
|
res.sendFile(`${buildPath}/thumb-${req.hostname}.mp4`)
|
|
break
|
|
case 'normal':
|
|
text = `${req.ip}\nlmao`
|
|
res.contentType('mp4')
|
|
res.setHeader('Content-Disposition', 'inline')
|
|
const renderProcess = await addTextOverlay(file??randomInputFile(), text)
|
|
req.socket.on('close', renderProcess.kill)
|
|
renderProcess.stream.pipe(res)
|
|
break
|
|
}
|
|
}
|
|
|
|
app.get('/:input/*', (req, res, next) => {
|
|
const inputs = getInputs()
|
|
const matching = inputs.find(e => e.path==`inputs/${req.params.input}.mp4`)
|
|
if (matching) {
|
|
handleVideoReq(req, res, matching)
|
|
return
|
|
}
|
|
next()
|
|
})
|
|
|
|
app.get('/*', (req, res) => handleVideoReq(req, res))
|
|
|
|
app.set('trust proxy', 'loopback')
|
|
|
|
app.listen(PORT, '127.0.0.1', async () => {
|
|
console.log('Getting input file durations (used for compression)')
|
|
await setupInputs()
|
|
console.log('Ready')
|
|
console.log(`Listening on http://127.0.0.1:${PORT}`)
|
|
}) |