mirror of https://gitgud.io/fatchan/jschan.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
2.2 KiB
63 lines
2.2 KiB
5 years ago
|
'use strict';
|
||
|
|
||
4 years ago
|
const { outputFile } = require('fs-extra')
|
||
5 years ago
|
, formatSize = require(__dirname+'/files/formatsize.js')
|
||
5 years ago
|
, pug = require('pug')
|
||
|
, path = require('path')
|
||
4 years ago
|
, commit = require(__dirname+'/commit.js')
|
||
5 years ago
|
, uploadDirectory = require(__dirname+'/files/uploadDirectory.js')
|
||
4 years ago
|
, { hcaptcha, google } = require(__dirname+'/../configs/secrets.js')
|
||
5 years ago
|
, redlock = require(__dirname+'/../redlock.js')
|
||
4 years ago
|
, { addCallback } = require(__dirname+'/../redis.js')
|
||
4 years ago
|
, { version } = require(__dirname+'/../package.json')
|
||
5 years ago
|
, templateDirectory = path.join(__dirname+'/../views/pages/')
|
||
3 years ago
|
, Permissions = require(__dirname+'/permissions.js')
|
||
4 years ago
|
, config = require(__dirname+'/../config.js');
|
||
|
|
||
3 years ago
|
let { archiveLinksURL, lockWait, globalLimits, boardDefaults, cacheTemplates,
|
||
|
reverseImageLinksURL, meta, enableWebring, captchaOptions, globalAnnouncement } = config.get
|
||
4 years ago
|
, renderLocals = null;
|
||
|
|
||
3 years ago
|
const updateLocals = () => {
|
||
|
({ archiveLinksURL, lockWait, globalLimits, boardDefaults, cacheTemplates,
|
||
|
reverseImageLinksURL, meta, enableWebring, captchaOptions, globalAnnouncement } = config.get);
|
||
4 years ago
|
renderLocals = {
|
||
3 years ago
|
Permissions,
|
||
5 years ago
|
cache: cacheTemplates,
|
||
3 years ago
|
archiveLinksURL,
|
||
|
reverseImageLinksURL,
|
||
5 years ago
|
meta,
|
||
4 years ago
|
commit,
|
||
4 years ago
|
version,
|
||
5 years ago
|
defaultTheme: boardDefaults.theme,
|
||
5 years ago
|
defaultCodeTheme: boardDefaults.codeTheme,
|
||
5 years ago
|
postFilesSize: formatSize(globalLimits.postFilesSize.max),
|
||
5 years ago
|
globalLimits,
|
||
4 years ago
|
enableWebring,
|
||
4 years ago
|
captchaType: captchaOptions.type,
|
||
|
googleRecaptchaSiteKey: google.siteKey,
|
||
3 years ago
|
hcaptchaSiteKey: hcaptcha.siteKey,
|
||
4 years ago
|
captchaGridSize: captchaOptions.grid.size,
|
||
4 years ago
|
globalAnnouncement,
|
||
4 years ago
|
};
|
||
|
};
|
||
4 years ago
|
|
||
4 years ago
|
updateLocals();
|
||
|
addCallback('config', updateLocals);
|
||
4 years ago
|
|
||
|
module.exports = async (htmlName, templateName, options, json=null) => {
|
||
|
const html = pug.renderFile(`${templateDirectory}${templateName}`, {
|
||
|
...options,
|
||
|
...renderLocals,
|
||
5 years ago
|
});
|
||
5 years ago
|
const lock = await redlock.lock(`locks:${htmlName}`, lockWait);
|
||
5 years ago
|
const htmlPromise = outputFile(`${uploadDirectory}/html/${htmlName}`, html);
|
||
5 years ago
|
let jsonPromise;
|
||
|
if (json !== null) {
|
||
5 years ago
|
jsonPromise = outputFile(`${uploadDirectory}/json/${json.name}`, JSON.stringify(json.data));
|
||
5 years ago
|
}
|
||
|
await Promise.all([htmlPromise, jsonPromise]);
|
||
5 years ago
|
await lock.unlock();
|
||
4 years ago
|
return { html, json: json ? json.data : null };
|
||
5 years ago
|
};
|