2022-10-17 07:52:55 +00:00
|
|
|
import path from 'path'
|
|
|
|
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'
|
|
|
|
] })
|
|
|
|
}
|
|
|
|
|
|
|
|
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') || ''
|
|
|
|
let imageLink = ctx.query.get('i') || ''
|
|
|
|
|
|
|
|
if (ctx.url.match(/^\/[a-zA-Z0-9][a-zA-Z0-9][a-zA-Z0-9]+$/)) {
|
|
|
|
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 = res.first[0].video_link
|
|
|
|
imageLink = res.first[0].image_link
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
ctx.log.error(err)
|
|
|
|
ctx.state.error = 'Unknown error while fetching link.'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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'
|
|
|
|
}
|
|
|
|
}
|