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