]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/client-html.ts
Set canonical link to original video/playlist url
[github/Chocobozzz/PeerTube.git] / server / lib / client-html.ts
1 import * as express from 'express'
2 import { buildFileLocale, getDefaultLocale, is18nLocale, POSSIBLE_LOCALES } from '../../shared/core-utils/i18n/i18n'
3 import {
4 AVATARS_SIZE,
5 CUSTOM_HTML_TAG_COMMENTS,
6 EMBED_SIZE,
7 PLUGIN_GLOBAL_CSS_PATH,
8 WEBSERVER,
9 FILES_CONTENT_HASH
10 } from '../initializers/constants'
11 import { join } from 'path'
12 import { escapeHTML, isTestInstance, sha256 } from '../helpers/core-utils'
13 import { VideoModel } from '../models/video/video'
14 import { VideoPlaylistModel } from '../models/video/video-playlist'
15 import validator from 'validator'
16 import { VideoPrivacy, VideoPlaylistPrivacy } from '../../shared/models/videos'
17 import { readFile } from 'fs-extra'
18 import { getActivityStreamDuration } from '../models/video/video-format-utils'
19 import { AccountModel } from '../models/account/account'
20 import { VideoChannelModel } from '../models/video/video-channel'
21 import * as Bluebird from 'bluebird'
22 import { CONFIG } from '../initializers/config'
23 import { logger } from '../helpers/logger'
24 import { MAccountActor, MChannelActor } from '../types/models'
25 import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes'
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 export 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, originUrl, siteName, title, description, image, embed, ogType, twitterCard, schemaType
121 })
122
123 return customHtml
124 }
125
126 static async getWatchPlaylistHTMLPage (videoPlaylistId: string, req: express.Request, res: express.Response) {
127 // Let Angular application handle errors
128 if (!validator.isInt(videoPlaylistId) && !validator.isUUID(videoPlaylistId, 4)) {
129 res.status(HttpStatusCode.NOT_FOUND_404)
130 return ClientHtml.getIndexHTML(req, res)
131 }
132
133 const [ html, videoPlaylist ] = await Promise.all([
134 ClientHtml.getIndexHTML(req, res),
135 VideoPlaylistModel.loadWithAccountAndChannel(videoPlaylistId, null)
136 ])
137
138 // Let Angular application handle errors
139 if (!videoPlaylist || videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) {
140 res.status(HttpStatusCode.NOT_FOUND_404)
141 return html
142 }
143
144 let customHtml = ClientHtml.addTitleTag(html, escapeHTML(videoPlaylist.name))
145 customHtml = ClientHtml.addDescriptionTag(customHtml, escapeHTML(videoPlaylist.description))
146
147 const url = videoPlaylist.getWatchUrl()
148 const originUrl = videoPlaylist.url
149 const title = escapeHTML(videoPlaylist.name)
150 const siteName = escapeHTML(CONFIG.INSTANCE.NAME)
151 const description = escapeHTML(videoPlaylist.description)
152
153 const image = {
154 url: videoPlaylist.getThumbnailUrl()
155 }
156
157 const embed = {
158 url: WEBSERVER.URL + videoPlaylist.getEmbedStaticPath(),
159 createdAt: videoPlaylist.createdAt.toISOString()
160 }
161
162 const list = {
163 numberOfItems: videoPlaylist.get('videosLength') as number
164 }
165
166 const ogType = 'video'
167 const twitterCard = CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary'
168 const schemaType = 'ItemList'
169
170 customHtml = ClientHtml.addTags(customHtml, {
171 url, originUrl, siteName, embed, title, description, image, list, ogType, twitterCard, schemaType
172 })
173
174 return customHtml
175 }
176
177 static async getAccountHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
178 return this.getAccountOrChannelHTMLPage(() => AccountModel.loadByNameWithHost(nameWithHost), req, res)
179 }
180
181 static async getVideoChannelHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
182 return this.getAccountOrChannelHTMLPage(() => VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithHost), req, res)
183 }
184
185 static async getEmbedHTML () {
186 const path = ClientHtml.getEmbedPath()
187
188 if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path]
189
190 const buffer = await readFile(path)
191
192 let html = buffer.toString()
193 html = await ClientHtml.addAsyncPluginCSS(html)
194
195 ClientHtml.htmlCache[path] = html
196
197 return html
198 }
199
200 private static async getAccountOrChannelHTMLPage (
201 loader: () => Bluebird<MAccountActor | MChannelActor>,
202 req: express.Request,
203 res: express.Response
204 ) {
205 const [ html, entity ] = await Promise.all([
206 ClientHtml.getIndexHTML(req, res),
207 loader()
208 ])
209
210 // Let Angular application handle errors
211 if (!entity) {
212 res.status(HttpStatusCode.NOT_FOUND_404)
213 return ClientHtml.getIndexHTML(req, res)
214 }
215
216 let customHtml = ClientHtml.addTitleTag(html, escapeHTML(entity.getDisplayName()))
217 customHtml = ClientHtml.addDescriptionTag(customHtml, escapeHTML(entity.description))
218
219 const url = entity.Actor.url
220 const originUrl = entity.Actor.url
221 const siteName = escapeHTML(CONFIG.INSTANCE.NAME)
222 const title = escapeHTML(entity.getDisplayName())
223 const description = escapeHTML(entity.description)
224
225 const image = {
226 url: entity.Actor.getAvatarUrl(),
227 width: AVATARS_SIZE.width,
228 height: AVATARS_SIZE.height
229 }
230
231 const ogType = 'website'
232 const twitterCard = 'summary'
233 const schemaType = 'ProfilePage'
234
235 customHtml = ClientHtml.addTags(customHtml, { url, originUrl, title, siteName, description, image, ogType, twitterCard, schemaType })
236
237 return customHtml
238 }
239
240 private static async getIndexHTML (req: express.Request, res: express.Response, paramLang?: string) {
241 const path = ClientHtml.getIndexPath(req, res, paramLang)
242 if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path]
243
244 const buffer = await readFile(path)
245
246 let html = buffer.toString()
247
248 if (paramLang) html = ClientHtml.addHtmlLang(html, paramLang)
249 html = ClientHtml.addManifestContentHash(html)
250 html = ClientHtml.addFaviconContentHash(html)
251 html = ClientHtml.addLogoContentHash(html)
252 html = ClientHtml.addCustomCSS(html)
253 html = await ClientHtml.addAsyncPluginCSS(html)
254
255 ClientHtml.htmlCache[path] = html
256
257 return html
258 }
259
260 private static getIndexPath (req: express.Request, res: express.Response, paramLang: string) {
261 let lang: string
262
263 // Check param lang validity
264 if (paramLang && is18nLocale(paramLang)) {
265 lang = paramLang
266
267 // Save locale in cookies
268 res.cookie('clientLanguage', lang, {
269 secure: WEBSERVER.SCHEME === 'https',
270 sameSite: 'none',
271 maxAge: 1000 * 3600 * 24 * 90 // 3 months
272 })
273
274 } else if (req.cookies.clientLanguage && is18nLocale(req.cookies.clientLanguage)) {
275 lang = req.cookies.clientLanguage
276 } else {
277 lang = req.acceptsLanguages(POSSIBLE_LOCALES) || getDefaultLocale()
278 }
279
280 return join(__dirname, '../../../client/dist/' + buildFileLocale(lang) + '/index.html')
281 }
282
283 private static getEmbedPath () {
284 return join(__dirname, '../../../client/dist/standalone/videos/embed.html')
285 }
286
287 private static addHtmlLang (htmlStringPage: string, paramLang: string) {
288 return htmlStringPage.replace('<html>', `<html lang="${paramLang}">`)
289 }
290
291 private static addManifestContentHash (htmlStringPage: string) {
292 return htmlStringPage.replace('[manifestContentHash]', FILES_CONTENT_HASH.MANIFEST)
293 }
294
295 private static addFaviconContentHash (htmlStringPage: string) {
296 return htmlStringPage.replace('[faviconContentHash]', FILES_CONTENT_HASH.FAVICON)
297 }
298
299 private static addLogoContentHash (htmlStringPage: string) {
300 return htmlStringPage.replace('[logoContentHash]', FILES_CONTENT_HASH.LOGO)
301 }
302
303 private static addTitleTag (htmlStringPage: string, title?: string) {
304 let text = title || CONFIG.INSTANCE.NAME
305 if (title) text += ` - ${CONFIG.INSTANCE.NAME}`
306
307 const titleTag = `<title>${text}</title>`
308
309 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.TITLE, titleTag)
310 }
311
312 private static addDescriptionTag (htmlStringPage: string, description?: string) {
313 const content = description || CONFIG.INSTANCE.SHORT_DESCRIPTION
314 const descriptionTag = `<meta name="description" content="${content}" />`
315
316 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.DESCRIPTION, descriptionTag)
317 }
318
319 private static addCustomCSS (htmlStringPage: string) {
320 const styleTag = `<style class="custom-css-style">${CONFIG.INSTANCE.CUSTOMIZATIONS.CSS}</style>`
321
322 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.CUSTOM_CSS, styleTag)
323 }
324
325 private static async addAsyncPluginCSS (htmlStringPage: string) {
326 const globalCSSContent = await readFile(PLUGIN_GLOBAL_CSS_PATH)
327 if (globalCSSContent.byteLength === 0) return htmlStringPage
328
329 const fileHash = sha256(globalCSSContent)
330 const linkTag = `<link rel="stylesheet" href="/plugins/global.css?hash=${fileHash}" />`
331
332 return htmlStringPage.replace('</head>', linkTag + '</head>')
333 }
334
335 private static generateOpenGraphMetaTags (tags: Tags) {
336 const metaTags = {
337 'og:type': tags.ogType,
338 'og:site_name': tags.siteName,
339 'og:title': tags.title,
340 'og:image': tags.image.url
341 }
342
343 if (tags.image.width && tags.image.height) {
344 metaTags['og:image:width'] = tags.image.width
345 metaTags['og:image:height'] = tags.image.height
346 }
347
348 metaTags['og:url'] = tags.url
349 metaTags['og:description'] = tags.description
350
351 if (tags.embed) {
352 metaTags['og:video:url'] = tags.embed.url
353 metaTags['og:video:secure_url'] = tags.embed.url
354 metaTags['og:video:type'] = 'text/html'
355 metaTags['og:video:width'] = EMBED_SIZE.width
356 metaTags['og:video:height'] = EMBED_SIZE.height
357 }
358
359 return metaTags
360 }
361
362 private static generateStandardMetaTags (tags: Tags) {
363 return {
364 name: tags.title,
365 description: tags.description,
366 image: tags.image.url
367 }
368 }
369
370 private static generateTwitterCardMetaTags (tags: Tags) {
371 const metaTags = {
372 'twitter:card': tags.twitterCard,
373 'twitter:site': CONFIG.SERVICES.TWITTER.USERNAME,
374 'twitter:title': tags.title,
375 'twitter:description': tags.description,
376 'twitter:image': tags.image.url
377 }
378
379 if (tags.image.width && tags.image.height) {
380 metaTags['twitter:image:width'] = tags.image.width
381 metaTags['twitter:image:height'] = tags.image.height
382 }
383
384 if (tags.twitterCard === 'player') {
385 metaTags['twitter:player'] = tags.embed.url
386 metaTags['twitter:player:width'] = EMBED_SIZE.width
387 metaTags['twitter:player:height'] = EMBED_SIZE.height
388 }
389
390 return metaTags
391 }
392
393 private static generateSchemaTags (tags: Tags) {
394 const schema = {
395 '@context': 'http://schema.org',
396 '@type': tags.schemaType,
397 'name': tags.title,
398 'description': tags.description,
399 'image': tags.image.url,
400 'url': tags.url
401 }
402
403 if (tags.list) {
404 schema['numberOfItems'] = tags.list.numberOfItems
405 schema['thumbnailUrl'] = tags.image.url
406 }
407
408 if (tags.embed) {
409 schema['embedUrl'] = tags.embed.url
410 schema['uploadDate'] = tags.embed.createdAt
411
412 if (tags.embed.duration) schema['duration'] = tags.embed.duration
413 if (tags.embed.views) schema['iterationCount'] = tags.embed.views
414
415 schema['thumbnailUrl'] = tags.image.url
416 schema['contentUrl'] = tags.url
417 }
418
419 return schema
420 }
421
422 private static addTags (htmlStringPage: string, tagsValues: Tags) {
423 const openGraphMetaTags = this.generateOpenGraphMetaTags(tagsValues)
424 const standardMetaTags = this.generateStandardMetaTags(tagsValues)
425 const twitterCardMetaTags = this.generateTwitterCardMetaTags(tagsValues)
426 const schemaTags = this.generateSchemaTags(tagsValues)
427
428 const { url, title, embed, originUrl } = tagsValues
429
430 const oembedLinkTags: { type: string, href: string, title: string }[] = []
431
432 if (embed) {
433 oembedLinkTags.push({
434 type: 'application/json+oembed',
435 href: WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(url),
436 title
437 })
438 }
439
440 let tagsString = ''
441
442 // Opengraph
443 Object.keys(openGraphMetaTags).forEach(tagName => {
444 const tagValue = openGraphMetaTags[tagName]
445
446 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
447 })
448
449 // Standard
450 Object.keys(standardMetaTags).forEach(tagName => {
451 const tagValue = standardMetaTags[tagName]
452
453 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
454 })
455
456 // Twitter card
457 Object.keys(twitterCardMetaTags).forEach(tagName => {
458 const tagValue = twitterCardMetaTags[tagName]
459
460 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
461 })
462
463 // OEmbed
464 for (const oembedLinkTag of oembedLinkTags) {
465 tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />`
466 }
467
468 // Schema.org
469 if (schemaTags) {
470 tagsString += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>`
471 }
472
473 // SEO, use origin URL
474 tagsString += `<link rel="canonical" href="${originUrl}" />`
475
476 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.META_TAGS, tagsString)
477 }
478 }