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