Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Server
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
web-et-numerique
Factory
Resin
Server
Commits
5994b80e
Commit
5994b80e
authored
2 months ago
by
Etienne LOUPIAS
Browse files
Options
Downloads
Patches
Plain Diff
refacto
parent
87c5d6c0
No related branches found
No related tags found
1 merge request
!451
render page for linkedin
Pipeline
#115361
passed
2 months ago
Stage: test
Stage: build
Stage: deploy
Stage: quality
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/app.controller.ts
+25
-28
25 additions, 28 deletions
src/app.controller.ts
src/shared/utils.ts
+9
-9
9 additions, 9 deletions
src/shared/utils.ts
with
34 additions
and
37 deletions
src/app.controller.ts
+
25
−
28
View file @
5994b80e
...
...
@@ -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
);
}
}
This diff is collapsed.
Click to expand it.
src/shared/utils.ts
+
9
−
9
View file @
5994b80e
...
...
@@ -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
ge
neratePDF
(
url
:
string
):
Promise
<
Uint8Arr
ay
>
{
async
function
ge
tPage
(
url
:
string
):
Promise
<
a
n
y
>
{
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
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment