]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/client-html.ts
f19ec7df02ebe6ca540c932b951c21a310b51e32
[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 { buildFileLocale, getDefaultLocale, is18nLocale, POSSIBLE_LOCALES } from '../../shared/core-utils/i18n/i18n'
6 import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes'
7 import { VideoPlaylistPrivacy, VideoPrivacy } from '../../shared/models/videos'
8 import { escapeHTML, isTestInstance, sha256 } from '../helpers/core-utils'
9 import { logger } from '../helpers/logger'
10 import { CONFIG } from '../initializers/config'
11 import {
12 ACCEPT_HEADERS,
13 AVATARS_SIZE,
14 CUSTOM_HTML_TAG_COMMENTS,
15 EMBED_SIZE,
16 FILES_CONTENT_HASH,
17 PLUGIN_GLOBAL_CSS_PATH,
18 WEBSERVER
19 } from '../initializers/constants'
20 import { AccountModel } from '../models/account/account'
21 import { VideoModel } from '../models/video/video'
22 import { VideoChannelModel } from '../models/video/video-channel'
23 import { getActivityStreamDuration } from '../models/video/video-format-utils'
24 import { VideoPlaylistModel } from '../models/video/video-playlist'
25 import { MAccountActor, MChannelActor } from '../types/models'
26
27 type Tags = {
28 ogType: string
29 twitterCard: 'player' | 'summary' | 'summary_large_image'
30 schemaType: string
31
32 list?: {
33 numberOfItems: number
34 }
35
36 siteName: string
37 title: string
38 url: string
39 originUrl: string
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
56 class ClientHtml {
57
58 private static htmlCache: { [path: string]: string } = {}
59
60 static invalidCache () {
61 logger.info('Cleaning HTML cache.')
62
63 ClientHtml.htmlCache = {}
64 }
65
66 static async getDefaultHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
67 const html = paramLang
68 ? await ClientHtml.getIndexHTML(req, res, paramLang)
69 : await ClientHtml.getIndexHTML(req, res)
70
71 let customHtml = ClientHtml.addTitleTag(html)
72 customHtml = ClientHtml.addDescriptionTag(customHtml)
73
74 return customHtml
75 }
76
77 static async getWatchHTMLPage (videoId: string, req: express.Request, res: express.Response) {
78 // Let Angular application handle errors
79 if (!validator.isInt(videoId) && !validator.isUUID(videoId, 4)) {
80 res.status(HttpStatusCode.NOT_FOUND_404)
81 return ClientHtml.getIndexHTML(req, res)
82 }
83
84 const [ html, video ] = await Promise.all([
85 ClientHtml.getIndexHTML(req, res),
86 VideoModel.loadWithBlacklist(videoId)
87 ])
88
89 // Let Angular application handle errors
90 if (!video || video.privacy === VideoPrivacy.PRIVATE || video.privacy === VideoPrivacy.INTERNAL || video.VideoBlacklist) {
91 res.status(HttpStatusCode.NOT_FOUND_404)
92 return html
93 }
94
95 let customHtml = ClientHtml.addTitleTag(html, escapeHTML(video.name))
96 customHtml = ClientHtml.addDescriptionTag(customHtml, escapeHTML(video.description))
97
98 const url = WEBSERVER.URL + video.getWatchStaticPath()
99 const originUrl = video.url
100 const title = escapeHTML(video.name)
101 const siteName = escapeHTML(CONFIG.INSTANCE.NAME)
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
119 customHtml = ClientHtml.addTags(customHtml, {
120 url,
121 originUrl,
122 siteName,
123 title,
124 description,
125 image,
126 embed,
127 ogType,
128 twitterCard,
129 schemaType
130 })
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)) {
138 res.status(HttpStatusCode.NOT_FOUND_404)
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) {
149 res.status(HttpStatusCode.NOT_FOUND_404)
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()
157 const originUrl = videoPlaylist.url
158 const title = escapeHTML(videoPlaylist.name)
159 const siteName = escapeHTML(CONFIG.INSTANCE.NAME)
160 const description = escapeHTML(videoPlaylist.description)
161
162 const image = {
163 url: videoPlaylist.getThumbnailUrl()
164 }
165
166 const embed = {
167 url: WEBSERVER.URL + videoPlaylist.getEmbedStaticPath(),
168 createdAt: videoPlaylist.createdAt.toISOString()
169 }
170
171 const list = {
172 numberOfItems: videoPlaylist.get('videosLength') as number
173 }
174
175 const ogType = 'video'
176 const twitterCard = CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary'
177 const schemaType = 'ItemList'
178
179 customHtml = ClientHtml.addTags(customHtml, {
180 url,
181 originUrl,
182 siteName,
183 embed,
184 title,
185 description,
186 image,
187 list,
188 ogType,
189 twitterCard,
190 schemaType
191 })
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
204 static async getEmbedHTML () {
205 const path = ClientHtml.getEmbedPath()
206
207 if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path]
208
209 const buffer = await readFile(path)
210
211 let html = buffer.toString()
212 html = await ClientHtml.addAsyncPluginCSS(html)
213 html = ClientHtml.addCustomCSS(html)
214 html = ClientHtml.addTitleTag(html)
215
216 ClientHtml.htmlCache[path] = html
217
218 return html
219 }
220
221 private static async getAccountOrChannelHTMLPage (
222 loader: () => Promise<MAccountActor | MChannelActor>,
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) {
233 res.status(HttpStatusCode.NOT_FOUND_404)
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))
239
240 const url = entity.getLocalUrl()
241 const originUrl = entity.Actor.url
242 const siteName = escapeHTML(CONFIG.INSTANCE.NAME)
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
256 customHtml = ClientHtml.addTags(customHtml, {
257 url,
258 originUrl,
259 title,
260 siteName,
261 description,
262 image,
263 ogType,
264 twitterCard,
265 schemaType
266 })
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)
273 if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path]
274
275 const buffer = await readFile(path)
276
277 let html = buffer.toString()
278
279 if (paramLang) html = ClientHtml.addHtmlLang(html, paramLang)
280 html = ClientHtml.addManifestContentHash(html)
281 html = ClientHtml.addFaviconContentHash(html)
282 html = ClientHtml.addLogoContentHash(html)
283 html = ClientHtml.addCustomCSS(html)
284 html = await ClientHtml.addAsyncPluginCSS(html)
285
286 ClientHtml.htmlCache[path] = html
287
288 return html
289 }
290
291 private static getIndexPath (req: express.Request, res: express.Response, paramLang: string) {
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, {
300 secure: WEBSERVER.SCHEME === 'https',
301 sameSite: 'none',
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
314 private static getEmbedPath () {
315 return join(__dirname, '../../../client/dist/standalone/videos/embed.html')
316 }
317
318 private static addHtmlLang (htmlStringPage: string, paramLang: string) {
319 return htmlStringPage.replace('<html>', `<html lang="${paramLang}">`)
320 }
321
322 private static addManifestContentHash (htmlStringPage: string) {
323 return htmlStringPage.replace('[manifestContentHash]', FILES_CONTENT_HASH.MANIFEST)
324 }
325
326 private static addFaviconContentHash (htmlStringPage: string) {
327 return htmlStringPage.replace('[faviconContentHash]', FILES_CONTENT_HASH.FAVICON)
328 }
329
330 private static addLogoContentHash (htmlStringPage: string) {
331 return htmlStringPage.replace('[logoContentHash]', FILES_CONTENT_HASH.LOGO)
332 }
333
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>`
339
340 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.TITLE, titleTag)
341 }
342
343 private static addDescriptionTag (htmlStringPage: string, description?: string) {
344 const content = description || CONFIG.INSTANCE.SHORT_DESCRIPTION
345 const descriptionTag = `<meta name="description" content="${content}" />`
346
347 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.DESCRIPTION, descriptionTag)
348 }
349
350 private static addCustomCSS (htmlStringPage: string) {
351 const styleTag = `<style class="custom-css-style">${CONFIG.INSTANCE.CUSTOMIZATIONS.CSS}</style>`
352
353 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.CUSTOM_CSS, styleTag)
354 }
355
356 private static async addAsyncPluginCSS (htmlStringPage: string) {
357 const globalCSSContent = await readFile(PLUGIN_GLOBAL_CSS_PATH)
358 if (globalCSSContent.byteLength === 0) return htmlStringPage
359
360 const fileHash = sha256(globalCSSContent)
361 const linkTag = `<link rel="stylesheet" href="/plugins/global.css?hash=${fileHash}" />`
362
363 return htmlStringPage.replace('</head>', linkTag + '</head>')
364 }
365
366 private static generateOpenGraphMetaTags (tags: Tags) {
367 const metaTags = {
368 'og:type': tags.ogType,
369 'og:site_name': tags.siteName,
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 }
378
379 metaTags['og:url'] = tags.url
380 metaTags['og:description'] = tags.description
381
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 }
389
390 return metaTags
391 }
392
393 private static generateStandardMetaTags (tags: Tags) {
394 return {
395 name: tags.title,
396 description: tags.description,
397 image: tags.image.url
398 }
399 }
400
401 private static generateTwitterCardMetaTags (tags: Tags) {
402 const metaTags = {
403 'twitter:card': tags.twitterCard,
404 'twitter:site': CONFIG.SERVICES.TWITTER.USERNAME,
405 'twitter:title': tags.title,
406 'twitter:description': tags.description,
407 'twitter:image': tags.image.url
408 }
409
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
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
421 return metaTags
422 }
423
424 private static generateSchemaTags (tags: Tags) {
425 const schema = {
426 '@context': 'http://schema.org',
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
442
443 if (tags.embed.duration) schema['duration'] = tags.embed.duration
444 if (tags.embed.views) schema['iterationCount'] = tags.embed.views
445
446 schema['thumbnailUrl'] = tags.image.url
447 schema['contentUrl'] = tags.url
448 }
449
450 return schema
451 }
452
453 private static addTags (htmlStringPage: string, tagsValues: Tags) {
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
459 const { url, title, embed, originUrl } = tagsValues
460
461 const oembedLinkTags: { type: string, href: string, title: string }[] = []
462
463 if (embed) {
464 oembedLinkTags.push({
465 type: 'application/json+oembed',
466 href: WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(url),
467 title
468 })
469 }
470
471 let tagsString = ''
472
473 // Opengraph
474 Object.keys(openGraphMetaTags).forEach(tagName => {
475 const tagValue = openGraphMetaTags[tagName]
476
477 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
478 })
479
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
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
500 if (schemaTags) {
501 tagsString += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>`
502 }
503
504 // SEO, use origin URL
505 tagsString += `<link rel="canonical" href="${originUrl}" />`
506
507 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.META_TAGS, tagsString)
508 }
509 }
510
511 function sendHTML (html: string, res: express.Response) {
512 res.set('Content-Type', 'text/html; charset=UTF-8')
513
514 return res.send(html)
515 }
516
517 async 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
534 export {
535 ClientHtml,
536 sendHTML,
537 serveIndexHTML
538 }
539
540 async 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 }