2022-12-28 22:12:53 +01:00
|
|
|
import {NextFunction, Request, Response} from 'express';
|
|
|
|
import {ErrorCodes, ErrorDTO} from '../../common/entities/Error';
|
|
|
|
import {Message} from '../../common/entities/Message';
|
2024-03-25 21:38:09 +01:00
|
|
|
import {PrivateConfigClass} from '../../common/config/private/PrivateConfigClass';
|
2022-12-28 22:12:53 +01:00
|
|
|
import {UserDTO, UserRoles} from '../../common/entities/UserDTO';
|
|
|
|
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';
|
|
|
|
import {TAGS} from '../../common/config/public/ClientConfig';
|
2023-11-13 16:51:25 +01:00
|
|
|
import {ExtensionConfigWrapper} from '../model/extension/ExtensionConfigWrapper';
|
2016-03-26 11:19:10 +01:00
|
|
|
|
2023-01-08 11:09:16 +01:00
|
|
|
const forcedDebug = process.env['NODE_ENV'] === 'debug';
|
|
|
|
|
2016-03-26 11:19:10 +01:00
|
|
|
export class RenderingMWs {
|
2022-04-04 19:37:31 +02:00
|
|
|
public static renderResult(
|
2023-10-14 19:06:12 +02:00
|
|
|
req: Request,
|
|
|
|
res: Response,
|
|
|
|
next: NextFunction
|
2022-04-25 18:09:06 +02:00
|
|
|
): void {
|
2018-05-12 12:19:51 -04:00
|
|
|
if (typeof req.resultPipe === 'undefined') {
|
2017-06-10 22:32:56 +02:00
|
|
|
return next();
|
2018-05-12 12:19:51 -04:00
|
|
|
}
|
2016-03-26 11:19:10 +01:00
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
return RenderingMWs.renderMessage(res, req.resultPipe);
|
|
|
|
}
|
2016-03-26 11:19:10 +01:00
|
|
|
|
2022-04-04 19:37:31 +02:00
|
|
|
public static renderSessionUser(
|
2023-10-14 19:06:12 +02:00
|
|
|
req: Request,
|
|
|
|
res: Response,
|
|
|
|
next: NextFunction
|
2022-04-25 18:09:06 +02:00
|
|
|
): void {
|
2022-04-04 19:37:31 +02:00
|
|
|
if (!req.session['user']) {
|
2018-03-30 15:30:30 -04:00
|
|
|
return next(new ErrorDTO(ErrorCodes.GENERAL_ERROR, 'User not exists'));
|
2016-03-26 11:19:10 +01:00
|
|
|
}
|
|
|
|
|
2021-04-18 15:48:35 +02:00
|
|
|
const user = {
|
2022-04-04 19:37:31 +02: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,
|
2021-04-18 15:48:35 +02:00
|
|
|
} as UserDTO;
|
2020-01-07 22:17:54 +01:00
|
|
|
|
|
|
|
if (!user.csrfToken && req.csrfToken) {
|
|
|
|
user.csrfToken = req.csrfToken();
|
|
|
|
}
|
|
|
|
|
2017-06-10 22:32:56 +02:00
|
|
|
RenderingMWs.renderMessage(res, user);
|
|
|
|
}
|
2016-03-26 11:19:10 +01:00
|
|
|
|
2022-04-04 19:37:31 +02:00
|
|
|
public static renderSharing(
|
2023-10-14 19:06:12 +02:00
|
|
|
req: Request,
|
|
|
|
res: Response,
|
|
|
|
next: NextFunction
|
2022-04-25 18:09:06 +02:00
|
|
|
): void {
|
2018-05-12 12:19:51 -04:00
|
|
|
if (!req.resultPipe) {
|
2017-07-03 19:17:49 +02:00
|
|
|
return next();
|
2018-05-12 12:19:51 -04:00
|
|
|
}
|
2017-07-03 19:17:49 +02:00
|
|
|
|
2022-12-10 00:32:31 +01:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2022-12-28 22:12:53 +01:00
|
|
|
const {password, creator, ...sharing} = req.resultPipe as SharingDTO;
|
2017-07-03 19:17:49 +02:00
|
|
|
RenderingMWs.renderMessage(res, sharing);
|
|
|
|
}
|
|
|
|
|
2022-04-04 19:37:31 +02:00
|
|
|
public static renderSharingList(
|
2023-10-14 19:06:12 +02:00
|
|
|
req: Request,
|
|
|
|
res: Response,
|
|
|
|
next: NextFunction
|
2022-04-25 18:09:06 +02:00
|
|
|
): void {
|
2020-09-06 14:44:25 +02:00
|
|
|
if (!req.resultPipe) {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
2022-04-25 18:09:06 +02:00
|
|
|
const shares = Utils.clone(req.resultPipe as SharingDTO[]);
|
2021-04-18 15:48:35 +02:00
|
|
|
shares.forEach((s): void => {
|
2020-09-06 16:12:30 +02:00
|
|
|
delete s.password;
|
|
|
|
delete s.creator.password;
|
|
|
|
});
|
|
|
|
return RenderingMWs.renderMessage(res, shares);
|
2020-09-06 14:44:25 +02:00
|
|
|
}
|
|
|
|
|
2022-04-04 19:37:31 +02:00
|
|
|
public static renderFile(
|
2023-10-14 19:06:12 +02:00
|
|
|
req: Request,
|
|
|
|
res: Response,
|
|
|
|
next: NextFunction
|
2022-04-25 18:09:06 +02:00
|
|
|
): void {
|
2018-05-12 12:19:51 -04:00
|
|
|
if (!req.resultPipe) {
|
2017-06-10 22:32:56 +02:00
|
|
|
return next();
|
2018-05-12 12:19:51 -04:00
|
|
|
}
|
2022-04-25 18:09:06 +02:00
|
|
|
return res.sendFile(req.resultPipe as string, {
|
2022-04-04 19:37:31 +02:00
|
|
|
maxAge: 31536000,
|
|
|
|
dotfiles: 'allow',
|
|
|
|
});
|
2017-06-10 22:32:56 +02:00
|
|
|
}
|
|
|
|
|
2022-04-04 19:37:31 +02:00
|
|
|
public static renderOK(
|
2023-10-14 19:06:12 +02:00
|
|
|
req: Request,
|
|
|
|
res: Response
|
2022-04-04 19:37:31 +02:00
|
|
|
): void {
|
2018-05-12 12:19:51 -04:00
|
|
|
const message = new Message<string>(null, 'ok');
|
2017-06-10 22:32:56 +02:00
|
|
|
res.json(message);
|
|
|
|
}
|
2016-03-26 11:19:10 +01:00
|
|
|
|
2022-04-04 19:37:31 +02:00
|
|
|
public static async renderConfig(
|
2023-10-14 19:06:12 +02:00
|
|
|
req: Request,
|
|
|
|
res: Response
|
2022-04-04 19:37:31 +02:00
|
|
|
): Promise<void> {
|
2023-11-13 16:51:25 +01:00
|
|
|
const originalConf = await ExtensionConfigWrapper.original();
|
2022-01-14 11:02:17 +01:00
|
|
|
// These are sensitive information, do not send to the client side
|
2020-01-03 20:28:03 +01:00
|
|
|
originalConf.Server.sessionSecret = null;
|
2024-03-25 23:10:10 +01:00
|
|
|
const originalConfJSON = JSON.parse(JSON.stringify(originalConf.toJSON({
|
|
|
|
attachState: true,
|
|
|
|
attachVolatile: true,
|
|
|
|
skipTags: {secret: true} as TAGS
|
|
|
|
}) as PrivateConfigClass));
|
|
|
|
|
2022-04-04 19:37:31 +02:00
|
|
|
const message = new Message<PrivateConfigClass>(
|
2023-10-14 19:06:12 +02:00
|
|
|
null,
|
2024-03-25 23:10:10 +01:00
|
|
|
originalConfJSON
|
2022-04-04 19:37:31 +02:00
|
|
|
);
|
2017-07-08 12:43:42 +02:00
|
|
|
res.json(message);
|
|
|
|
}
|
|
|
|
|
2022-04-04 19:37:31 +02:00
|
|
|
public static renderError(
|
2023-10-14 19:06:12 +02:00
|
|
|
err: Error,
|
|
|
|
req: Request,
|
|
|
|
res: Response,
|
|
|
|
next: NextFunction
|
2022-04-25 18:09:06 +02:00
|
|
|
): void {
|
2017-07-15 12:47:11 +02:00
|
|
|
if (err instanceof ErrorDTO) {
|
2017-07-13 23:39:09 +02:00
|
|
|
if (err.details) {
|
2024-03-31 09:28:04 +11:00
|
|
|
LoggerRouter.log(logFn, req, res);
|
2023-10-14 19:06:12 +02:00
|
|
|
// use separate rendering for detailsStr
|
|
|
|
const d = err.detailsStr;
|
|
|
|
delete err.detailsStr;
|
2024-03-31 09:28:04 +11:00
|
|
|
console.log(err);
|
2023-10-14 19:06:12 +02:00
|
|
|
err.detailsStr = d;
|
2022-04-04 19:37:31 +02:00
|
|
|
delete err.details; // do not send back error object to the client side
|
2019-12-10 14:50:20 +01:00
|
|
|
|
|
|
|
// hide error details for non developers
|
2022-04-04 19:37:31 +02:00
|
|
|
if (
|
2023-10-14 19:06:12 +02:00
|
|
|
!(
|
|
|
|
forcedDebug ||
|
|
|
|
(req.session &&
|
|
|
|
req.session['user'] &&
|
|
|
|
req.session['user'].role >= UserRoles.Developer)
|
|
|
|
)
|
2022-04-04 19:37:31 +02:00
|
|
|
) {
|
|
|
|
delete err.detailsStr;
|
2017-07-13 23:39:09 +02:00
|
|
|
}
|
2017-07-09 14:23:50 +02:00
|
|
|
}
|
2022-04-25 18:09:06 +02:00
|
|
|
const message = new Message<null>(err, null);
|
|
|
|
res.json(message);
|
|
|
|
return;
|
2016-03-26 11:19:10 +01:00
|
|
|
}
|
2020-12-27 18:57:02 +01:00
|
|
|
NotificationManager.error('Unknown server error', err, req);
|
2017-06-10 22:32:56 +02:00
|
|
|
return next(err);
|
|
|
|
}
|
2016-03-26 11:19:10 +01:00
|
|
|
|
2021-04-18 15:48:35 +02:00
|
|
|
protected static renderMessage<T>(res: Response, content: T): void {
|
2018-05-12 12:19:51 -04:00
|
|
|
const message = new Message<T>(null, content);
|
2017-06-10 22:32:56 +02:00
|
|
|
res.json(message);
|
|
|
|
}
|
|
|
|
}
|