2016-04-10 00:06:29 +08:00
|
|
|
import {NextFunction, Request, Response} from "express";
|
2017-12-25 07:42:25 +08:00
|
|
|
import * as path from "path";
|
|
|
|
import * as fs from "fs";
|
2016-05-09 23:04:56 +08:00
|
|
|
import {Utils} from "../../common/Utils";
|
2017-06-04 21:25:08 +08:00
|
|
|
import {Config} from "../../common/config/private/Config";
|
2017-07-04 01:17:49 +08:00
|
|
|
import {ProjectPath} from "../ProjectPath";
|
2017-07-14 05:39:09 +08:00
|
|
|
import {AuthenticationMWs} from "../middlewares/user/AuthenticationMWs";
|
2017-12-25 07:42:25 +08:00
|
|
|
import {CookieNames} from "../../common/CookieNames";
|
|
|
|
|
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
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public static route(app) {
|
2017-12-25 07:42:25 +08:00
|
|
|
const setLocale = (req: Request, res: Response, next: Function) => {
|
|
|
|
let localePath = "";
|
|
|
|
let selectedLocale = req['locale'];
|
|
|
|
if (req.cookies && req.cookies[CookieNames.lang]) {
|
|
|
|
if (Config.Client.languages.indexOf(req.cookies[CookieNames.lang]) !== -1) {
|
|
|
|
console.log(selectedLocale);
|
|
|
|
selectedLocale = req.cookies[CookieNames.lang];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (selectedLocale !== 'en') {
|
|
|
|
localePath = req['locale'];
|
|
|
|
}
|
|
|
|
res.cookie(CookieNames.lang, selectedLocale);
|
|
|
|
req['localePath'] = localePath;
|
|
|
|
next();
|
|
|
|
};
|
|
|
|
|
2017-07-14 05:39:09 +08:00
|
|
|
const renderIndex = (req: Request, res: Response) => {
|
2017-12-25 07:42:25 +08:00
|
|
|
res.sendFile(path.resolve(ProjectPath.FrontendFolder, req['localePath'], 'index.html'), {maxAge: 31536000});
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const redirectToBase = (locale: string) => {
|
|
|
|
return (req: Request, res: Response) => {
|
|
|
|
console.log(locale);
|
|
|
|
if (Config.Client.languages.indexOf(locale) !== -1) {
|
|
|
|
res.cookie(CookieNames.lang, locale);
|
|
|
|
}
|
|
|
|
res.redirect("/");
|
|
|
|
};
|
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) {
|
|
|
|
let user = Utils.clone(req.session.user);
|
|
|
|
delete user.password;
|
|
|
|
res.tpl.user = user;
|
|
|
|
}
|
|
|
|
res.tpl.clientConfig = Config.Client;
|
|
|
|
|
|
|
|
return next();
|
|
|
|
});
|
2016-05-09 23:04:56 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
app.get('/config_inject.js', (req: Request, res: Response) => {
|
2017-12-25 07:42:25 +08:00
|
|
|
res.render(path.resolve(ProjectPath.FrontendFolder, 'config_inject.ejs'), res.tpl);
|
2017-06-11 04:32:56 +08:00
|
|
|
});
|
2017-12-25 07:42:25 +08:00
|
|
|
|
2017-07-14 05:39:09 +08:00
|
|
|
app.get(['/', '/login', "/gallery*", "/share*", "/admin", "/search*"],
|
|
|
|
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 => {
|
|
|
|
app.get(['/' + l + '/', '/' + l + '/login', '/' + l + "/gallery*", '/' + l + "/share*", '/' + l + "/admin", '/' + l + "/search*"],
|
|
|
|
redirectToBase(l)
|
|
|
|
);
|
|
|
|
});
|
2016-03-20 00:31:42 +08:00
|
|
|
|
2017-12-25 07:42:25 +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);
|
|
|
|
});
|
|
|
|
});
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-05-09 23:04:56 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|