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';
|
2018-05-13 00:19:51 +08:00
|
|
|
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';
|
2017-12-25 07:42:25 +08:00
|
|
|
|
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-04-10 00:06:29 +08:00
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
export class PublicRouter {
|
2017-06-04 21:25:08 +08:00
|
|
|
|
2018-05-13 00:19:51 +08:00
|
|
|
|
2018-11-29 06:49:33 +08:00
|
|
|
public static route(app: Express) {
|
2017-12-25 07:42:25 +08:00
|
|
|
const setLocale = (req: Request, res: Response, next: Function) => {
|
2018-03-31 03:30:30 +08:00
|
|
|
let localePath = '';
|
2017-12-25 07:42:25 +08:00
|
|
|
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') {
|
2019-01-03 04:21:20 +08:00
|
|
|
localePath = selectedLocale;
|
2017-12-25 07:42:25 +08:00
|
|
|
}
|
|
|
|
res.cookie(CookieNames.lang, selectedLocale);
|
|
|
|
req['localePath'] = localePath;
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
|
2018-05-13 00:19:51 +08:00
|
|
|
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));
|
2018-05-13 00:19:51 +08:00
|
|
|
}
|
|
|
|
res.send(str);
|
|
|
|
});
|
2017-12-25 07:42:25 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
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-12-25 07:42:25 +08:00
|
|
|
};
|
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
|
|
|
|
2017-12-25 07:42:25 +08:00
|
|
|
|
2019-02-15 07:25:55 +08:00
|
|
|
app.get(['/', '/login', '/gallery*', '/share*', '/admin', '/duplicates', '/faces', '/search*'],
|
2017-07-14 05:39:09 +08:00
|
|
|
AuthenticationMWs.tryAuthenticate,
|
2017-12-25 07:42:25 +08:00
|
|
|
setLocale,
|
2017-07-14 05:39:09 +08:00
|
|
|
renderIndex
|
|
|
|
);
|
2017-12-25 07:42:25 +08:00
|
|
|
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*'],
|
2017-12-25 07:42:25 +08:00
|
|
|
redirectToBase(l)
|
|
|
|
);
|
|
|
|
});
|
2016-03-20 00:31:42 +08:00
|
|
|
|
2018-12-08 18:28:56 +08:00
|
|
|
app.get('/assets/:file(*)',
|
2017-12-25 07:42:25 +08:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|