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