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

118 lines
3.3 KiB
TypeScript
Raw Normal View History

2018-11-29 06:49:33 +08:00
import {Express, NextFunction, Request, Response} from 'express';
2018-03-31 03:30:30 +08:00
import * as path from 'path';
import * as fs from 'fs';
import * as ejs from 'ejs';
2018-03-31 03:30:30 +08:00
import {Utils} from '../../common/Utils';
import {Config} from '../../common/config/private/Config';
import {ProjectPath} from '../ProjectPath';
import {AuthenticationMWs} from '../middlewares/user/AuthenticationMWs';
import {CookieNames} from '../../common/CookieNames';
2018-11-29 06:49:33 +08:00
import {ErrorCodes, ErrorDTO} from '../../common/entities/Error';
2018-11-29 06:49:33 +08:00
declare global {
namespace Express {
interface Request {
locale?: string;
localePath?: string;
tpl?: any;
}
interface Response {
tpl?: any;
}
}
}
2016-05-09 23:04:56 +08:00
export class PublicRouter {
2017-06-04 21:25:08 +08:00
2018-11-29 06:49:33 +08:00
public static route(app: Express) {
const setLocale = (req: Request, res: Response, next: Function) => {
2018-03-31 03:30:30 +08:00
let localePath = '';
let selectedLocale = req['locale'];
if (req.cookies && req.cookies[CookieNames.lang]) {
if (Config.Client.languages.indexOf(req.cookies[CookieNames.lang]) !== -1) {
selectedLocale = req.cookies[CookieNames.lang];
}
}
if (selectedLocale !== 'en') {
localePath = req['locale'];
}
res.cookie(CookieNames.lang, selectedLocale);
req['localePath'] = localePath;
next();
};
const renderIndex = (req: Request, res: Response, next: Function) => {
ejs.renderFile(path.resolve(ProjectPath.FrontendFolder, req['localePath'], 'index.html'),
res.tpl, (err, str) => {
if (err) {
2018-11-29 06:49:33 +08:00
return next(new ErrorDTO(ErrorCodes.GENERAL_ERROR, err.message));
}
res.send(str);
});
};
const redirectToBase = (locale: string) => {
return (req: Request, res: Response) => {
if (Config.Client.languages.indexOf(locale) !== -1) {
res.cookie(CookieNames.lang, locale);
}
2018-03-31 03:30:30 +08:00
res.redirect('/?ln=' + locale);
};
2017-07-14 05:39:09 +08:00
};
app.use(
(req: Request, res: Response, next: NextFunction) => {
res.tpl = {};
res.tpl.user = null;
if (req.session.user) {
2018-03-31 03:30:30 +08:00
const user = Utils.clone(req.session.user);
2017-07-14 05:39:09 +08:00
delete user.password;
res.tpl.user = user;
}
res.tpl.clientConfig = Config.Client;
return next();
});
2016-05-09 23:04:56 +08:00
2018-03-31 03:30:30 +08:00
app.get(['/', '/login', '/gallery*', '/share*', '/admin', '/search*'],
2017-07-14 05:39:09 +08:00
AuthenticationMWs.tryAuthenticate,
setLocale,
2017-07-14 05:39:09 +08:00
renderIndex
);
Config.Client.languages.forEach(l => {
2018-03-31 03:30:30 +08:00
app.get(['/' + l + '/', '/' + l + '/login', '/' + l + '/gallery*', '/' + l + '/share*', '/' + l + '/admin', '/' + l + '/search*'],
redirectToBase(l)
);
});
2018-12-08 18:28:56 +08:00
app.get('/assets/:file(*)',
setLocale,
(req: Request, res: Response) => {
const file = path.resolve(ProjectPath.FrontendFolder, req['localePath'], 'assets', req.params.file);
fs.exists(file, (exists: boolean) => {
if (!exists) {
return res.sendStatus(404);
}
res.sendFile(file);
});
});
app.get('/:file',
setLocale,
(req: Request, res: Response) => {
const file = path.resolve(ProjectPath.FrontendFolder, req['localePath'], req.params.file);
fs.exists(file, (exists: boolean) => {
if (!exists) {
return res.sendStatus(404);
}
res.sendFile(file);
});
});
}
2016-05-09 23:04:56 +08:00
}