1 import express from 'express'
2 import { readFile } from 'fs-extra'
3 import { join } from 'path'
4 import validator from 'validator'
5 import { toCompleteUUID } from '@server/helpers/custom-validators/misc'
6 import { escapeHTML } from '@shared/core-utils/renderer'
7 import { HTMLServerConfig } from '@shared/models'
8 import { buildFileLocale, getDefaultLocale, is18nLocale, POSSIBLE_LOCALES } from '../../shared/core-utils/i18n/i18n'
9 import { HttpStatusCode } from '../../shared/models/http/http-error-codes'
10 import { VideoPlaylistPrivacy, VideoPrivacy } from '../../shared/models/videos'
11 import { isTestInstance, sha256 } from '../helpers/core-utils'
12 import { logger } from '../helpers/logger'
13 import { mdToPlainText } from '../helpers/markdown'
14 import { CONFIG } from '../initializers/config'
18 CUSTOM_HTML_TAG_COMMENTS,
21 PLUGIN_GLOBAL_CSS_PATH,
23 } from '../initializers/constants'
24 import { AccountModel } from '../models/account/account'
25 import { getActivityStreamDuration } from '../models/video/formatter/video-format-utils'
26 import { VideoModel } from '../models/video/video'
27 import { VideoChannelModel } from '../models/video/video-channel'
28 import { VideoPlaylistModel } from '../models/video/video-playlist'
29 import { MAccountActor, MChannelActor } from '../types/models'
30 import { ServerConfigManager } from './server-config-manager'
34 twitterCard: 'player' | 'summary' | 'summary_large_image'
41 escapedSiteName: string
43 escapedDescription: string
48 disallowIndexation?: boolean
66 private static htmlCache: { [path: string]: string } = {}
68 static invalidCache () {
69 logger.info('Cleaning HTML cache.')
71 ClientHtml.htmlCache = {}
74 static async getDefaultHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
75 const html = paramLang
76 ? await ClientHtml.getIndexHTML(req, res, paramLang)
77 : await ClientHtml.getIndexHTML(req, res)
79 let customHtml = ClientHtml.addTitleTag(html)
80 customHtml = ClientHtml.addDescriptionTag(customHtml)
85 static async getWatchHTMLPage (videoIdArg: string, req: express.Request, res: express.Response) {
86 const videoId = toCompleteUUID(videoIdArg)
88 // Let Angular application handle errors
89 if (!validator.isInt(videoId) && !validator.isUUID(videoId, 4)) {
90 res.status(HttpStatusCode.NOT_FOUND_404)
91 return ClientHtml.getIndexHTML(req, res)
94 const [ html, video ] = await Promise.all([
95 ClientHtml.getIndexHTML(req, res),
96 VideoModel.loadWithBlacklist(videoId)
99 // Let Angular application handle errors
100 if (!video || video.privacy === VideoPrivacy.PRIVATE || video.privacy === VideoPrivacy.INTERNAL || video.VideoBlacklist) {
101 res.status(HttpStatusCode.NOT_FOUND_404)
104 const description = mdToPlainText(video.description)
106 let customHtml = ClientHtml.addTitleTag(html, video.name)
107 customHtml = ClientHtml.addDescriptionTag(customHtml, description)
109 const url = WEBSERVER.URL + video.getWatchStaticPath()
110 const originUrl = video.url
111 const title = video.name
112 const siteName = CONFIG.INSTANCE.NAME
115 url: WEBSERVER.URL + video.getPreviewStaticPath()
119 url: WEBSERVER.URL + video.getEmbedStaticPath(),
120 createdAt: video.createdAt.toISOString(),
121 duration: getActivityStreamDuration(video.duration),
125 const ogType = 'video'
126 const twitterCard = CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary_large_image'
127 const schemaType = 'VideoObject'
129 customHtml = ClientHtml.addTags(customHtml, {
132 escapedSiteName: escapeHTML(siteName),
133 escapedTitle: escapeHTML(title),
134 escapedDescription: escapeHTML(description),
145 static async getWatchPlaylistHTMLPage (videoPlaylistIdArg: string, req: express.Request, res: express.Response) {
146 const videoPlaylistId = toCompleteUUID(videoPlaylistIdArg)
148 // Let Angular application handle errors
149 if (!validator.isInt(videoPlaylistId) && !validator.isUUID(videoPlaylistId, 4)) {
150 res.status(HttpStatusCode.NOT_FOUND_404)
151 return ClientHtml.getIndexHTML(req, res)
154 const [ html, videoPlaylist ] = await Promise.all([
155 ClientHtml.getIndexHTML(req, res),
156 VideoPlaylistModel.loadWithAccountAndChannel(videoPlaylistId, null)
159 // Let Angular application handle errors
160 if (!videoPlaylist || videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) {
161 res.status(HttpStatusCode.NOT_FOUND_404)
165 const description = mdToPlainText(videoPlaylist.description)
167 let customHtml = ClientHtml.addTitleTag(html, videoPlaylist.name)
168 customHtml = ClientHtml.addDescriptionTag(customHtml, description)
170 const url = WEBSERVER.URL + videoPlaylist.getWatchStaticPath()
171 const originUrl = videoPlaylist.url
172 const title = videoPlaylist.name
173 const siteName = CONFIG.INSTANCE.NAME
176 url: videoPlaylist.getThumbnailUrl()
180 url: WEBSERVER.URL + videoPlaylist.getEmbedStaticPath(),
181 createdAt: videoPlaylist.createdAt.toISOString()
185 numberOfItems: videoPlaylist.get('videosLength') as number
188 const ogType = 'video'
189 const twitterCard = CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary'
190 const schemaType = 'ItemList'
192 customHtml = ClientHtml.addTags(customHtml, {
195 escapedSiteName: escapeHTML(siteName),
196 escapedTitle: escapeHTML(title),
197 escapedDescription: escapeHTML(description),
209 static async getAccountHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
210 const accountModelPromise = AccountModel.loadByNameWithHost(nameWithHost)
211 return this.getAccountOrChannelHTMLPage(() => accountModelPromise, req, res)
214 static async getVideoChannelHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
215 const videoChannelModelPromise = VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithHost)
216 return this.getAccountOrChannelHTMLPage(() => videoChannelModelPromise, req, res)
219 static async getActorHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
220 const [ account, channel ] = await Promise.all([
221 AccountModel.loadByNameWithHost(nameWithHost),
222 VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithHost)
225 return this.getAccountOrChannelHTMLPage(() => Promise.resolve(account || channel), req, res)
228 static async getEmbedHTML () {
229 const path = ClientHtml.getEmbedPath()
231 if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path]
233 const buffer = await readFile(path)
234 const serverConfig = await ServerConfigManager.Instance.getHTMLServerConfig()
236 let html = buffer.toString()
237 html = await ClientHtml.addAsyncPluginCSS(html)
238 html = ClientHtml.addCustomCSS(html)
239 html = ClientHtml.addTitleTag(html)
240 html = ClientHtml.addDescriptionTag(html)
241 html = ClientHtml.addServerConfig(html, serverConfig)
243 ClientHtml.htmlCache[path] = html
248 private static async getAccountOrChannelHTMLPage (
249 loader: () => Promise<MAccountActor | MChannelActor>,
250 req: express.Request,
251 res: express.Response
253 const [ html, entity ] = await Promise.all([
254 ClientHtml.getIndexHTML(req, res),
258 // Let Angular application handle errors
260 res.status(HttpStatusCode.NOT_FOUND_404)
261 return ClientHtml.getIndexHTML(req, res)
264 const description = mdToPlainText(entity.description)
266 let customHtml = ClientHtml.addTitleTag(html, entity.getDisplayName())
267 customHtml = ClientHtml.addDescriptionTag(customHtml, description)
269 const url = entity.getLocalUrl()
270 const originUrl = entity.Actor.url
271 const siteName = CONFIG.INSTANCE.NAME
272 const title = entity.getDisplayName()
275 url: entity.Actor.getAvatarUrl(),
276 width: ACTOR_IMAGES_SIZE.AVATARS.width,
277 height: ACTOR_IMAGES_SIZE.AVATARS.height
280 const ogType = 'website'
281 const twitterCard = 'summary'
282 const schemaType = 'ProfilePage'
284 customHtml = ClientHtml.addTags(customHtml, {
287 escapedTitle: escapeHTML(title),
288 escapedSiteName: escapeHTML(siteName),
289 escapedDescription: escapeHTML(description),
294 disallowIndexation: !entity.Actor.isOwned()
300 private static async getIndexHTML (req: express.Request, res: express.Response, paramLang?: string) {
301 const path = ClientHtml.getIndexPath(req, res, paramLang)
302 if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path]
304 const buffer = await readFile(path)
305 const serverConfig = await ServerConfigManager.Instance.getHTMLServerConfig()
307 let html = buffer.toString()
309 html = ClientHtml.addManifestContentHash(html)
310 html = ClientHtml.addFaviconContentHash(html)
311 html = ClientHtml.addLogoContentHash(html)
312 html = ClientHtml.addCustomCSS(html)
313 html = ClientHtml.addServerConfig(html, serverConfig)
314 html = await ClientHtml.addAsyncPluginCSS(html)
316 ClientHtml.htmlCache[path] = html
321 private static getIndexPath (req: express.Request, res: express.Response, paramLang: string) {
324 // Check param lang validity
325 if (paramLang && is18nLocale(paramLang)) {
328 // Save locale in cookies
329 res.cookie('clientLanguage', lang, {
330 secure: WEBSERVER.SCHEME === 'https',
332 maxAge: 1000 * 3600 * 24 * 90 // 3 months
335 } else if (req.cookies.clientLanguage && is18nLocale(req.cookies.clientLanguage)) {
336 lang = req.cookies.clientLanguage
338 lang = req.acceptsLanguages(POSSIBLE_LOCALES) || getDefaultLocale()
342 'Serving %s HTML language', buildFileLocale(lang),
343 { cookie: req.cookies?.clientLanguage, paramLang, acceptLanguage: req.headers['accept-language'] }
346 return join(__dirname, '../../../client/dist/' + buildFileLocale(lang) + '/index.html')
349 private static getEmbedPath () {
350 return join(__dirname, '../../../client/dist/standalone/videos/embed.html')
353 private static addManifestContentHash (htmlStringPage: string) {
354 return htmlStringPage.replace('[manifestContentHash]', FILES_CONTENT_HASH.MANIFEST)
357 private static addFaviconContentHash (htmlStringPage: string) {
358 return htmlStringPage.replace('[faviconContentHash]', FILES_CONTENT_HASH.FAVICON)
361 private static addLogoContentHash (htmlStringPage: string) {
362 return htmlStringPage.replace('[logoContentHash]', FILES_CONTENT_HASH.LOGO)
365 private static addTitleTag (htmlStringPage: string, title?: string) {
366 let text = title || CONFIG.INSTANCE.NAME
367 if (title) text += ` - ${CONFIG.INSTANCE.NAME}`
369 const titleTag = `<title>${escapeHTML(text)}</title>`
371 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.TITLE, titleTag)
374 private static addDescriptionTag (htmlStringPage: string, description?: string) {
375 const content = description || CONFIG.INSTANCE.SHORT_DESCRIPTION
376 const descriptionTag = `<meta name="description" content="${escapeHTML(content)}" />`
378 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.DESCRIPTION, descriptionTag)
381 private static addCustomCSS (htmlStringPage: string) {
382 const styleTag = `<style class="custom-css-style">${CONFIG.INSTANCE.CUSTOMIZATIONS.CSS}</style>`
384 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.CUSTOM_CSS, styleTag)
387 private static addServerConfig (htmlStringPage: string, serverConfig: HTMLServerConfig) {
388 // Stringify the JSON object, and then stringify the string object so we can inject it into the HTML
389 const serverConfigString = JSON.stringify(JSON.stringify(serverConfig))
390 const configScriptTag = `<script type="application/javascript">window.PeerTubeServerConfig = ${serverConfigString}</script>`
392 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.SERVER_CONFIG, configScriptTag)
395 private static async addAsyncPluginCSS (htmlStringPage: string) {
396 const globalCSSContent = await readFile(PLUGIN_GLOBAL_CSS_PATH)
397 if (globalCSSContent.byteLength === 0) return htmlStringPage
399 const fileHash = sha256(globalCSSContent)
400 const linkTag = `<link rel="stylesheet" href="/plugins/global.css?hash=${fileHash}" />`
402 return htmlStringPage.replace('</head>', linkTag + '</head>')
405 private static generateOpenGraphMetaTags (tags: Tags) {
407 'og:type': tags.ogType,
408 'og:site_name': tags.escapedSiteName,
409 'og:title': tags.escapedTitle,
410 'og:image': tags.image.url
413 if (tags.image.width && tags.image.height) {
414 metaTags['og:image:width'] = tags.image.width
415 metaTags['og:image:height'] = tags.image.height
418 metaTags['og:url'] = tags.url
419 metaTags['og:description'] = tags.escapedDescription
422 metaTags['og:video:url'] = tags.embed.url
423 metaTags['og:video:secure_url'] = tags.embed.url
424 metaTags['og:video:type'] = 'text/html'
425 metaTags['og:video:width'] = EMBED_SIZE.width
426 metaTags['og:video:height'] = EMBED_SIZE.height
432 private static generateStandardMetaTags (tags: Tags) {
434 name: tags.escapedTitle,
435 description: tags.escapedDescription,
436 image: tags.image.url
440 private static generateTwitterCardMetaTags (tags: Tags) {
442 'twitter:card': tags.twitterCard,
443 'twitter:site': CONFIG.SERVICES.TWITTER.USERNAME,
444 'twitter:title': tags.escapedTitle,
445 'twitter:description': tags.escapedDescription,
446 'twitter:image': tags.image.url
449 if (tags.image.width && tags.image.height) {
450 metaTags['twitter:image:width'] = tags.image.width
451 metaTags['twitter:image:height'] = tags.image.height
454 if (tags.twitterCard === 'player') {
455 metaTags['twitter:player'] = tags.embed.url
456 metaTags['twitter:player:width'] = EMBED_SIZE.width
457 metaTags['twitter:player:height'] = EMBED_SIZE.height
463 private static generateSchemaTags (tags: Tags) {
465 '@context': 'http://schema.org',
466 '@type': tags.schemaType,
467 'name': tags.escapedTitle,
468 'description': tags.escapedDescription,
469 'image': tags.image.url,
474 schema['numberOfItems'] = tags.list.numberOfItems
475 schema['thumbnailUrl'] = tags.image.url
479 schema['embedUrl'] = tags.embed.url
480 schema['uploadDate'] = tags.embed.createdAt
482 if (tags.embed.duration) schema['duration'] = tags.embed.duration
483 if (tags.embed.views) schema['iterationCount'] = tags.embed.views
485 schema['thumbnailUrl'] = tags.image.url
486 schema['contentUrl'] = tags.url
492 private static addTags (htmlStringPage: string, tagsValues: Tags) {
493 const openGraphMetaTags = this.generateOpenGraphMetaTags(tagsValues)
494 const standardMetaTags = this.generateStandardMetaTags(tagsValues)
495 const twitterCardMetaTags = this.generateTwitterCardMetaTags(tagsValues)
496 const schemaTags = this.generateSchemaTags(tagsValues)
498 const { url, escapedTitle, embed, originUrl, disallowIndexation } = tagsValues
500 const oembedLinkTags: { type: string, href: string, escapedTitle: string }[] = []
503 oembedLinkTags.push({
504 type: 'application/json+oembed',
505 href: WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(url),
513 Object.keys(openGraphMetaTags).forEach(tagName => {
514 const tagValue = openGraphMetaTags[tagName]
516 tagsStr += `<meta property="${tagName}" content="${tagValue}" />`
520 Object.keys(standardMetaTags).forEach(tagName => {
521 const tagValue = standardMetaTags[tagName]
523 tagsStr += `<meta property="${tagName}" content="${tagValue}" />`
527 Object.keys(twitterCardMetaTags).forEach(tagName => {
528 const tagValue = twitterCardMetaTags[tagName]
530 tagsStr += `<meta property="${tagName}" content="${tagValue}" />`
534 for (const oembedLinkTag of oembedLinkTags) {
535 tagsStr += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.escapedTitle}" />`
540 tagsStr += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>`
543 // SEO, use origin URL
544 tagsStr += `<link rel="canonical" href="${originUrl}" />`
546 if (disallowIndexation) {
547 tagsStr += `<meta name="robots" content="noindex" />`
550 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.META_TAGS, tagsStr)
554 function sendHTML (html: string, res: express.Response, localizedHTML: boolean = false) {
555 res.set('Content-Type', 'text/html; charset=UTF-8')
558 res.set('Vary', 'Accept-Language')
561 return res.send(html)
564 async function serveIndexHTML (req: express.Request, res: express.Response) {
565 if (req.accepts(ACCEPT_HEADERS) === 'html' || !req.headers.accept) {
567 await generateHTMLPage(req, res, req.params.language)
570 logger.error('Cannot generate HTML page.', err)
571 return res.status(HttpStatusCode.INTERNAL_SERVER_ERROR_500).end()
575 return res.status(HttpStatusCode.NOT_ACCEPTABLE_406).end()
578 // ---------------------------------------------------------------------------
586 async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
587 const html = await ClientHtml.getDefaultHTMLPage(req, res, paramLang)
589 return sendHTML(html, res, true)