1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2025-07-03 00:47:20 +02:00
Files
pigallery2/src/backend/middlewares/RenderingMWs.ts

122 lines
3.8 KiB
TypeScript
Raw Normal View History

2018-03-30 15:30:30 -04:00
import {NextFunction, Request, Response} from 'express';
import {ErrorCodes, ErrorDTO} from '../../common/entities/Error';
import {Message} from '../../common/entities/Message';
2020-01-28 18:36:52 +01:00
import {Config, PrivateConfigClass} from '../../common/config/private/Config';
2020-01-07 22:17:54 +01:00
import {UserDTO, UserRoles} from '../../common/entities/UserDTO';
2018-03-30 15:30:30 -04:00
import {NotificationManager} from '../model/NotifocationManager';
import {Logger} from '../Logger';
import {SharingDTO} from '../../common/entities/SharingDTO';
import {Utils} from '../../common/Utils';
import {LoggerRouter} from '../routes/LoggerRouter';
export class RenderingMWs {
public static renderResult(req: Request, res: Response, next: NextFunction): any {
if (typeof req.resultPipe === 'undefined') {
return next();
}
return RenderingMWs.renderMessage(res, req.resultPipe);
}
public static renderSessionUser(req: Request, res: Response, next: NextFunction): any {
if (!(req.session.user)) {
2018-03-30 15:30:30 -04:00
return next(new ErrorDTO(ErrorCodes.GENERAL_ERROR, 'User not exists'));
}
const user = {
2020-01-07 22:17:54 +01:00
id: req.session.user.id,
name: req.session.user.name,
csrfToken: req.session.user.csrfToken || req.csrfToken(),
role: req.session.user.role,
usedSharingKey: req.session.user.usedSharingKey,
permissions: req.session.user.permissions
} as UserDTO;
2020-01-07 22:17:54 +01:00
if (!user.csrfToken && req.csrfToken) {
user.csrfToken = req.csrfToken();
}
RenderingMWs.renderMessage(res, user);
}
public static renderSharing(req: Request, res: Response, next: NextFunction): any {
if (!req.resultPipe) {
2017-07-03 19:17:49 +02:00
return next();
}
2017-07-03 19:17:49 +02:00
2020-01-07 22:17:54 +01:00
const {password, creator, ...sharing} = req.resultPipe;
2017-07-03 19:17:49 +02:00
RenderingMWs.renderMessage(res, sharing);
}
public static renderSharingList(req: Request, res: Response, next: NextFunction): any {
if (!req.resultPipe) {
return next();
}
const shares: SharingDTO[] = Utils.clone(req.resultPipe);
shares.forEach((s): void => {
delete s.password;
delete s.creator.password;
});
return RenderingMWs.renderMessage(res, shares);
}
public static renderFile(req: Request, res: Response, next: NextFunction): any {
if (!req.resultPipe) {
return next();
}
2019-12-29 23:25:22 +01:00
return res.sendFile(req.resultPipe, {maxAge: 31536000, dotfiles: 'allow'});
}
public static renderOK(req: Request, res: Response, next: NextFunction): void {
const message = new Message<string>(null, 'ok');
res.json(message);
}
2017-07-08 12:43:42 +02:00
public static async renderConfig(req: Request, res: Response, next: NextFunction): Promise<void> {
2020-01-28 18:36:52 +01:00
const originalConf = await Config.original();
// These are sensitive information, do not send to the client side
originalConf.Server.sessionSecret = null;
originalConf.Server.Database.enforcedUsers = null;
const message = new Message<PrivateConfigClass>(null, originalConf.toJSON({
2020-02-04 19:37:47 +01:00
attachState: true,
attachVolatile: true
}) as any);
2017-07-08 12:43:42 +02:00
res.json(message);
}
public static renderError(err: any, req: Request, res: Response, next: NextFunction): any {
2017-07-09 14:23:50 +02:00
2017-07-15 12:47:11 +02:00
if (err instanceof ErrorDTO) {
2017-07-13 23:39:09 +02:00
if (err.details) {
2019-01-02 21:21:20 +01:00
Logger.warn('Handled error:');
LoggerRouter.log(Logger.warn, req, res);
2019-01-02 21:21:20 +01:00
console.log(err);
2019-12-10 14:50:20 +01:00
delete (err.details); // do not send back error object to the client side
// hide error details for non developers
if (!(req.session && req.session.user && req.session.user.role >= UserRoles.Developer)) {
2019-12-10 14:50:20 +01:00
delete (err.detailsStr);
2017-07-13 23:39:09 +02:00
}
2017-07-09 14:23:50 +02:00
}
const message = new Message<any>(err, null);
return res.json(message);
}
NotificationManager.error('Unknown server error', err, req);
return next(err);
}
protected static renderMessage<T>(res: Response, content: T): void {
const message = new Message<T>(null, content);
res.json(message);
}
}