2018-03-31 03:30:30 +08:00
|
|
|
import {NextFunction, Request, Response} from 'express';
|
|
|
|
import {ErrorCodes, ErrorDTO} from '../../common/entities/Error';
|
|
|
|
import {Utils} from '../../common/Utils';
|
|
|
|
import {Message} from '../../common/entities/Message';
|
|
|
|
import {SharingDTO} from '../../common/entities/SharingDTO';
|
|
|
|
import {Config} from '../../common/config/private/Config';
|
|
|
|
import {PrivateConfigClass} from '../../common/config/private/PrivateConfigClass';
|
|
|
|
import {UserRoles} from '../../common/entities/UserDTO';
|
|
|
|
import {NotificationManager} from '../model/NotifocationManager';
|
|
|
|
import {Logger} from '../Logger';
|
2016-03-26 18:19:10 +08:00
|
|
|
|
|
|
|
export class RenderingMWs {
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public static renderResult(req: Request, res: Response, next: NextFunction) {
|
2018-05-13 00:19:51 +08:00
|
|
|
if (typeof req.resultPipe === 'undefined') {
|
2017-06-11 04:32:56 +08:00
|
|
|
return next();
|
2018-05-13 00:19:51 +08:00
|
|
|
}
|
2016-03-26 18:19:10 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
return RenderingMWs.renderMessage(res, req.resultPipe);
|
|
|
|
}
|
2016-03-26 18:19:10 +08:00
|
|
|
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public static renderSessionUser(req: Request, res: Response, next: NextFunction) {
|
|
|
|
if (!(req.session.user)) {
|
2018-03-31 03:30:30 +08:00
|
|
|
return next(new ErrorDTO(ErrorCodes.GENERAL_ERROR, 'User not exists'));
|
2016-03-26 18:19:10 +08:00
|
|
|
}
|
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
const user = Utils.clone(req.session.user);
|
2017-06-11 04:32:56 +08:00
|
|
|
delete user.password;
|
|
|
|
RenderingMWs.renderMessage(res, user);
|
|
|
|
}
|
2016-03-26 18:19:10 +08:00
|
|
|
|
2017-07-04 01:17:49 +08:00
|
|
|
public static renderSharing(req: Request, res: Response, next: NextFunction) {
|
2018-05-13 00:19:51 +08:00
|
|
|
if (!req.resultPipe) {
|
2017-07-04 01:17:49 +08:00
|
|
|
return next();
|
2018-05-13 00:19:51 +08:00
|
|
|
}
|
2017-07-04 01:17:49 +08:00
|
|
|
|
|
|
|
const sharing = Utils.clone<SharingDTO>(req.resultPipe);
|
|
|
|
delete sharing.password;
|
|
|
|
RenderingMWs.renderMessage(res, sharing);
|
|
|
|
}
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public static renderFile(req: Request, res: Response, next: NextFunction) {
|
2018-05-13 00:19:51 +08:00
|
|
|
if (!req.resultPipe) {
|
2017-06-11 04:32:56 +08:00
|
|
|
return next();
|
2018-05-13 00:19:51 +08:00
|
|
|
}
|
2016-03-26 18:19:10 +08:00
|
|
|
|
2017-07-28 06:04:19 +08:00
|
|
|
return res.sendFile(req.resultPipe, {maxAge: 31536000});
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public static renderOK(req: Request, res: Response, next: NextFunction) {
|
2018-05-13 00:19:51 +08:00
|
|
|
const message = new Message<string>(null, 'ok');
|
2017-06-11 04:32:56 +08:00
|
|
|
res.json(message);
|
|
|
|
}
|
2016-03-26 18:19:10 +08:00
|
|
|
|
2017-07-08 18:43:42 +08:00
|
|
|
|
|
|
|
public static renderConfig(req: Request, res: Response, next: NextFunction) {
|
2018-05-13 00:19:51 +08:00
|
|
|
const message = new Message<PrivateConfigClass>(null, Config.original());
|
2017-07-08 18:43:42 +08:00
|
|
|
res.json(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public static renderError(err: any, req: Request, res: Response, next: NextFunction): any {
|
2017-07-09 20:23:50 +08:00
|
|
|
|
2017-07-15 18:47:11 +08:00
|
|
|
if (err instanceof ErrorDTO) {
|
2017-07-14 05:39:09 +08:00
|
|
|
if (err.details) {
|
2018-05-24 10:42:50 +08:00
|
|
|
Logger.warn('Handled error:', err);
|
2017-07-14 05:39:09 +08:00
|
|
|
if (!(req.session.user && req.session.user.role >= UserRoles.Developer)) {
|
|
|
|
delete (err.details);
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
err.details = err.details.toString() || err.details;
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
}
|
2017-07-09 20:23:50 +08:00
|
|
|
}
|
2018-05-13 00:19:51 +08:00
|
|
|
const message = new Message<any>(err, null);
|
2017-06-11 04:32:56 +08:00
|
|
|
return res.json(message);
|
2016-03-26 18:19:10 +08:00
|
|
|
}
|
2018-03-31 03:30:30 +08:00
|
|
|
NotificationManager.error('unknown server error', err);
|
2017-06-11 04:32:56 +08:00
|
|
|
return next(err);
|
|
|
|
}
|
2016-03-26 18:19:10 +08:00
|
|
|
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
protected static renderMessage<T>(res: Response, content: T) {
|
2018-05-13 00:19:51 +08:00
|
|
|
const message = new Message<T>(null, content);
|
2017-06-11 04:32:56 +08:00
|
|
|
res.json(message);
|
|
|
|
}
|
2016-03-26 18:19:10 +08:00
|
|
|
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|