2022-08-21 21:54:24 +00:00
|
|
|
import path from 'path'
|
2022-08-18 09:59:48 +00:00
|
|
|
import Parent from '../base/serve.mjs'
|
2022-08-21 21:54:24 +00:00
|
|
|
import fs from 'fs/promises'
|
|
|
|
import dot from 'dot'
|
2022-08-18 09:59:48 +00:00
|
|
|
|
|
|
|
export default class ServeHandler extends Parent {
|
2022-08-21 21:54:24 +00:00
|
|
|
traverseTree(set, tree) {
|
|
|
|
for (let branch of tree) {
|
|
|
|
if (branch.children && branch.children.length) {
|
|
|
|
set.add(branch.id)
|
|
|
|
this.traverseTree(set, branch.children)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-18 09:59:48 +00:00
|
|
|
async serveIndex(ctx) {
|
2022-08-21 21:54:24 +00:00
|
|
|
let indexFile = await fs.readFile(path.join(this.root, 'index.html'))
|
|
|
|
this.template = dot.template(indexFile.toString(), { argName: ['headerDescription', 'headerImage', 'headerTitle', 'headerUrl', 'payloadData', 'payloadTree', 'version', 'nonce', 'type', 'banner', 'media'] })
|
|
|
|
|
2022-08-18 09:59:48 +00:00
|
|
|
let payload = {
|
|
|
|
headerDescription: 'Small fansubbing and scanlation group translating and encoding our favourite shows from Japan.',
|
|
|
|
headerImage: this.frontend + '/assets/img/heart.png',
|
|
|
|
headerTitle: 'NFP Moe - Anime/Manga translation group',
|
|
|
|
headerUrl: this.frontend + ctx.url,
|
|
|
|
payloadData: null,
|
|
|
|
payloadTree: null,
|
|
|
|
version: this.version,
|
|
|
|
nonce: ctx.state.nonce,
|
2022-08-21 21:54:24 +00:00
|
|
|
type: 'page',
|
|
|
|
banner: false,
|
|
|
|
media: false,
|
2022-08-18 09:59:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-08-21 21:54:24 +00:00
|
|
|
let tree = await this.pageRoutes.getPageTree(ctx, true)
|
|
|
|
let setOfBranches = new Set()
|
|
|
|
this.traverseTree(setOfBranches, tree.tree)
|
|
|
|
|
|
|
|
payload.payloadTree = JSON.stringify(tree)
|
2022-08-18 09:59:48 +00:00
|
|
|
if (ctx.url === '/' || (ctx.url.startsWith('/page/') && ctx.url.lastIndexOf('/') === 5)) {
|
|
|
|
ctx.params.path = null
|
|
|
|
if (ctx.url.lastIndexOf('/') === 5) {
|
|
|
|
ctx.params.path = ctx.url.slice(ctx.url.lastIndexOf('/') + 1)
|
|
|
|
}
|
2022-08-21 21:54:24 +00:00
|
|
|
let data = await this.pageRoutes.getPage(ctx, true)
|
|
|
|
if (!data.page) {
|
|
|
|
payload.type = 'frontpage'
|
|
|
|
}
|
|
|
|
else if (setOfBranches.has(data.page.id)) {
|
|
|
|
payload.type = 'page_with_children'
|
|
|
|
}
|
|
|
|
payload.media = data.page?.media_avif_preview || false
|
|
|
|
payload.banner = data.featured?.banner_avif_preview || data.page?.banner_avif_preview || false
|
|
|
|
payload.payloadData = JSON.stringify(data)
|
|
|
|
} else if (ctx.url.startsWith('/article/') && ctx.url.lastIndexOf('/') === 8) {
|
|
|
|
ctx.params.path = ctx.url.slice(ctx.url.lastIndexOf('/') + 1)
|
|
|
|
let data = await this.articleRoutes.getArticle(ctx, true)
|
|
|
|
payload.media = data.article?.media_avif_preview || false
|
|
|
|
payload.payloadData = JSON.stringify(data)
|
|
|
|
payload.type = 'article'
|
2022-08-18 09:59:48 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
ctx.log.error(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.body = this.template(payload)
|
|
|
|
ctx.type = 'text/html; charset=utf-8'
|
|
|
|
}
|
|
|
|
}
|