]>
Commit | Line | Data |
---|---|---|
e032aec9 | 1 | import * as express from 'express' |
b49f22d8 C |
2 | import { readFile } from 'fs-extra' |
3 | import { join } from 'path' | |
4 | import validator from 'validator' | |
aea0b0e7 C |
5 | import { escapeHTML } from '@shared/core-utils/renderer' |
6 | import { HTMLServerConfig } from '@shared/models' | |
bd45d503 | 7 | import { buildFileLocale, getDefaultLocale, is18nLocale, POSSIBLE_LOCALES } from '../../shared/core-utils/i18n/i18n' |
b49f22d8 C |
8 | import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes' |
9 | import { VideoPlaylistPrivacy, VideoPrivacy } from '../../shared/models/videos' | |
4097c6d6 | 10 | import { isTestInstance, sha256 } from '../helpers/core-utils' |
b49f22d8 | 11 | import { logger } from '../helpers/logger' |
aea0b0e7 | 12 | import { mdToPlainText } from '../helpers/markdown' |
b49f22d8 | 13 | import { CONFIG } from '../initializers/config' |
8d987ec6 | 14 | import { |
b49f22d8 | 15 | ACCEPT_HEADERS, |
2cb03dc1 | 16 | ACTOR_IMAGES_SIZE, |
8d987ec6 K |
17 | CUSTOM_HTML_TAG_COMMENTS, |
18 | EMBED_SIZE, | |
f2eb23cd | 19 | FILES_CONTENT_HASH, |
b49f22d8 C |
20 | PLUGIN_GLOBAL_CSS_PATH, |
21 | WEBSERVER | |
8d987ec6 | 22 | } from '../initializers/constants' |
92bf2f62 | 23 | import { AccountModel } from '../models/account/account' |
b49f22d8 | 24 | import { VideoModel } from '../models/video/video' |
92bf2f62 | 25 | import { VideoChannelModel } from '../models/video/video-channel' |
b49f22d8 C |
26 | import { getActivityStreamDuration } from '../models/video/video-format-utils' |
27 | import { VideoPlaylistModel } from '../models/video/video-playlist' | |
8d987ec6 | 28 | import { MAccountActor, MChannelActor } from '../types/models' |
aea0b0e7 | 29 | import { getHTMLServerConfig } from './config' |
e032aec9 | 30 | |
6fad8e51 C |
31 | type Tags = { |
32 | ogType: string | |
b96777c3 | 33 | twitterCard: 'player' | 'summary' | 'summary_large_image' |
6fad8e51 C |
34 | schemaType: string |
35 | ||
36 | list?: { | |
37 | numberOfItems: number | |
38 | } | |
39 | ||
865af3fd | 40 | siteName: string |
6fad8e51 C |
41 | title: string |
42 | url: string | |
106fa224 | 43 | originUrl: string |
6fad8e51 C |
44 | description: string |
45 | ||
46 | embed?: { | |
47 | url: string | |
48 | createdAt: string | |
49 | duration?: string | |
50 | views?: number | |
51 | } | |
52 | ||
53 | image: { | |
54 | url: string | |
55 | width?: number | |
56 | height?: number | |
57 | } | |
58 | } | |
59 | ||
f2eb23cd | 60 | class ClientHtml { |
e032aec9 | 61 | |
a1587156 | 62 | private static htmlCache: { [path: string]: string } = {} |
e032aec9 C |
63 | |
64 | static invalidCache () { | |
3e753302 C |
65 | logger.info('Cleaning HTML cache.') |
66 | ||
e032aec9 C |
67 | ClientHtml.htmlCache = {} |
68 | } | |
69 | ||
9aac4423 | 70 | static async getDefaultHTMLPage (req: express.Request, res: express.Response, paramLang?: string) { |
7738273b RK |
71 | const html = paramLang |
72 | ? await ClientHtml.getIndexHTML(req, res, paramLang) | |
73 | : await ClientHtml.getIndexHTML(req, res) | |
e032aec9 | 74 | |
9aac4423 C |
75 | let customHtml = ClientHtml.addTitleTag(html) |
76 | customHtml = ClientHtml.addDescriptionTag(customHtml) | |
e032aec9 | 77 | |
9aac4423 | 78 | return customHtml |
e032aec9 C |
79 | } |
80 | ||
81 | static async getWatchHTMLPage (videoId: string, req: express.Request, res: express.Response) { | |
e032aec9 | 82 | // Let Angular application handle errors |
92bf2f62 | 83 | if (!validator.isInt(videoId) && !validator.isUUID(videoId, 4)) { |
2d53be02 | 84 | res.status(HttpStatusCode.NOT_FOUND_404) |
e032aec9 C |
85 | return ClientHtml.getIndexHTML(req, res) |
86 | } | |
87 | ||
88 | const [ html, video ] = await Promise.all([ | |
89 | ClientHtml.getIndexHTML(req, res), | |
d636ab58 | 90 | VideoModel.loadWithBlacklist(videoId) |
e032aec9 C |
91 | ]) |
92 | ||
93 | // Let Angular application handle errors | |
22a73cb8 | 94 | if (!video || video.privacy === VideoPrivacy.PRIVATE || video.privacy === VideoPrivacy.INTERNAL || video.VideoBlacklist) { |
2d53be02 | 95 | res.status(HttpStatusCode.NOT_FOUND_404) |
c08579e1 | 96 | return html |
e032aec9 C |
97 | } |
98 | ||
9aac4423 | 99 | let customHtml = ClientHtml.addTitleTag(html, escapeHTML(video.name)) |
a073c912 | 100 | customHtml = ClientHtml.addDescriptionTag(customHtml, mdToPlainText(video.description)) |
8d987ec6 K |
101 | |
102 | const url = WEBSERVER.URL + video.getWatchStaticPath() | |
106fa224 | 103 | const originUrl = video.url |
8d987ec6 | 104 | const title = escapeHTML(video.name) |
865af3fd | 105 | const siteName = escapeHTML(CONFIG.INSTANCE.NAME) |
a073c912 | 106 | const description = mdToPlainText(video.description) |
8d987ec6 K |
107 | |
108 | const image = { | |
109 | url: WEBSERVER.URL + video.getPreviewStaticPath() | |
110 | } | |
111 | ||
112 | const embed = { | |
113 | url: WEBSERVER.URL + video.getEmbedStaticPath(), | |
114 | createdAt: video.createdAt.toISOString(), | |
115 | duration: getActivityStreamDuration(video.duration), | |
116 | views: video.views | |
117 | } | |
118 | ||
119 | const ogType = 'video' | |
120 | const twitterCard = CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary_large_image' | |
121 | const schemaType = 'VideoObject' | |
122 | ||
106fa224 | 123 | customHtml = ClientHtml.addTags(customHtml, { |
a59f210f C |
124 | url, |
125 | originUrl, | |
126 | siteName, | |
127 | title, | |
128 | description, | |
129 | image, | |
130 | embed, | |
131 | ogType, | |
132 | twitterCard, | |
133 | schemaType | |
106fa224 | 134 | }) |
8d987ec6 K |
135 | |
136 | return customHtml | |
137 | } | |
138 | ||
139 | static async getWatchPlaylistHTMLPage (videoPlaylistId: string, req: express.Request, res: express.Response) { | |
140 | // Let Angular application handle errors | |
141 | if (!validator.isInt(videoPlaylistId) && !validator.isUUID(videoPlaylistId, 4)) { | |
2d53be02 | 142 | res.status(HttpStatusCode.NOT_FOUND_404) |
8d987ec6 K |
143 | return ClientHtml.getIndexHTML(req, res) |
144 | } | |
145 | ||
146 | const [ html, videoPlaylist ] = await Promise.all([ | |
147 | ClientHtml.getIndexHTML(req, res), | |
148 | VideoPlaylistModel.loadWithAccountAndChannel(videoPlaylistId, null) | |
149 | ]) | |
150 | ||
151 | // Let Angular application handle errors | |
152 | if (!videoPlaylist || videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) { | |
2d53be02 | 153 | res.status(HttpStatusCode.NOT_FOUND_404) |
8d987ec6 K |
154 | return html |
155 | } | |
156 | ||
157 | let customHtml = ClientHtml.addTitleTag(html, escapeHTML(videoPlaylist.name)) | |
a073c912 | 158 | customHtml = ClientHtml.addDescriptionTag(customHtml, mdToPlainText(videoPlaylist.description)) |
8d987ec6 K |
159 | |
160 | const url = videoPlaylist.getWatchUrl() | |
106fa224 | 161 | const originUrl = videoPlaylist.url |
8d987ec6 | 162 | const title = escapeHTML(videoPlaylist.name) |
865af3fd | 163 | const siteName = escapeHTML(CONFIG.INSTANCE.NAME) |
a073c912 | 164 | const description = mdToPlainText(videoPlaylist.description) |
8d987ec6 K |
165 | |
166 | const image = { | |
167 | url: videoPlaylist.getThumbnailUrl() | |
168 | } | |
169 | ||
6fad8e51 C |
170 | const embed = { |
171 | url: WEBSERVER.URL + videoPlaylist.getEmbedStaticPath(), | |
172 | createdAt: videoPlaylist.createdAt.toISOString() | |
173 | } | |
174 | ||
8d987ec6 | 175 | const list = { |
6fad8e51 | 176 | numberOfItems: videoPlaylist.get('videosLength') as number |
8d987ec6 K |
177 | } |
178 | ||
179 | const ogType = 'video' | |
6fad8e51 | 180 | const twitterCard = CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary' |
8d987ec6 K |
181 | const schemaType = 'ItemList' |
182 | ||
106fa224 | 183 | customHtml = ClientHtml.addTags(customHtml, { |
a59f210f C |
184 | url, |
185 | originUrl, | |
186 | siteName, | |
187 | embed, | |
188 | title, | |
189 | description, | |
190 | image, | |
191 | list, | |
192 | ogType, | |
193 | twitterCard, | |
194 | schemaType | |
106fa224 | 195 | }) |
92bf2f62 C |
196 | |
197 | return customHtml | |
198 | } | |
199 | ||
200 | static async getAccountHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) { | |
201 | return this.getAccountOrChannelHTMLPage(() => AccountModel.loadByNameWithHost(nameWithHost), req, res) | |
202 | } | |
203 | ||
204 | static async getVideoChannelHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) { | |
205 | return this.getAccountOrChannelHTMLPage(() => VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithHost), req, res) | |
206 | } | |
207 | ||
cf649c2e C |
208 | static async getEmbedHTML () { |
209 | const path = ClientHtml.getEmbedPath() | |
cf649c2e | 210 | |
b9da21bd | 211 | if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path] |
cf649c2e C |
212 | |
213 | const buffer = await readFile(path) | |
aea0b0e7 | 214 | const serverConfig = await getHTMLServerConfig() |
cf649c2e C |
215 | |
216 | let html = buffer.toString() | |
217 | html = await ClientHtml.addAsyncPluginCSS(html) | |
2564d97e | 218 | html = ClientHtml.addCustomCSS(html) |
915e2bbb | 219 | html = ClientHtml.addTitleTag(html) |
aea0b0e7 C |
220 | html = ClientHtml.addDescriptionTag(html) |
221 | html = ClientHtml.addServerConfig(html, serverConfig) | |
cf649c2e C |
222 | |
223 | ClientHtml.htmlCache[path] = html | |
224 | ||
225 | return html | |
226 | } | |
227 | ||
92bf2f62 | 228 | private static async getAccountOrChannelHTMLPage ( |
b49f22d8 | 229 | loader: () => Promise<MAccountActor | MChannelActor>, |
92bf2f62 C |
230 | req: express.Request, |
231 | res: express.Response | |
232 | ) { | |
233 | const [ html, entity ] = await Promise.all([ | |
234 | ClientHtml.getIndexHTML(req, res), | |
235 | loader() | |
236 | ]) | |
237 | ||
238 | // Let Angular application handle errors | |
239 | if (!entity) { | |
2d53be02 | 240 | res.status(HttpStatusCode.NOT_FOUND_404) |
92bf2f62 C |
241 | return ClientHtml.getIndexHTML(req, res) |
242 | } | |
243 | ||
244 | let customHtml = ClientHtml.addTitleTag(html, escapeHTML(entity.getDisplayName())) | |
a073c912 | 245 | customHtml = ClientHtml.addDescriptionTag(customHtml, mdToPlainText(entity.description)) |
8d987ec6 | 246 | |
a59f210f | 247 | const url = entity.getLocalUrl() |
106fa224 | 248 | const originUrl = entity.Actor.url |
865af3fd | 249 | const siteName = escapeHTML(CONFIG.INSTANCE.NAME) |
8d987ec6 | 250 | const title = escapeHTML(entity.getDisplayName()) |
a073c912 | 251 | const description = mdToPlainText(entity.description) |
8d987ec6 K |
252 | |
253 | const image = { | |
254 | url: entity.Actor.getAvatarUrl(), | |
2cb03dc1 C |
255 | width: ACTOR_IMAGES_SIZE.AVATARS.width, |
256 | height: ACTOR_IMAGES_SIZE.AVATARS.height | |
8d987ec6 K |
257 | } |
258 | ||
259 | const ogType = 'website' | |
260 | const twitterCard = 'summary' | |
261 | const schemaType = 'ProfilePage' | |
262 | ||
a59f210f C |
263 | customHtml = ClientHtml.addTags(customHtml, { |
264 | url, | |
265 | originUrl, | |
266 | title, | |
267 | siteName, | |
268 | description, | |
269 | image, | |
270 | ogType, | |
271 | twitterCard, | |
272 | schemaType | |
273 | }) | |
9aac4423 C |
274 | |
275 | return customHtml | |
276 | } | |
277 | ||
278 | private static async getIndexHTML (req: express.Request, res: express.Response, paramLang?: string) { | |
279 | const path = ClientHtml.getIndexPath(req, res, paramLang) | |
b9da21bd | 280 | if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path] |
9aac4423 C |
281 | |
282 | const buffer = await readFile(path) | |
aea0b0e7 | 283 | const serverConfig = await getHTMLServerConfig() |
9aac4423 C |
284 | |
285 | let html = buffer.toString() | |
286 | ||
7738273b | 287 | if (paramLang) html = ClientHtml.addHtmlLang(html, paramLang) |
caf2aaf4 K |
288 | html = ClientHtml.addManifestContentHash(html) |
289 | html = ClientHtml.addFaviconContentHash(html) | |
290 | html = ClientHtml.addLogoContentHash(html) | |
9aac4423 | 291 | html = ClientHtml.addCustomCSS(html) |
aea0b0e7 | 292 | html = ClientHtml.addServerConfig(html, serverConfig) |
a8b666e9 | 293 | html = await ClientHtml.addAsyncPluginCSS(html) |
9aac4423 | 294 | |
a1587156 | 295 | ClientHtml.htmlCache[path] = html |
9aac4423 C |
296 | |
297 | return html | |
e032aec9 C |
298 | } |
299 | ||
7738273b | 300 | private static getIndexPath (req: express.Request, res: express.Response, paramLang: string) { |
e032aec9 C |
301 | let lang: string |
302 | ||
303 | // Check param lang validity | |
304 | if (paramLang && is18nLocale(paramLang)) { | |
305 | lang = paramLang | |
306 | ||
307 | // Save locale in cookies | |
308 | res.cookie('clientLanguage', lang, { | |
6dd9de95 | 309 | secure: WEBSERVER.SCHEME === 'https', |
bc90883f | 310 | sameSite: 'none', |
e032aec9 C |
311 | maxAge: 1000 * 3600 * 24 * 90 // 3 months |
312 | }) | |
313 | ||
314 | } else if (req.cookies.clientLanguage && is18nLocale(req.cookies.clientLanguage)) { | |
315 | lang = req.cookies.clientLanguage | |
316 | } else { | |
317 | lang = req.acceptsLanguages(POSSIBLE_LOCALES) || getDefaultLocale() | |
318 | } | |
319 | ||
320 | return join(__dirname, '../../../client/dist/' + buildFileLocale(lang) + '/index.html') | |
321 | } | |
322 | ||
cf649c2e C |
323 | private static getEmbedPath () { |
324 | return join(__dirname, '../../../client/dist/standalone/videos/embed.html') | |
325 | } | |
326 | ||
7738273b RK |
327 | private static addHtmlLang (htmlStringPage: string, paramLang: string) { |
328 | return htmlStringPage.replace('<html>', `<html lang="${paramLang}">`) | |
329 | } | |
330 | ||
caf2aaf4 K |
331 | private static addManifestContentHash (htmlStringPage: string) { |
332 | return htmlStringPage.replace('[manifestContentHash]', FILES_CONTENT_HASH.MANIFEST) | |
333 | } | |
334 | ||
1fdf8edb | 335 | private static addFaviconContentHash (htmlStringPage: string) { |
caf2aaf4 K |
336 | return htmlStringPage.replace('[faviconContentHash]', FILES_CONTENT_HASH.FAVICON) |
337 | } | |
338 | ||
1fdf8edb | 339 | private static addLogoContentHash (htmlStringPage: string) { |
caf2aaf4 K |
340 | return htmlStringPage.replace('[logoContentHash]', FILES_CONTENT_HASH.LOGO) |
341 | } | |
342 | ||
9aac4423 C |
343 | private static addTitleTag (htmlStringPage: string, title?: string) { |
344 | let text = title || CONFIG.INSTANCE.NAME | |
345 | if (title) text += ` - ${CONFIG.INSTANCE.NAME}` | |
346 | ||
347 | const titleTag = `<title>${text}</title>` | |
e032aec9 C |
348 | |
349 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.TITLE, titleTag) | |
350 | } | |
351 | ||
9aac4423 C |
352 | private static addDescriptionTag (htmlStringPage: string, description?: string) { |
353 | const content = description || CONFIG.INSTANCE.SHORT_DESCRIPTION | |
354 | const descriptionTag = `<meta name="description" content="${content}" />` | |
e032aec9 C |
355 | |
356 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.DESCRIPTION, descriptionTag) | |
357 | } | |
358 | ||
359 | private static addCustomCSS (htmlStringPage: string) { | |
ffb321be | 360 | const styleTag = `<style class="custom-css-style">${CONFIG.INSTANCE.CUSTOMIZATIONS.CSS}</style>` |
e032aec9 C |
361 | |
362 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.CUSTOM_CSS, styleTag) | |
363 | } | |
364 | ||
aea0b0e7 C |
365 | private static addServerConfig (htmlStringPage: string, serverConfig: HTMLServerConfig) { |
366 | const serverConfigString = JSON.stringify(serverConfig) | |
367 | const configScriptTag = `<script type="application/javascript">window.PeerTubeServerConfig = '${serverConfigString}'</script>` | |
368 | ||
369 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.SERVER_CONFIG, configScriptTag) | |
370 | } | |
371 | ||
a8b666e9 C |
372 | private static async addAsyncPluginCSS (htmlStringPage: string) { |
373 | const globalCSSContent = await readFile(PLUGIN_GLOBAL_CSS_PATH) | |
3e753302 | 374 | if (globalCSSContent.byteLength === 0) return htmlStringPage |
a8b666e9 C |
375 | |
376 | const fileHash = sha256(globalCSSContent) | |
377 | const linkTag = `<link rel="stylesheet" href="/plugins/global.css?hash=${fileHash}" />` | |
ffb321be C |
378 | |
379 | return htmlStringPage.replace('</head>', linkTag + '</head>') | |
380 | } | |
381 | ||
6fad8e51 | 382 | private static generateOpenGraphMetaTags (tags: Tags) { |
8d987ec6 K |
383 | const metaTags = { |
384 | 'og:type': tags.ogType, | |
865af3fd | 385 | 'og:site_name': tags.siteName, |
8d987ec6 K |
386 | 'og:title': tags.title, |
387 | 'og:image': tags.image.url | |
388 | } | |
389 | ||
390 | if (tags.image.width && tags.image.height) { | |
391 | metaTags['og:image:width'] = tags.image.width | |
392 | metaTags['og:image:height'] = tags.image.height | |
393 | } | |
e032aec9 | 394 | |
8d987ec6 | 395 | metaTags['og:url'] = tags.url |
a073c912 | 396 | metaTags['og:description'] = mdToPlainText(tags.description) |
e032aec9 | 397 | |
8d987ec6 K |
398 | if (tags.embed) { |
399 | metaTags['og:video:url'] = tags.embed.url | |
400 | metaTags['og:video:secure_url'] = tags.embed.url | |
401 | metaTags['og:video:type'] = 'text/html' | |
402 | metaTags['og:video:width'] = EMBED_SIZE.width | |
403 | metaTags['og:video:height'] = EMBED_SIZE.height | |
404 | } | |
e032aec9 | 405 | |
8d987ec6 K |
406 | return metaTags |
407 | } | |
e032aec9 | 408 | |
6fad8e51 | 409 | private static generateStandardMetaTags (tags: Tags) { |
8d987ec6 K |
410 | return { |
411 | name: tags.title, | |
a073c912 | 412 | description: mdToPlainText(tags.description), |
8d987ec6 K |
413 | image: tags.image.url |
414 | } | |
415 | } | |
e032aec9 | 416 | |
6fad8e51 | 417 | private static generateTwitterCardMetaTags (tags: Tags) { |
8d987ec6 K |
418 | const metaTags = { |
419 | 'twitter:card': tags.twitterCard, | |
e032aec9 | 420 | 'twitter:site': CONFIG.SERVICES.TWITTER.USERNAME, |
8d987ec6 K |
421 | 'twitter:title': tags.title, |
422 | 'twitter:description': tags.description, | |
423 | 'twitter:image': tags.image.url | |
e032aec9 C |
424 | } |
425 | ||
8d987ec6 K |
426 | if (tags.image.width && tags.image.height) { |
427 | metaTags['twitter:image:width'] = tags.image.width | |
428 | metaTags['twitter:image:height'] = tags.image.height | |
429 | } | |
430 | ||
b96777c3 C |
431 | if (tags.twitterCard === 'player') { |
432 | metaTags['twitter:player'] = tags.embed.url | |
433 | metaTags['twitter:player:width'] = EMBED_SIZE.width | |
434 | metaTags['twitter:player:height'] = EMBED_SIZE.height | |
435 | } | |
436 | ||
8d987ec6 K |
437 | return metaTags |
438 | } | |
e032aec9 | 439 | |
6fad8e51 | 440 | private static generateSchemaTags (tags: Tags) { |
8d987ec6 | 441 | const schema = { |
e032aec9 | 442 | '@context': 'http://schema.org', |
8d987ec6 K |
443 | '@type': tags.schemaType, |
444 | 'name': tags.title, | |
445 | 'description': tags.description, | |
446 | 'image': tags.image.url, | |
447 | 'url': tags.url | |
448 | } | |
449 | ||
450 | if (tags.list) { | |
451 | schema['numberOfItems'] = tags.list.numberOfItems | |
452 | schema['thumbnailUrl'] = tags.image.url | |
453 | } | |
454 | ||
455 | if (tags.embed) { | |
456 | schema['embedUrl'] = tags.embed.url | |
457 | schema['uploadDate'] = tags.embed.createdAt | |
6fad8e51 C |
458 | |
459 | if (tags.embed.duration) schema['duration'] = tags.embed.duration | |
460 | if (tags.embed.views) schema['iterationCount'] = tags.embed.views | |
461 | ||
8d987ec6 K |
462 | schema['thumbnailUrl'] = tags.image.url |
463 | schema['contentUrl'] = tags.url | |
464 | } | |
465 | ||
466 | return schema | |
467 | } | |
468 | ||
6fad8e51 | 469 | private static addTags (htmlStringPage: string, tagsValues: Tags) { |
8d987ec6 K |
470 | const openGraphMetaTags = this.generateOpenGraphMetaTags(tagsValues) |
471 | const standardMetaTags = this.generateStandardMetaTags(tagsValues) | |
472 | const twitterCardMetaTags = this.generateTwitterCardMetaTags(tagsValues) | |
473 | const schemaTags = this.generateSchemaTags(tagsValues) | |
474 | ||
106fa224 | 475 | const { url, title, embed, originUrl } = tagsValues |
8d987ec6 | 476 | |
6fad8e51 | 477 | const oembedLinkTags: { type: string, href: string, title: string }[] = [] |
8d987ec6 K |
478 | |
479 | if (embed) { | |
480 | oembedLinkTags.push({ | |
481 | type: 'application/json+oembed', | |
482 | href: WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(url), | |
483 | title | |
484 | }) | |
e032aec9 C |
485 | } |
486 | ||
487 | let tagsString = '' | |
488 | ||
489 | // Opengraph | |
490 | Object.keys(openGraphMetaTags).forEach(tagName => { | |
a1587156 | 491 | const tagValue = openGraphMetaTags[tagName] |
e032aec9 C |
492 | |
493 | tagsString += `<meta property="${tagName}" content="${tagValue}" />` | |
494 | }) | |
495 | ||
8d987ec6 K |
496 | // Standard |
497 | Object.keys(standardMetaTags).forEach(tagName => { | |
498 | const tagValue = standardMetaTags[tagName] | |
499 | ||
500 | tagsString += `<meta property="${tagName}" content="${tagValue}" />` | |
501 | }) | |
502 | ||
503 | // Twitter card | |
504 | Object.keys(twitterCardMetaTags).forEach(tagName => { | |
505 | const tagValue = twitterCardMetaTags[tagName] | |
506 | ||
507 | tagsString += `<meta property="${tagName}" content="${tagValue}" />` | |
508 | }) | |
509 | ||
e032aec9 C |
510 | // OEmbed |
511 | for (const oembedLinkTag of oembedLinkTags) { | |
512 | tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />` | |
513 | } | |
514 | ||
515 | // Schema.org | |
8d987ec6 K |
516 | if (schemaTags) { |
517 | tagsString += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>` | |
518 | } | |
92bf2f62 | 519 | |
8d987ec6 | 520 | // SEO, use origin URL |
106fa224 | 521 | tagsString += `<link rel="canonical" href="${originUrl}" />` |
92bf2f62 | 522 | |
8d987ec6 | 523 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.META_TAGS, tagsString) |
e032aec9 C |
524 | } |
525 | } | |
f2eb23cd RK |
526 | |
527 | function sendHTML (html: string, res: express.Response) { | |
528 | res.set('Content-Type', 'text/html; charset=UTF-8') | |
529 | ||
530 | return res.send(html) | |
531 | } | |
532 | ||
533 | async function serveIndexHTML (req: express.Request, res: express.Response) { | |
534 | if (req.accepts(ACCEPT_HEADERS) === 'html' || | |
535 | !req.headers.accept) { | |
536 | try { | |
537 | await generateHTMLPage(req, res, req.params.language) | |
538 | return | |
539 | } catch (err) { | |
540 | logger.error('Cannot generate HTML page.', err) | |
541 | return res.sendStatus(HttpStatusCode.INTERNAL_SERVER_ERROR_500) | |
542 | } | |
543 | } | |
544 | ||
545 | return res.sendStatus(HttpStatusCode.NOT_ACCEPTABLE_406) | |
546 | } | |
547 | ||
548 | // --------------------------------------------------------------------------- | |
549 | ||
550 | export { | |
551 | ClientHtml, | |
552 | sendHTML, | |
553 | serveIndexHTML | |
554 | } | |
555 | ||
556 | async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) { | |
557 | const html = await ClientHtml.getDefaultHTMLPage(req, res, paramLang) | |
558 | ||
559 | return sendHTML(html, res) | |
560 | } |