1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/backend/middlewares/RenderingMWs.ts

90 lines
2.7 KiB
TypeScript
Raw Normal View History

import {NextFunction, Request, Response} from "express";
import {Error, ErrorCodes} from "../../common/entities/Error";
import {Utils} from "../../common/Utils";
import {Message} from "../../common/entities/Message";
2017-07-04 01:17:49 +08:00
import {SharingDTO} from "../../common/entities/SharingDTO";
2017-07-08 18:43:42 +08:00
import {Config} from "../../common/config/private/Config";
import {PrivateConfigClass} from "../../common/config/private/PrivateConfigClass";
2017-07-09 20:23:50 +08:00
import {UserRoles} from "../../common/entities/UserDTO";
import {NotificationManager} from "../model/NotifocationManager";
2017-07-14 05:39:09 +08:00
import {Logger} from "../Logger";
export class RenderingMWs {
public static renderResult(req: Request, res: Response, next: NextFunction) {
if (!req.resultPipe)
return next();
return RenderingMWs.renderMessage(res, req.resultPipe);
}
public static renderSessionUser(req: Request, res: Response, next: NextFunction) {
if (!(req.session.user)) {
2017-07-04 01:17:49 +08:00
return next(new Error(ErrorCodes.GENERAL_ERROR, "User not exists"));
}
2017-07-04 01:17:49 +08:00
const user = Utils.clone(req.session.user);
delete user.password;
RenderingMWs.renderMessage(res, user);
}
2017-07-04 01:17:49 +08:00
public static renderSharing(req: Request, res: Response, next: NextFunction) {
if (!req.resultPipe)
return next();
const sharing = Utils.clone<SharingDTO>(req.resultPipe);
delete sharing.password;
RenderingMWs.renderMessage(res, sharing);
}
public static renderFile(req: Request, res: Response, next: NextFunction) {
if (!req.resultPipe)
return next();
return res.sendFile(req.resultPipe);
}
public static renderOK(req: Request, res: Response, next: NextFunction) {
let message = new Message<string>(null, "ok");
res.json(message);
}
2017-07-08 18:43:42 +08:00
public static renderConfig(req: Request, res: Response, next: NextFunction) {
2017-07-13 00:31:19 +08:00
let message = new Message<PrivateConfigClass>(null, Config.original());
2017-07-08 18:43:42 +08:00
res.json(message);
}
public static renderError(err: any, req: Request, res: Response, next: NextFunction): any {
2017-07-09 20:23:50 +08:00
if (err instanceof Error) {
2017-07-14 05:39:09 +08:00
if (err.details) {
if (!(req.session.user && req.session.user.role >= UserRoles.Developer)) {
Logger.warn("Handled error:", err.details.toString() || err.details);
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
}
let message = new Message<any>(err, null);
return res.json(message);
}
2017-07-09 20:23:50 +08:00
NotificationManager.error("unknown server error", err);
return next(err);
}
protected static renderMessage<T>(res: Response, content: T) {
let message = new Message<T>(null, content);
res.json(message);
}
}