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.
78 lines
2.1 KiB
78 lines
2.1 KiB
// site-controller.js
|
|
// Copyright (C) 2022,2024 DTP Technologies, LLC
|
|
// All Rights Reserved
|
|
|
|
'use strict';
|
|
|
|
import path from 'node:path';
|
|
|
|
import multer from 'multer';
|
|
import slugTool from 'slug';
|
|
|
|
import { SiteCommon } from './site-common.js';
|
|
import { SiteLog } from './site-log.js';
|
|
|
|
export class SiteController extends SiteCommon {
|
|
|
|
constructor (dtp, definition) {
|
|
super(dtp);
|
|
this.name = definition.name;
|
|
this.log = new SiteLog(dtp, definition);
|
|
|
|
this.children = { };
|
|
}
|
|
|
|
async loadChild (filename) {
|
|
const pathObj = path.parse(filename);
|
|
|
|
const ControllerClass = (await import(filename)).default;
|
|
if (!ControllerClass) {
|
|
this.log.error('failed to receive a default export class from child controller', { script: pathObj.name });
|
|
throw new Error('Child controller failed to provide a default export');
|
|
}
|
|
|
|
this.log.info('loading child controller', {
|
|
script: pathObj.name,
|
|
name: ControllerClass.name,
|
|
slug: ControllerClass.slug,
|
|
});
|
|
const controller = new ControllerClass(this.dtp);
|
|
|
|
this.children[ControllerClass.slug] = controller;
|
|
return controller.start();
|
|
}
|
|
|
|
getPaginationParameters (req, maxPerPage, pageParamName = 'p', cppParamName = 'cpp') {
|
|
const pagination = {
|
|
p: parseInt(req.query[pageParamName] || '1', 10),
|
|
cpp: parseInt(req.query[cppParamName] || maxPerPage.toString(), 10),
|
|
};
|
|
if (pagination.p < 1) {
|
|
pagination.p = 1;
|
|
}
|
|
if (pagination.cpp > maxPerPage) {
|
|
pagination.cpp = maxPerPage;
|
|
}
|
|
pagination.skip = (pagination.p - 1) * pagination.cpp;
|
|
return pagination;
|
|
}
|
|
|
|
createMulter (slug, options) {
|
|
if (!!slug && (typeof slug === 'object')) {
|
|
options = slug;
|
|
slug = this.name;
|
|
} else {
|
|
slug = slug || this.name;
|
|
}
|
|
slug = slugTool(slug).toLowerCase();
|
|
options = Object.assign({
|
|
dest: `/tmp/${this.dtp.config.site.domainKey}/${slug}`,
|
|
}, options || { });
|
|
return multer(options);
|
|
}
|
|
|
|
async createCsrfToken (req, name) {
|
|
const { csrfToken } = this.dtp.platform.services;
|
|
return csrfToken.create(req, { name });
|
|
}
|
|
}
|