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