Skip to content

Commit

Permalink
Adding base url to language redirect. fixes #793
Browse files Browse the repository at this point in the history
  • Loading branch information
bpatrik committed Dec 2, 2023
1 parent 4d7e7fe commit 60a0bee
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 87 deletions.
175 changes: 88 additions & 87 deletions src/backend/routes/PublicRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {ServerTimeEntry} from '../middlewares/ServerTimingMWs';
import {ClientConfig, TAGS} from '../../common/config/public/ClientConfig';
import {QueryParams} from '../../common/QueryParams';
import {PhotoProcessing} from '../model/fileaccess/fileprocessing/PhotoProcessing';
import {Utils} from '../../common/Utils';

declare global {
// eslint-disable-next-line @typescript-eslint/no-namespace
Expand All @@ -35,7 +36,7 @@ export class PublicRouter {
let selectedLocale = req.locale;
if (req.cookies && req.cookies[CookieNames.lang]) {
if (
Config.Server.languages.indexOf(req.cookies[CookieNames.lang]) !== -1
Config.Server.languages.indexOf(req.cookies[CookieNames.lang]) !== -1
) {
selectedLocale = req.cookies[CookieNames.lang];
}
Expand All @@ -48,14 +49,14 @@ export class PublicRouter {
// index.html should not be cached as it contains template that can change
const renderIndex = (req: Request, res: Response, next: NextFunction) => {
ejs.renderFile(
path.join(ProjectPath.FrontendFolder, req.localePath, 'index.html'),
res.tpl,
(err, str) => {
if (err) {
return next(new ErrorDTO(ErrorCodes.GENERAL_ERROR, err.message));
}
res.send(str);
path.join(ProjectPath.FrontendFolder, req.localePath, 'index.html'),
res.tpl,
(err, str) => {
if (err) {
return next(new ErrorDTO(ErrorCodes.GENERAL_ERROR, err.message));
}
res.send(str);
}
);
};

Expand All @@ -64,7 +65,7 @@ export class PublicRouter {
if (Config.Server.languages.indexOf(locale) !== -1) {
res.cookie(CookieNames.lang, locale);
}
res.redirect('/?ln=' + locale);
res.redirect(Utils.concatUrls('/' + Config.Server.urlBase) + '/?ln=' + locale);
};
};

Expand Down Expand Up @@ -94,12 +95,12 @@ export class PublicRouter {
}) as unknown as ClientConfig;
// Escaping html tags, like <script></script>
confCopy.Server.customHTMLHead =
confCopy.Server.customHTMLHead
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#039;');
confCopy.Server.customHTMLHead
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#039;');
res.tpl.Config = confCopy;
res.tpl.customHTMLHead = Config.Server.customHTMLHead;
const selectedTheme = Config.Gallery.Themes.availableThemes.find(th => th.name === Config.Gallery.Themes.selectedTheme)?.theme || '';
Expand Down Expand Up @@ -140,7 +141,7 @@ export class PublicRouter {
'photo'
],
start_url:
Config.Server.publicUrl === '' ? '.' : Config.Server.publicUrl,
Config.Server.publicUrl === '' ? '.' : Config.Server.publicUrl,
background_color: '#000000',
theme_color: '#000000',
});
Expand Down Expand Up @@ -185,40 +186,40 @@ export class PublicRouter {
y: vBs[1]
};
return '<svg ' +
' xmlns="http://www.w3.org/2000/svg"' +
' viewBox="' + vBs.join(' ') + '">' +
(theme === 'auto' ? ('<style>' +
' path, circle {' +
' fill: black;' +
' }' +
' circle.bg,rect.bg {' +
' fill: white;' +
' }' +
' @media (prefers-color-scheme: dark) {' +
' path, circle {' +
' fill: white;' +
' }' +
' circle.bg,rect.bg {' +
' fill: black;' +
' }' +
' }' +
' </style>') :
(theme != null ?
('<style>' +
' path, circle {' +
' fill: ' + theme + ';' +
' }' +
' circle.bg {' +
' fill: black;' +
' }' +
' </style>')
: '<style>' +
' circle.bg,rect.bg {' +
' fill: white;' +
' }' +
' </style>')) +
`<rect class="bg" x="${canvasStart.x}" y="${canvasStart.y}" width="${canvasSize}" height="${canvasSize}" rx="15" />` +
Config.Server.svgIcon.items + '</svg>';
' xmlns="http://www.w3.org/2000/svg"' +
' viewBox="' + vBs.join(' ') + '">' +
(theme === 'auto' ? ('<style>' +
' path, circle {' +
' fill: black;' +
' }' +
' circle.bg,rect.bg {' +
' fill: white;' +
' }' +
' @media (prefers-color-scheme: dark) {' +
' path, circle {' +
' fill: white;' +
' }' +
' circle.bg,rect.bg {' +
' fill: black;' +
' }' +
' }' +
' </style>') :
(theme != null ?
('<style>' +
' path, circle {' +
' fill: ' + theme + ';' +
' }' +
' circle.bg {' +
' fill: black;' +
' }' +
' </style>')
: '<style>' +
' circle.bg,rect.bg {' +
' fill: white;' +
' }' +
' </style>')) +
`<rect class="bg" x="${canvasStart.x}" y="${canvasStart.y}" width="${canvasSize}" height="${canvasSize}" rx="15" />` +
Config.Server.svgIcon.items + '</svg>';
};

app.get('/icon.svg', (req: Request, res: Response) => {
Expand Down Expand Up @@ -276,44 +277,44 @@ export class PublicRouter {
});

app.get(
[
'/',
'/login',
'/gallery*',
'/share/:' + QueryParams.gallery.sharingKey_params,
'/shareLogin',
'/admin',
'/duplicates',
'/faces',
'/albums',
'/search*',
],
AuthenticationMWs.tryAuthenticate,
addTPl, // add template after authentication was successful
setLocale,
renderIndex
[
'/',
'/login',
'/gallery*',
'/share/:' + QueryParams.gallery.sharingKey_params,
'/shareLogin',
'/admin',
'/duplicates',
'/faces',
'/albums',
'/search*',
],
AuthenticationMWs.tryAuthenticate,
addTPl, // add template after authentication was successful
setLocale,
renderIndex
);
Config.Server.languages.forEach((l) => {
app.get(
[
'/' + l + '/',
'/' + l + '/login',
'/' + l + '/gallery*',
'/' + l + '/share*',
'/' + l + '/admin',
'/' + l + '/search*',
],
redirectToBase(l)
[
'/' + l + '/',
'/' + l + '/login',
'/' + l + '/gallery*',
'/' + l + '/share*',
'/' + l + '/admin',
'/' + l + '/search*',
],
redirectToBase(l)
);
});

const renderFile = (subDir = '') => {
return (req: Request, res: Response) => {
const file = path.join(
ProjectPath.FrontendFolder,
req.localePath,
subDir,
req.params.file
ProjectPath.FrontendFolder,
req.localePath,
subDir,
req.params.file
);
if (!fs.existsSync(file)) {
return res.sendStatus(404);
Expand All @@ -326,16 +327,16 @@ export class PublicRouter {
};

app.get(
'/assets/:file(*)',
setLocale,
AuthenticationMWs.normalizePathParam('file'),
renderFile('assets')
'/assets/:file(*)',
setLocale,
AuthenticationMWs.normalizePathParam('file'),
renderFile('assets')
);
app.get(
'/:file',
setLocale,
AuthenticationMWs.normalizePathParam('file'),
renderFile()
'/:file',
setLocale,
AuthenticationMWs.normalizePathParam('file'),
renderFile()
);
}
}
2 changes: 2 additions & 0 deletions test/common/unit/Utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ describe('Utils', () => {
expect(Utils.concatUrls('abc\\', 'cde')).to.be.equal('abc/cde');
expect(Utils.concatUrls('abc/', 'cde/')).to.be.equal('abc/cde');
expect(Utils.concatUrls('./abc\\', 'cde/')).to.be.equal('./abc/cde');
expect(Utils.concatUrls('/abc\\', 'cde/')).to.be.equal('/abc/cde');
expect(Utils.concatUrls('abc/', '\\cde/')).to.be.equal('abc/cde');
expect(Utils.concatUrls('/abc/', '\\cde/')).to.be.equal('/abc/cde');
expect(Utils.concatUrls('abc\\', '\\cde/')).to.be.equal('abc/cde');
expect(Utils.concatUrls('abc\\', '/cde/')).to.be.equal('abc/cde');
expect(Utils.concatUrls('abc/', '/cde/')).to.be.equal('abc/cde');
Expand Down

0 comments on commit 60a0bee

Please sign in to comment.