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