2019-10-01 17:18:20 +00:00
|
|
|
import { readFileSync } from 'fs'
|
|
|
|
import dot from 'dot'
|
|
|
|
import striptags from 'striptags'
|
|
|
|
|
|
|
|
import config from './config.mjs'
|
|
|
|
import Page from './page/model.mjs'
|
|
|
|
import Article from './article/model.mjs'
|
|
|
|
|
|
|
|
const body = readFileSync('./public/index.html').toString()
|
|
|
|
const bodyTemplate = dot.template(body)
|
2019-10-01 17:36:28 +00:00
|
|
|
const frontend = config.get('frontend:url')
|
2019-10-01 17:18:20 +00:00
|
|
|
|
2019-10-02 17:40:32 +00:00
|
|
|
function mapArticle(trim = false, x, includeBanner = false, includeFiles = true) {
|
2019-10-01 17:18:20 +00:00
|
|
|
return {
|
|
|
|
id: x.id,
|
2019-10-02 01:50:31 +00:00
|
|
|
published_at: x.published_at,
|
2019-10-01 17:18:20 +00:00
|
|
|
path: x.path,
|
|
|
|
description: x.description,
|
|
|
|
name: x.name,
|
2019-10-02 18:47:20 +00:00
|
|
|
staff: x.staff && ({
|
|
|
|
id: x.staff.id,
|
|
|
|
fullname: x.staff.fullname,
|
|
|
|
}) || null,
|
2019-10-01 17:18:20 +00:00
|
|
|
media: x.media && ({
|
2019-10-02 17:40:32 +00:00
|
|
|
link: !trim && x.media.link || null,
|
2019-10-01 17:18:20 +00:00
|
|
|
large_url: x.media.large_url,
|
2021-01-04 17:47:59 +00:00
|
|
|
large_url_avif: x.media.large_url_avif,
|
2019-10-01 17:18:20 +00:00
|
|
|
medium_url: x.media.medium_url,
|
2021-01-04 17:47:59 +00:00
|
|
|
medium_url_avif: x.media.medium_url_avif,
|
2019-10-01 17:18:20 +00:00
|
|
|
small_url: x.media.small_url,
|
2021-01-04 17:47:59 +00:00
|
|
|
small_url_avif: x.media.small_url_avif,
|
2019-10-01 17:18:20 +00:00
|
|
|
}) || null,
|
2019-10-02 17:40:32 +00:00
|
|
|
banner: x.banner && includeBanner && ({
|
2019-10-01 17:18:20 +00:00
|
|
|
large_url: x.banner.large_url,
|
2021-01-04 17:47:59 +00:00
|
|
|
large_url_avif: x.banner.large_url_avif,
|
2019-10-01 17:18:20 +00:00
|
|
|
medium_url: x.banner.medium_url,
|
2021-01-04 17:47:59 +00:00
|
|
|
medium_url_avif: x.banner.medium_url_avif,
|
2019-10-01 17:18:20 +00:00
|
|
|
small_url: x.banner.small_url,
|
2021-01-04 17:47:59 +00:00
|
|
|
small_url_avif: x.banner.small_url_avif,
|
2019-10-01 17:18:20 +00:00
|
|
|
}) || null,
|
|
|
|
parent: x.parent && ({
|
|
|
|
id: x.parent.id,
|
|
|
|
name: x.parent.name,
|
|
|
|
path: x.parent.path,
|
|
|
|
}),
|
2019-10-02 17:40:32 +00:00
|
|
|
files: x.files && includeFiles && x.files.map(f => ({
|
2019-10-01 17:18:20 +00:00
|
|
|
filename: f.filename,
|
|
|
|
url: f.url,
|
|
|
|
magnet: f.magnet,
|
|
|
|
meta: f.meta.torrent && ({
|
|
|
|
torrent: {
|
2019-10-02 17:40:32 +00:00
|
|
|
name: f.meta.torrent.name,
|
|
|
|
files: f.meta.torrent.files.map(tf => {
|
|
|
|
if (trim && f.meta.torrent.files.length > 4) return 1
|
|
|
|
return {
|
|
|
|
name: tf.name,
|
|
|
|
size: tf.size,
|
|
|
|
}
|
|
|
|
}),
|
2019-10-01 17:18:20 +00:00
|
|
|
},
|
|
|
|
}) || {},
|
|
|
|
})) || [],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function mapPage(x) {
|
|
|
|
return {
|
|
|
|
id: x.id,
|
|
|
|
created_at: x.created_at,
|
|
|
|
path: x.path,
|
|
|
|
description: x.description,
|
|
|
|
name: x.name,
|
|
|
|
media: x.media && ({
|
2019-10-02 17:40:32 +00:00
|
|
|
link: x.media.link,
|
2019-10-01 17:18:20 +00:00
|
|
|
large_url: x.media.large_url,
|
2021-01-04 17:47:59 +00:00
|
|
|
large_url_avif: x.media.large_url_avif,
|
2019-10-01 17:18:20 +00:00
|
|
|
medium_url: x.media.medium_url,
|
2021-01-04 17:47:59 +00:00
|
|
|
medium_url_avif: x.media.medium_url_avif,
|
2019-10-01 17:18:20 +00:00
|
|
|
small_url: x.media.small_url,
|
2021-01-04 17:47:59 +00:00
|
|
|
small_url_avif: x.media.small_url_avif,
|
2019-10-01 17:18:20 +00:00
|
|
|
}) || null,
|
2019-10-02 19:25:57 +00:00
|
|
|
parent: x.parent && ({
|
|
|
|
id: x.parent.id,
|
|
|
|
name: x.parent.name,
|
|
|
|
path: x.parent.path,
|
|
|
|
}),
|
2019-10-01 17:18:20 +00:00
|
|
|
banner: x.banner && ({
|
|
|
|
large_url: x.banner.large_url,
|
2021-01-04 17:47:59 +00:00
|
|
|
large_url_avif: x.banner.large_url_avif,
|
2019-10-01 17:18:20 +00:00
|
|
|
medium_url: x.banner.medium_url,
|
2021-01-04 17:47:59 +00:00
|
|
|
medium_url_avif: x.banner.medium_url_avif,
|
2019-10-01 17:18:20 +00:00
|
|
|
small_url: x.banner.small_url,
|
2021-01-04 17:47:59 +00:00
|
|
|
small_url_avif: x.banner.small_url_avif,
|
2019-10-01 17:18:20 +00:00
|
|
|
}) || null,
|
|
|
|
children: x.children && x.children.map(f => ({
|
|
|
|
id: f.id,
|
|
|
|
path: f.path,
|
|
|
|
name: f.name,
|
|
|
|
})) || [],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function serveIndex(ctx, path) {
|
|
|
|
let tree = null
|
|
|
|
let data = null
|
|
|
|
let links = null
|
2019-10-02 00:16:11 +00:00
|
|
|
let featured = null
|
2019-10-01 17:36:28 +00:00
|
|
|
let url = frontend + ctx.request.url
|
2021-01-04 17:47:59 +00:00
|
|
|
let image = frontend + '/assets/img/heart.png'
|
|
|
|
let image_avif = frontend + '/assets/img/heart.png'
|
2019-10-01 17:18:20 +00:00
|
|
|
let title = 'NFP Moe - Anime/Manga translation group'
|
|
|
|
let description = 'Small fansubbing and scanlation group translating and encoding our favourite shows from Japan.'
|
|
|
|
try {
|
|
|
|
tree = (await Page.getTree()).toJSON()
|
|
|
|
tree.forEach(item => (
|
|
|
|
item.children = item.children.map(x => (
|
|
|
|
{ id: x.id, name: x.name, path: x.path }
|
|
|
|
))
|
|
|
|
))
|
2019-10-02 18:47:20 +00:00
|
|
|
featured = await Article.getFeatured(['media', 'banner'])
|
2019-10-02 00:16:11 +00:00
|
|
|
if (featured) {
|
2019-10-02 17:40:32 +00:00
|
|
|
featured = mapArticle(true, featured.toJSON(), true, false)
|
2019-10-02 00:16:11 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 17:18:20 +00:00
|
|
|
if (path === '/') {
|
2019-10-02 21:48:50 +00:00
|
|
|
let currPage = Number(ctx.query.page || '1')
|
|
|
|
data = await Article.getFrontpageArticles(currPage)
|
2019-10-01 17:18:20 +00:00
|
|
|
|
|
|
|
if (data.pagination.rowCount > 10) {
|
|
|
|
links = {
|
2019-10-02 21:48:50 +00:00
|
|
|
current: { title: 'Page ' + currPage },
|
2019-10-01 17:18:20 +00:00
|
|
|
next: { page: 2, title: 'Next' },
|
|
|
|
last: { page: Math.ceil(data.pagination.rowCount / 10), title: 'Last' },
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
links = {
|
|
|
|
current: { title: 'Page 1' },
|
|
|
|
}
|
|
|
|
}
|
2019-10-02 21:48:50 +00:00
|
|
|
if (currPage > 1) {
|
|
|
|
links.previous = { page: currPage - 1, title: 'Previous' }
|
|
|
|
links.first = { page: 1, title: 'First' }
|
|
|
|
}
|
2019-10-02 17:40:32 +00:00
|
|
|
data = data.toJSON().map(mapArticle.bind(null, true))
|
2019-10-01 17:18:20 +00:00
|
|
|
} else if (path.startsWith('/article/') || path.startsWith('/page/')) {
|
|
|
|
let id = path.split('/')[2]
|
|
|
|
if (id) {
|
|
|
|
let found
|
|
|
|
if (path.startsWith('/article/')) {
|
2019-10-02 18:47:20 +00:00
|
|
|
found = await Article.getSingle(id, ['media', 'parent', 'banner', 'files', 'staff'], false, null, true)
|
2019-10-02 00:16:11 +00:00
|
|
|
if (found) {
|
2019-10-02 17:40:32 +00:00
|
|
|
found = mapArticle(false, found.toJSON())
|
2019-10-02 00:16:11 +00:00
|
|
|
}
|
2019-10-01 17:18:20 +00:00
|
|
|
data = found
|
|
|
|
} else {
|
2019-10-02 19:25:57 +00:00
|
|
|
found = await Page.getSingle(id, ['media', 'banner', 'children', 'parent'])
|
2019-10-01 17:18:20 +00:00
|
|
|
found = mapPage(found.toJSON())
|
|
|
|
data = found
|
|
|
|
}
|
2019-10-02 00:16:11 +00:00
|
|
|
if (found) {
|
|
|
|
if (found.media) {
|
|
|
|
image = found.media.large_url
|
2021-01-04 17:47:59 +00:00
|
|
|
image_avif = found.media.large_url_avifl
|
2019-10-02 00:16:11 +00:00
|
|
|
} else if (found.banner) {
|
|
|
|
image = found.banner.large_url
|
2021-01-04 17:47:59 +00:00
|
|
|
image_avif = found.banner.large_url_avifl
|
2019-10-02 00:16:11 +00:00
|
|
|
}
|
|
|
|
if (found.description) {
|
|
|
|
description = striptags(found.description)
|
|
|
|
}
|
|
|
|
if (found.parent) {
|
|
|
|
title = found.name + ' - ' + found.parent.name + ' - NFP Moe'
|
|
|
|
} else {
|
|
|
|
title = found.name + ' - NFP Moe'
|
|
|
|
}
|
2019-10-01 17:18:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
ctx.log.error(e)
|
|
|
|
data = null
|
|
|
|
links = null
|
|
|
|
}
|
|
|
|
ctx.body = bodyTemplate({
|
|
|
|
v: config.get('CIRCLECI_VERSION'),
|
|
|
|
tree: JSON.stringify(tree),
|
|
|
|
data: JSON.stringify(data),
|
|
|
|
links: JSON.stringify(links),
|
2019-10-02 00:16:11 +00:00
|
|
|
featured: JSON.stringify(featured),
|
2019-10-01 17:36:28 +00:00
|
|
|
url: url,
|
2019-10-01 17:18:20 +00:00
|
|
|
image: image,
|
2021-01-04 17:47:59 +00:00
|
|
|
image_avif: image_avif,
|
2019-10-01 17:18:20 +00:00
|
|
|
title: title,
|
|
|
|
description: description,
|
|
|
|
})
|
|
|
|
ctx.set('Content-Length', Buffer.byteLength(ctx.body))
|
|
|
|
ctx.set('Cache-Control', 'max-age=0')
|
|
|
|
ctx.set('Content-Type', 'text/html; charset=utf-8')
|
|
|
|
}
|