Skip to content
Snippets Groups Projects
Commit 5994b80e authored by Etienne LOUPIAS's avatar Etienne LOUPIAS
Browse files

refacto

parent 87c5d6c0
No related branches found
No related tags found
1 merge request!451render page for linkedin
Pipeline #115361 passed
......@@ -34,14 +34,8 @@ export class AppController {
};
}
@Post('/pdf')
@Header('Content-Type', 'application/pdf')
@Header('Cache-Control', 'no-cache, no-store, must-revalidate')
@Header('Pragma', 'no-cache')
@Header('Expires', '0')
async getPdf(@Body() body: GetPdfDto, @Res() res: Response): Promise<void> {
this.logger.log('getPdf: ' + JSON.stringify(body));
/** App URL which can be called from the chrome container (used to get pdf or prerender pages) */
private getAppUrlToBeCalled(): string {
let appUrl;
if (this.configurationService.isLocalConf()) {
// In local dev, use the docker host machine's IP to make the angular app accessible from the chrome container
......@@ -55,9 +49,22 @@ export class AppController {
} else {
appUrl = this.configurationService.appUrl;
}
this.logger.log('appUrl=' + appUrl);
return appUrl;
}
const buffer = await generatePDF(`${appUrl}/${body.urlPath}`);
@Post('/pdf')
@Header('Content-Type', 'application/pdf')
@Header('Cache-Control', 'no-cache, no-store, must-revalidate')
@Header('Pragma', 'no-cache')
@Header('Expires', '0')
async getPdf(@Body() body: GetPdfDto, @Res() res: Response): Promise<void> {
this.logger.log('getPdf: ' + JSON.stringify(body));
const appUrl = this.getAppUrlToBeCalled();
const url = `${appUrl}/${body.urlPath}`;
this.logger.log('url=' + url);
const buffer = await generatePDF(url);
res.set({
'Content-Disposition': `attachment; filename=${body.fileName}`,
......@@ -72,26 +79,16 @@ export class AppController {
@Header('Cache-Control', 'no-cache, no-store, must-revalidate')
@Header('Pragma', 'no-cache')
@Header('Expires', '0')
async getPage(@Param() params: any, @Res() res: Response): Promise<void> {
const page = params[0];
this.logger.log('getPage: ' + page);
async renderUrl(@Param() params: any, @Res() res: Response): Promise<void> {
// If the page is not starting with a slash, add it
const page = params[0].startsWith('/') ? params[0] : '/' + params[0];
this.logger.log('renderUrl: ' + page);
let appUrl;
if (this.configurationService.isLocalConf()) {
// In local dev, use the docker host machine's IP to make the angular app accessible from the chrome container
appUrl = 'http://' + (process.env.DOCKER_HOST_IP || '172.17.0.1') + ':4200';
} else if (process.env.IS_OPENSHIFT !== 'true') {
if (process.env.NODE_ENV === 'rec') {
appUrl = 'http://web-app-rec:8080';
} else {
appUrl = 'http://web-app:8080';
}
} else {
appUrl = this.configurationService.appUrl;
}
this.logger.log('appUrl=' + appUrl);
const appUrl = this.getAppUrlToBeCalled();
const url = `${appUrl}${page}`;
this.logger.log('url=' + url);
const html = await renderPage(`${appUrl}${page}`);
const html = await renderPage(url);
res.send(html);
}
}
......@@ -82,7 +82,7 @@ export const sanitize = (str: string) => {
.replace(/[^\w\s-]/g, ''); // remove all non alphanumeric characters except spaces and dashes
};
export async function generatePDF(url: string): Promise<Uint8Array> {
async function getPage(url: string): Promise<any> {
const browser = await connect({
// Default value for local dev
browserWSEndpoint: process.env.CHROME_WS_URL || 'ws://localhost:5000',
......@@ -91,6 +91,11 @@ export async function generatePDF(url: string): Promise<Uint8Array> {
const page = await browser.newPage();
await page.goto(url, { waitUntil: 'networkidle0' }); // wait until all is loaded
return { page, browser };
}
export async function generatePDF(url: string): Promise<Uint8Array> {
const { page, browser } = await getPage(url);
const buffer = await page.pdf({
format: 'A4',
......@@ -103,16 +108,11 @@ export async function generatePDF(url: string): Promise<Uint8Array> {
}
export async function renderPage(url: string): Promise<string> {
const browser = await connect({
// Default value for local dev
browserWSEndpoint: process.env.CHROME_WS_URL || 'ws://localhost:5000',
});
const { page, browser } = await getPage(url);
const page = await browser.newPage();
await page.goto(url, { waitUntil: 'networkidle0' }); // wait until all is loaded
// Puppeteer generated HTML (with computed og meta html tags, for LinkedIn post publication)
const html = await page.content();
const html = await page.content(); // Puppeteer generated HTML
await browser.close();
return html;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment