DTP Base provides a scalable and secure Node.js application development harness ready for production service.
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.
 
 
 
 

65 lines
1.6 KiB

// site-controller.js
// Copyright (C) 2022,2024 DTP Technologies, LLC
// All Rights Reserved
'use strict';
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) {
let child = await require(filename);
this.children[child.slug] = child;
let instance = child.create(this.dtp);
return await instance.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 });
}
}