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