]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/client-html.ts
Set canonical link to original video/playlist url
[github/Chocobozzz/PeerTube.git] / server / lib / client-html.ts
CommitLineData
e032aec9 1import * as express from 'express'
bd45d503 2import { buildFileLocale, getDefaultLocale, is18nLocale, POSSIBLE_LOCALES } from '../../shared/core-utils/i18n/i18n'
8d987ec6
K
3import {
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'
e032aec9 11import { join } from 'path'
b9da21bd 12import { escapeHTML, isTestInstance, sha256 } from '../helpers/core-utils'
e032aec9 13import { VideoModel } from '../models/video/video'
8d987ec6 14import { VideoPlaylistModel } from '../models/video/video-playlist'
7cde3b9c 15import validator from 'validator'
8d987ec6 16import { VideoPrivacy, VideoPlaylistPrivacy } from '../../shared/models/videos'
62689b94 17import { readFile } from 'fs-extra'
098eb377 18import { getActivityStreamDuration } from '../models/video/video-format-utils'
92bf2f62
C
19import { AccountModel } from '../models/account/account'
20import { VideoChannelModel } from '../models/video/video-channel'
21import * as Bluebird from 'bluebird'
6dd9de95 22import { CONFIG } from '../initializers/config'
3e753302 23import { logger } from '../helpers/logger'
8d987ec6 24import { MAccountActor, MChannelActor } from '../types/models'
2d53be02 25import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes'
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
e032aec9
C
56export class ClientHtml {
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
TC
119 customHtml = ClientHtml.addTags(customHtml, {
120 url, originUrl, siteName, title, description, image, embed, ogType, twitterCard, schemaType
121 })
8d987ec6
K
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)) {
2d53be02 129 res.status(HttpStatusCode.NOT_FOUND_404)
8d987ec6
K
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) {
2d53be02 140 res.status(HttpStatusCode.NOT_FOUND_404)
8d987ec6
K
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()
106fa224 148 const originUrl = videoPlaylist.url
8d987ec6 149 const title = escapeHTML(videoPlaylist.name)
865af3fd 150 const siteName = escapeHTML(CONFIG.INSTANCE.NAME)
8d987ec6
K
151 const description = escapeHTML(videoPlaylist.description)
152
153 const image = {
154 url: videoPlaylist.getThumbnailUrl()
155 }
156
6fad8e51
C
157 const embed = {
158 url: WEBSERVER.URL + videoPlaylist.getEmbedStaticPath(),
159 createdAt: videoPlaylist.createdAt.toISOString()
160 }
161
8d987ec6 162 const list = {
6fad8e51 163 numberOfItems: videoPlaylist.get('videosLength') as number
8d987ec6
K
164 }
165
166 const ogType = 'video'
6fad8e51 167 const twitterCard = CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary'
8d987ec6
K
168 const schemaType = 'ItemList'
169
106fa224
TC
170 customHtml = ClientHtml.addTags(customHtml, {
171 url, originUrl, siteName, embed, title, description, image, list, ogType, twitterCard, schemaType
172 })
92bf2f62
C
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
cf649c2e
C
185 static async getEmbedHTML () {
186 const path = ClientHtml.getEmbedPath()
cf649c2e 187
b9da21bd 188 if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path]
cf649c2e
C
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
92bf2f62 200 private static async getAccountOrChannelHTMLPage (
453e83ea 201 loader: () => Bluebird<MAccountActor | MChannelActor>,
92bf2f62
C
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) {
2d53be02 212 res.status(HttpStatusCode.NOT_FOUND_404)
92bf2f62
C
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))
8d987ec6
K
218
219 const url = entity.Actor.url
106fa224 220 const originUrl = entity.Actor.url
865af3fd 221 const siteName = escapeHTML(CONFIG.INSTANCE.NAME)
8d987ec6
K
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
106fa224 235 customHtml = ClientHtml.addTags(customHtml, { url, originUrl, title, siteName, description, image, ogType, twitterCard, schemaType })
9aac4423
C
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)
b9da21bd 242 if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path]
9aac4423
C
243
244 const buffer = await readFile(path)
245
246 let html = buffer.toString()
247
7738273b 248 if (paramLang) html = ClientHtml.addHtmlLang(html, paramLang)
caf2aaf4
K
249 html = ClientHtml.addManifestContentHash(html)
250 html = ClientHtml.addFaviconContentHash(html)
251 html = ClientHtml.addLogoContentHash(html)
9aac4423 252 html = ClientHtml.addCustomCSS(html)
a8b666e9 253 html = await ClientHtml.addAsyncPluginCSS(html)
9aac4423 254
a1587156 255 ClientHtml.htmlCache[path] = html
9aac4423
C
256
257 return html
e032aec9
C
258 }
259
7738273b 260 private static getIndexPath (req: express.Request, res: express.Response, paramLang: string) {
e032aec9
C
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, {
6dd9de95 269 secure: WEBSERVER.SCHEME === 'https',
bc90883f 270 sameSite: 'none',
e032aec9
C
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
cf649c2e
C
283 private static getEmbedPath () {
284 return join(__dirname, '../../../client/dist/standalone/videos/embed.html')
285 }
286
7738273b
RK
287 private static addHtmlLang (htmlStringPage: string, paramLang: string) {
288 return htmlStringPage.replace('<html>', `<html lang="${paramLang}">`)
289 }
290
caf2aaf4
K
291 private static addManifestContentHash (htmlStringPage: string) {
292 return htmlStringPage.replace('[manifestContentHash]', FILES_CONTENT_HASH.MANIFEST)
293 }
294
1fdf8edb 295 private static addFaviconContentHash (htmlStringPage: string) {
caf2aaf4
K
296 return htmlStringPage.replace('[faviconContentHash]', FILES_CONTENT_HASH.FAVICON)
297 }
298
1fdf8edb 299 private static addLogoContentHash (htmlStringPage: string) {
caf2aaf4
K
300 return htmlStringPage.replace('[logoContentHash]', FILES_CONTENT_HASH.LOGO)
301 }
302
9aac4423
C
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>`
e032aec9
C
308
309 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.TITLE, titleTag)
310 }
311
9aac4423
C
312 private static addDescriptionTag (htmlStringPage: string, description?: string) {
313 const content = description || CONFIG.INSTANCE.SHORT_DESCRIPTION
314 const descriptionTag = `<meta name="description" content="${content}" />`
e032aec9
C
315
316 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.DESCRIPTION, descriptionTag)
317 }
318
319 private static addCustomCSS (htmlStringPage: string) {
ffb321be 320 const styleTag = `<style class="custom-css-style">${CONFIG.INSTANCE.CUSTOMIZATIONS.CSS}</style>`
e032aec9
C
321
322 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.CUSTOM_CSS, styleTag)
323 }
324
a8b666e9
C
325 private static async addAsyncPluginCSS (htmlStringPage: string) {
326 const globalCSSContent = await readFile(PLUGIN_GLOBAL_CSS_PATH)
3e753302 327 if (globalCSSContent.byteLength === 0) return htmlStringPage
a8b666e9
C
328
329 const fileHash = sha256(globalCSSContent)
330 const linkTag = `<link rel="stylesheet" href="/plugins/global.css?hash=${fileHash}" />`
ffb321be
C
331
332 return htmlStringPage.replace('</head>', linkTag + '</head>')
333 }
334
6fad8e51 335 private static generateOpenGraphMetaTags (tags: Tags) {
8d987ec6
K
336 const metaTags = {
337 'og:type': tags.ogType,
865af3fd 338 'og:site_name': tags.siteName,
8d987ec6
K
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 }
e032aec9 347
8d987ec6
K
348 metaTags['og:url'] = tags.url
349 metaTags['og:description'] = tags.description
e032aec9 350
8d987ec6
K
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 }
e032aec9 358
8d987ec6
K
359 return metaTags
360 }
e032aec9 361
6fad8e51 362 private static generateStandardMetaTags (tags: Tags) {
8d987ec6
K
363 return {
364 name: tags.title,
365 description: tags.description,
366 image: tags.image.url
367 }
368 }
e032aec9 369
6fad8e51 370 private static generateTwitterCardMetaTags (tags: Tags) {
8d987ec6
K
371 const metaTags = {
372 'twitter:card': tags.twitterCard,
e032aec9 373 'twitter:site': CONFIG.SERVICES.TWITTER.USERNAME,
8d987ec6
K
374 'twitter:title': tags.title,
375 'twitter:description': tags.description,
376 'twitter:image': tags.image.url
e032aec9
C
377 }
378
8d987ec6
K
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
b96777c3
C
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
8d987ec6
K
390 return metaTags
391 }
e032aec9 392
6fad8e51 393 private static generateSchemaTags (tags: Tags) {
8d987ec6 394 const schema = {
e032aec9 395 '@context': 'http://schema.org',
8d987ec6
K
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
6fad8e51
C
411
412 if (tags.embed.duration) schema['duration'] = tags.embed.duration
413 if (tags.embed.views) schema['iterationCount'] = tags.embed.views
414
8d987ec6
K
415 schema['thumbnailUrl'] = tags.image.url
416 schema['contentUrl'] = tags.url
417 }
418
419 return schema
420 }
421
6fad8e51 422 private static addTags (htmlStringPage: string, tagsValues: Tags) {
8d987ec6
K
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
106fa224 428 const { url, title, embed, originUrl } = tagsValues
8d987ec6 429
6fad8e51 430 const oembedLinkTags: { type: string, href: string, title: string }[] = []
8d987ec6
K
431
432 if (embed) {
433 oembedLinkTags.push({
434 type: 'application/json+oembed',
435 href: WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(url),
436 title
437 })
e032aec9
C
438 }
439
440 let tagsString = ''
441
442 // Opengraph
443 Object.keys(openGraphMetaTags).forEach(tagName => {
a1587156 444 const tagValue = openGraphMetaTags[tagName]
e032aec9
C
445
446 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
447 })
448
8d987ec6
K
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
e032aec9
C
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
8d987ec6
K
469 if (schemaTags) {
470 tagsString += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>`
471 }
92bf2f62 472
8d987ec6 473 // SEO, use origin URL
106fa224 474 tagsString += `<link rel="canonical" href="${originUrl}" />`
92bf2f62 475
8d987ec6 476 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.META_TAGS, tagsString)
e032aec9
C
477 }
478}