2022-10-17 07:52:55 +00:00
|
|
|
import path from 'path'
|
2023-01-26 09:33:07 +00:00
|
|
|
import { HttpError } from 'flaska'
|
2022-10-17 07:52:55 +00:00
|
|
|
import Parent from '../base/serve.mjs'
|
|
|
|
import fs from 'fs/promises'
|
|
|
|
import fsSync from 'fs'
|
|
|
|
import dot from 'dot'
|
|
|
|
import config from '../base/config.mjs'
|
2022-10-17 14:13:20 +00:00
|
|
|
import AlphabeticID from './id.mjs'
|
2022-10-17 07:52:55 +00:00
|
|
|
|
|
|
|
export default class ServeHandler extends Parent {
|
|
|
|
loadTemplate(indexFile) {
|
|
|
|
this.template = dot.template(indexFile.toString(), { argName: [
|
|
|
|
'imageLink',
|
|
|
|
'videoLink',
|
|
|
|
'error',
|
|
|
|
'siteUrl',
|
|
|
|
'siteUrlBase',
|
|
|
|
'version',
|
|
|
|
'nonce',
|
|
|
|
'in_debug',
|
|
|
|
'inputVideo',
|
|
|
|
'inputImage'
|
2023-11-07 20:48:03 +00:00
|
|
|
], strip: false })
|
2022-10-17 07:52:55 +00:00
|
|
|
}
|
|
|
|
|
2023-01-26 09:33:07 +00:00
|
|
|
register(server) {
|
|
|
|
super.register(server)
|
|
|
|
server.flaska.onerror(this.serveError.bind(this))
|
|
|
|
}
|
|
|
|
|
|
|
|
serveError(err, ctx) {
|
|
|
|
ctx.log.error(err)
|
|
|
|
|
|
|
|
if (err instanceof HttpError) {
|
|
|
|
ctx.status = err.status
|
|
|
|
ctx.state.error = err.message
|
|
|
|
} else {
|
|
|
|
ctx.status = 500
|
|
|
|
ctx.state.error = 'Unknown error occured'
|
|
|
|
}
|
|
|
|
|
|
|
|
let videoLink = ctx.query.get('v') || ''
|
|
|
|
let imageLink = ctx.query.get('i') || ''
|
|
|
|
|
|
|
|
ctx.body = this.template({
|
|
|
|
videoLink: videoLink,
|
|
|
|
imageLink: imageLink,
|
|
|
|
error: ctx.state.error || '',
|
|
|
|
inputVideo: ctx.state.video || videoLink || '',
|
|
|
|
inputImage: ctx.state.image || imageLink || '',
|
|
|
|
siteUrl: this.frontend + ctx.url,
|
|
|
|
siteUrlBase: this.frontend + '/',
|
|
|
|
version: this.version,
|
|
|
|
nonce: ctx.state.nonce,
|
|
|
|
in_debug: config.get('NODE_ENV') === 'development' && false,
|
|
|
|
})
|
|
|
|
ctx.type = 'text/html; charset=utf-8'
|
|
|
|
}
|
|
|
|
|
2022-10-17 07:52:55 +00:00
|
|
|
async serveIndex(ctx) {
|
|
|
|
if (config.get('NODE_ENV') === 'development') {
|
|
|
|
let indexFile = await fs.readFile(path.join(this.root, 'index.html'))
|
|
|
|
this.loadTemplate(indexFile)
|
|
|
|
}
|
|
|
|
|
2022-10-17 14:13:20 +00:00
|
|
|
let videoLink = ctx.query.get('v') || ''
|
2023-04-14 09:36:04 +00:00
|
|
|
let imageLink = ctx.query.get('i') || (videoLink ? 'https://cdn.nfp.is/av1/empty.png' : '')
|
2022-10-17 14:13:20 +00:00
|
|
|
|
2023-01-26 09:33:07 +00:00
|
|
|
if (!ctx.state.error) {
|
|
|
|
if (ctx.url.match(/^\/[a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9]+$/) && ctx.url.length < 7) {
|
|
|
|
try {
|
|
|
|
let id = AlphabeticID.decode(ctx.url.slice(1))
|
|
|
|
if (id) {
|
|
|
|
let res = await ctx.db.safeCallProc('discord_embed.link_get', [id - 3843])
|
|
|
|
if (res.first.length) {
|
|
|
|
videoLink = ctx.state.video = res.first[0].video_link
|
|
|
|
if (!ctx.state.video.startsWith('https://cdn.discordapp.com')
|
|
|
|
&& !ctx.state.video.includes('catbox.')
|
|
|
|
&& ctx.state.video.includes('?')) {
|
|
|
|
videoLink = this.frontend + '/video/' + ctx.url.slice(1) + '.webm'
|
|
|
|
}
|
|
|
|
imageLink = res.first[0].image_link
|
|
|
|
} else {
|
|
|
|
ctx.status = 404
|
2022-11-30 13:54:17 +00:00
|
|
|
}
|
2022-10-17 14:13:20 +00:00
|
|
|
}
|
2023-01-26 09:33:07 +00:00
|
|
|
} catch (err) {
|
|
|
|
ctx.log.error(err, 'Unable to fetch resource ' + ctx.url.slice(1))
|
|
|
|
ctx.state.error = 'Unknown error while fetching link.'
|
2022-10-17 14:13:20 +00:00
|
|
|
}
|
2023-01-26 09:33:07 +00:00
|
|
|
} else if (ctx.url !== '/') {
|
|
|
|
ctx.status = 404
|
2022-10-17 14:13:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-30 15:35:27 +00:00
|
|
|
if (videoLink.startsWith('https://cdn.discordapp.com')) {
|
|
|
|
videoLink = videoLink.replace('https://cdn.discordapp.com', 'https://discordproxy.nfp.is')
|
|
|
|
}
|
|
|
|
|
2022-10-17 07:52:55 +00:00
|
|
|
let payload = {
|
2022-10-17 14:13:20 +00:00
|
|
|
videoLink: videoLink,
|
|
|
|
imageLink: imageLink,
|
2022-10-17 07:52:55 +00:00
|
|
|
error: ctx.state.error || '',
|
2022-10-17 14:13:20 +00:00
|
|
|
inputVideo: ctx.state.video || videoLink || '',
|
|
|
|
inputImage: ctx.state.image || imageLink || '',
|
2022-10-17 07:52:55 +00:00
|
|
|
siteUrl: this.frontend + ctx.url,
|
|
|
|
siteUrlBase: this.frontend + '/',
|
|
|
|
version: this.version,
|
|
|
|
nonce: ctx.state.nonce,
|
|
|
|
in_debug: config.get('NODE_ENV') === 'development' && false,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.body = this.template(payload)
|
|
|
|
ctx.type = 'text/html; charset=utf-8'
|
|
|
|
}
|
|
|
|
}
|