|
|
@ -1,39 +1,57 @@
|
|
|
|
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as path from 'path';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { ConfigurationService } from '@ghostfolio/api/services/configuration.service';
|
|
|
|
import { DEFAULT_LANGUAGE_CODE } from '@ghostfolio/common/config';
|
|
|
|
import { DEFAULT_LANGUAGE_CODE } from '@ghostfolio/common/config';
|
|
|
|
import { Injectable, Logger, NestMiddleware } from '@nestjs/common';
|
|
|
|
import { Injectable, NestMiddleware } from '@nestjs/common';
|
|
|
|
import { NextFunction, Request, Response } from 'express';
|
|
|
|
import { NextFunction, Request, Response } from 'express';
|
|
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
@Injectable()
|
|
|
|
export class FrontendMiddleware implements NestMiddleware {
|
|
|
|
export class FrontendMiddleware implements NestMiddleware {
|
|
|
|
use(req: Request, res: Response, next: NextFunction) {
|
|
|
|
public indexHtmlDe = fs.readFileSync(
|
|
|
|
if (req.url.includes('cover.png')) {
|
|
|
|
this.getPathOfIndexHtmlFile('de'),
|
|
|
|
Logger.log(`Referer: ${req.headers.referer}`, 'FrontendMiddleware');
|
|
|
|
'utf8'
|
|
|
|
|
|
|
|
);
|
|
|
|
// Resolve feature graphic for blog post
|
|
|
|
public indexHtmlEn = fs.readFileSync(
|
|
|
|
if (req.headers.referer?.includes('500-stars-on-github')) {
|
|
|
|
this.getPathOfIndexHtmlFile(DEFAULT_LANGUAGE_CODE),
|
|
|
|
res.sendFile(
|
|
|
|
'utf8'
|
|
|
|
path.join(
|
|
|
|
);
|
|
|
|
__dirname,
|
|
|
|
|
|
|
|
'..',
|
|
|
|
public constructor(
|
|
|
|
'client',
|
|
|
|
private readonly configurationService: ConfigurationService
|
|
|
|
'assets',
|
|
|
|
) {}
|
|
|
|
'images',
|
|
|
|
|
|
|
|
'blog',
|
|
|
|
public use(req: Request, res: Response, next: NextFunction) {
|
|
|
|
'500-stars-on-github.jpg'
|
|
|
|
let featureGraphicPath = 'assets/cover.png';
|
|
|
|
)
|
|
|
|
|
|
|
|
);
|
|
|
|
if (
|
|
|
|
} else {
|
|
|
|
req.path === '/en/blog/2022/08/500-stars-on-github' ||
|
|
|
|
// Skip
|
|
|
|
req.path === '/en/blog/2022/08/500-stars-on-github/'
|
|
|
|
next();
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
featureGraphicPath = 'assets/images/blog/500-stars-on-github.jpg';
|
|
|
|
} else if (req.path.startsWith('/api/') || this.isFileRequest(req.url)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (req.path.startsWith('/api/') || this.isFileRequest(req.url)) {
|
|
|
|
// Skip
|
|
|
|
// Skip
|
|
|
|
next();
|
|
|
|
next();
|
|
|
|
} else if (req.path.startsWith('/de/')) {
|
|
|
|
} else if (req.path === '/de' || req.path.startsWith('/de/')) {
|
|
|
|
res.sendFile(this.getPathOfIndexHtmlFile('de'));
|
|
|
|
res.send(
|
|
|
|
|
|
|
|
this.interpolate(this.indexHtmlDe, {
|
|
|
|
|
|
|
|
featureGraphicPath,
|
|
|
|
|
|
|
|
languageCode: 'de',
|
|
|
|
|
|
|
|
path: req.path,
|
|
|
|
|
|
|
|
rootUrl: this.configurationService.get('ROOT_URL')
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
res.sendFile(this.getPathOfIndexHtmlFile(DEFAULT_LANGUAGE_CODE));
|
|
|
|
res.send(
|
|
|
|
|
|
|
|
this.interpolate(this.indexHtmlEn, {
|
|
|
|
|
|
|
|
featureGraphicPath,
|
|
|
|
|
|
|
|
languageCode: DEFAULT_LANGUAGE_CODE,
|
|
|
|
|
|
|
|
path: req.path,
|
|
|
|
|
|
|
|
rootUrl: this.configurationService.get('ROOT_URL')
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -41,6 +59,16 @@ export class FrontendMiddleware implements NestMiddleware {
|
|
|
|
return path.join(__dirname, '..', 'client', aLocale, 'index.html');
|
|
|
|
return path.join(__dirname, '..', 'client', aLocale, 'index.html');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private interpolate(template: string, context: any) {
|
|
|
|
|
|
|
|
return template.replace(/[$]{([^}]+)}/g, (_, objectPath) => {
|
|
|
|
|
|
|
|
const properties = objectPath.split('.');
|
|
|
|
|
|
|
|
return properties.reduce(
|
|
|
|
|
|
|
|
(previous, current) => previous?.[current],
|
|
|
|
|
|
|
|
context
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private isFileRequest(filename: string) {
|
|
|
|
private isFileRequest(filename: string) {
|
|
|
|
if (filename === '/assets/LICENSE') {
|
|
|
|
if (filename === '/assets/LICENSE') {
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|