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 { mdToOneLinePlainText } from '@server/helpers/markdown'
7 import { ActorImageModel } from '@server/models/actor/actor-image'
8 import { root } from '@shared/core-utils'
9 import { escapeHTML } from '@shared/core-utils/renderer'
10 import { sha256 } from '@shared/extra-utils'
11 import { HTMLServerConfig } from '@shared/models'
12 import { buildFileLocale, getDefaultLocale, is18nLocale, POSSIBLE_LOCALES } from '../../shared/core-utils/i18n/i18n'
13 import { HttpStatusCode } from '../../shared/models/http/http-error-codes'
14 import { VideoPlaylistPrivacy, VideoPrivacy } from '../../shared/models/videos'
15 import { logger } from '../helpers/logger'
16 import { CONFIG } from '../initializers/config'
19 CUSTOM_HTML_TAG_COMMENTS,
22 PLUGIN_GLOBAL_CSS_PATH,
24 } from '../initializers/constants'
25 import { AccountModel } from '../models/account/account'
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 { getActivityStreamDuration } from './activitypub/activity'
31 import { getBiggestActorImage } from './actor-image'
32 import { ServerConfigManager } from './server-config-manager'
36 twitterCard: 'player' | 'summary' | 'summary_large_image'
43 escapedSiteName: string
45 escapedDescription: string
50 disallowIndexation?: boolean
68 private static htmlCache: { [path: string]: string } = {}
70 static invalidCache () {
71 logger.info('Cleaning HTML cache.')
73 ClientHtml.htmlCache = {}
76 static async getDefaultHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
77 const html = paramLang
78 ? await ClientHtml.getIndexHTML(req, res, paramLang)
79 : await ClientHtml.getIndexHTML(req, res)
81 let customHtml = ClientHtml.addTitleTag(html)
82 customHtml = ClientHtml.addDescriptionTag(customHtml)
87 static async getWatchHTMLPage (videoIdArg: string, req: express.Request, res: express.Response) {
88 const videoId = toCompleteUUID(videoIdArg)
90 // Let Angular application handle errors
91 if (!validator.isInt(videoId) && !validator.isUUID(videoId, 4)) {
92 res.status(HttpStatusCode.NOT_FOUND_404)
93 return ClientHtml.getIndexHTML(req, res)
96 const [ html, video ] = await Promise.all([
97 ClientHtml.getIndexHTML(req, res),
98 VideoModel.loadWithBlacklist(videoId)
101 // Let Angular application handle errors
102 if (!video || video.privacy === VideoPrivacy.PRIVATE || video.privacy === VideoPrivacy.INTERNAL || video.VideoBlacklist) {
103 res.status(HttpStatusCode.NOT_FOUND_404)
106 const description = mdToOneLinePlainText(video.description)
108 let customHtml = ClientHtml.addTitleTag(html, video.name)
109 customHtml = ClientHtml.addDescriptionTag(customHtml, description)
111 const url = WEBSERVER.URL + video.getWatchStaticPath()
112 const originUrl = video.url
113 const title = video.name
114 const siteName = CONFIG.INSTANCE.NAME
117 url: WEBSERVER.URL + video.getPreviewStaticPath()
121 url: WEBSERVER.URL + video.getEmbedStaticPath(),
122 createdAt: video.createdAt.toISOString(),
123 duration: getActivityStreamDuration(video.duration),
127 const ogType = 'video'
128 const twitterCard = CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary_large_image'
129 const schemaType = 'VideoObject'
131 customHtml = ClientHtml.addTags(customHtml, {
134 escapedSiteName: escapeHTML(siteName),
135 escapedTitle: escapeHTML(title),
136 escapedDescription: escapeHTML(description),
137 disallowIndexation: video.privacy !== VideoPrivacy.PUBLIC,
148 static async getWatchPlaylistHTMLPage (videoPlaylistIdArg: string, req: express.Request, res: express.Response) {
149 const videoPlaylistId = toCompleteUUID(videoPlaylistIdArg)
151 // Let Angular application handle errors
152 if (!validator.isInt(videoPlaylistId) && !validator.isUUID(videoPlaylistId, 4)) {
153 res.status(HttpStatusCode.NOT_FOUND_404)
154 return ClientHtml.getIndexHTML(req, res)
157 const [ html, videoPlaylist ] = await Promise.all([
158 ClientHtml.getIndexHTML(req, res),
159 VideoPlaylistModel.loadWithAccountAndChannel(videoPlaylistId, null)
162 // Let Angular application handle errors
163 if (!videoPlaylist || videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) {
164 res.status(HttpStatusCode.NOT_FOUND_404)
168 const description = mdToOneLinePlainText(videoPlaylist.description)
170 let customHtml = ClientHtml.addTitleTag(html, videoPlaylist.name)
171 customHtml = ClientHtml.addDescriptionTag(customHtml, description)
173 const url = WEBSERVER.URL + videoPlaylist.getWatchStaticPath()
174 const originUrl = videoPlaylist.url
175 const title = videoPlaylist.name
176 const siteName = CONFIG.INSTANCE.NAME
179 url: videoPlaylist.getThumbnailUrl()
183 url: WEBSERVER.URL + videoPlaylist.getEmbedStaticPath(),
184 createdAt: videoPlaylist.createdAt.toISOString()
188 numberOfItems: videoPlaylist.get('videosLength') as number
191 const ogType = 'video'
192 const twitterCard = CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary'
193 const schemaType = 'ItemList'
195 customHtml = ClientHtml.addTags(customHtml, {
198 escapedSiteName: escapeHTML(siteName),
199 escapedTitle: escapeHTML(title),
200 escapedDescription: escapeHTML(description),
201 disallowIndexation: videoPlaylist.privacy !== VideoPlaylistPrivacy.PUBLIC,
213 static async getAccountHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
214 const accountModelPromise = AccountModel.loadByNameWithHost(nameWithHost)
215 return this.getAccountOrChannelHTMLPage(() => accountModelPromise, req, res)
218 static async getVideoChannelHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
219 const videoChannelModelPromise = VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithHost)
220 return this.getAccountOrChannelHTMLPage(() => videoChannelModelPromise, req, res)
223 static async getActorHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
224 const [ account, channel ] = await Promise.all([
225 AccountModel.loadByNameWithHost(nameWithHost),
226 VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithHost)
229 return this.getAccountOrChannelHTMLPage(() => Promise.resolve(account || channel), req, res)
232 static async getEmbedHTML () {
233 const path = ClientHtml.getEmbedPath()
235 if (ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path]
237 const buffer = await readFile(path)
238 const serverConfig = await ServerConfigManager.Instance.getHTMLServerConfig()
240 let html = buffer.toString()
241 html = await ClientHtml.addAsyncPluginCSS(html)
242 html = ClientHtml.addCustomCSS(html)
243 html = ClientHtml.addTitleTag(html)
244 html = ClientHtml.addDescriptionTag(html)
245 html = ClientHtml.addServerConfig(html, serverConfig)
247 ClientHtml.htmlCache[path] = html
252 private static async getAccountOrChannelHTMLPage (
253 loader: () => Promise<MAccountActor | MChannelActor>,
254 req: express.Request,
255 res: express.Response
257 const [ html, entity ] = await Promise.all([
258 ClientHtml.getIndexHTML(req, res),
262 // Let Angular application handle errors
264 res.status(HttpStatusCode.NOT_FOUND_404)
265 return ClientHtml.getIndexHTML(req, res)
268 const description = mdToOneLinePlainText(entity.description)
270 let customHtml = ClientHtml.addTitleTag(html, entity.getDisplayName())
271 customHtml = ClientHtml.addDescriptionTag(customHtml, description)
273 const url = entity.getLocalUrl()
274 const originUrl = entity.Actor.url
275 const siteName = CONFIG.INSTANCE.NAME
276 const title = entity.getDisplayName()
278 const avatar = getBiggestActorImage(entity.Actor.Avatars)
280 url: ActorImageModel.getImageUrl(avatar),
281 width: avatar?.width,
282 height: avatar?.height
285 const ogType = 'website'
286 const twitterCard = 'summary'
287 const schemaType = 'ProfilePage'
289 customHtml = ClientHtml.addTags(customHtml, {
292 escapedTitle: escapeHTML(title),
293 escapedSiteName: escapeHTML(siteName),
294 escapedDescription: escapeHTML(description),
299 disallowIndexation: !entity.Actor.isOwned()
305 private static async getIndexHTML (req: express.Request, res: express.Response, paramLang?: string) {
306 const path = ClientHtml.getIndexPath(req, res, paramLang)
307 if (ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path]
309 const buffer = await readFile(path)
310 const serverConfig = await ServerConfigManager.Instance.getHTMLServerConfig()
312 let html = buffer.toString()
314 html = ClientHtml.addManifestContentHash(html)
315 html = ClientHtml.addFaviconContentHash(html)
316 html = ClientHtml.addLogoContentHash(html)
317 html = ClientHtml.addCustomCSS(html)
318 html = ClientHtml.addServerConfig(html, serverConfig)
319 html = await ClientHtml.addAsyncPluginCSS(html)
321 ClientHtml.htmlCache[path] = html
326 private static getIndexPath (req: express.Request, res: express.Response, paramLang: string) {
329 // Check param lang validity
330 if (paramLang && is18nLocale(paramLang)) {
333 // Save locale in cookies
334 res.cookie('clientLanguage', lang, {
335 secure: WEBSERVER.SCHEME === 'https',
337 maxAge: 1000 * 3600 * 24 * 90 // 3 months
340 } else if (req.cookies.clientLanguage && is18nLocale(req.cookies.clientLanguage)) {
341 lang = req.cookies.clientLanguage
343 lang = req.acceptsLanguages(POSSIBLE_LOCALES) || getDefaultLocale()
347 'Serving %s HTML language', buildFileLocale(lang),
348 { cookie: req.cookies?.clientLanguage, paramLang, acceptLanguage: req.headers['accept-language'] }
351 return join(root(), 'client', 'dist', buildFileLocale(lang), 'index.html')
354 private static getEmbedPath () {
355 return join(root(), 'client', 'dist', 'standalone', 'videos', 'embed.html')
358 private static addManifestContentHash (htmlStringPage: string) {
359 return htmlStringPage.replace('[manifestContentHash]', FILES_CONTENT_HASH.MANIFEST)
362 private static addFaviconContentHash (htmlStringPage: string) {
363 return htmlStringPage.replace('[faviconContentHash]', FILES_CONTENT_HASH.FAVICON)
366 private static addLogoContentHash (htmlStringPage: string) {
367 return htmlStringPage.replace('[logoContentHash]', FILES_CONTENT_HASH.LOGO)
370 private static addTitleTag (htmlStringPage: string, title?: string) {
371 let text = title || CONFIG.INSTANCE.NAME
372 if (title) text += ` - ${CONFIG.INSTANCE.NAME}`
374 const titleTag = `<title>${escapeHTML(text)}</title>`
376 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.TITLE, titleTag)
379 private static addDescriptionTag (htmlStringPage: string, description?: string) {
380 const content = description || CONFIG.INSTANCE.SHORT_DESCRIPTION
381 const descriptionTag = `<meta name="description" content="${escapeHTML(content)}" />`
383 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.DESCRIPTION, descriptionTag)
386 private static addCustomCSS (htmlStringPage: string) {
387 const styleTag = `<style class="custom-css-style">${CONFIG.INSTANCE.CUSTOMIZATIONS.CSS}</style>`
389 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.CUSTOM_CSS, styleTag)
392 private static addServerConfig (htmlStringPage: string, serverConfig: HTMLServerConfig) {
393 // Stringify the JSON object, and then stringify the string object so we can inject it into the HTML
394 const serverConfigString = JSON.stringify(JSON.stringify(serverConfig))
395 const configScriptTag = `<script type="application/javascript">window.PeerTubeServerConfig = ${serverConfigString}</script>`
397 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.SERVER_CONFIG, configScriptTag)
400 private static async addAsyncPluginCSS (htmlStringPage: string) {
401 const globalCSSContent = await readFile(PLUGIN_GLOBAL_CSS_PATH)
402 if (globalCSSContent.byteLength === 0) return htmlStringPage
404 const fileHash = sha256(globalCSSContent)
405 const linkTag = `<link rel="stylesheet" href="/plugins/global.css?hash=${fileHash}" />`
407 return htmlStringPage.replace('</head>', linkTag + '</head>')
410 private static generateOpenGraphMetaTags (tags: Tags) {
412 'og:type': tags.ogType,
413 'og:site_name': tags.escapedSiteName,
414 'og:title': tags.escapedTitle,
415 'og:image': tags.image.url
418 if (tags.image.width && tags.image.height) {
419 metaTags['og:image:width'] = tags.image.width
420 metaTags['og:image:height'] = tags.image.height
423 metaTags['og:url'] = tags.url
424 metaTags['og:description'] = tags.escapedDescription
427 metaTags['og:video:url'] = tags.embed.url
428 metaTags['og:video:secure_url'] = tags.embed.url
429 metaTags['og:video:type'] = 'text/html'
430 metaTags['og:video:width'] = EMBED_SIZE.width
431 metaTags['og:video:height'] = EMBED_SIZE.height
437 private static generateStandardMetaTags (tags: Tags) {
439 name: tags.escapedTitle,
440 description: tags.escapedDescription,
441 image: tags.image.url
445 private static generateTwitterCardMetaTags (tags: Tags) {
447 'twitter:card': tags.twitterCard,
448 'twitter:site': CONFIG.SERVICES.TWITTER.USERNAME,
449 'twitter:title': tags.escapedTitle,
450 'twitter:description': tags.escapedDescription,
451 'twitter:image': tags.image.url
454 if (tags.image.width && tags.image.height) {
455 metaTags['twitter:image:width'] = tags.image.width
456 metaTags['twitter:image:height'] = tags.image.height
459 if (tags.twitterCard === 'player') {
460 metaTags['twitter:player'] = tags.embed.url
461 metaTags['twitter:player:width'] = EMBED_SIZE.width
462 metaTags['twitter:player:height'] = EMBED_SIZE.height
468 private static generateSchemaTags (tags: Tags) {
470 '@context': 'http://schema.org',
471 '@type': tags.schemaType,
472 'name': tags.escapedTitle,
473 'description': tags.escapedDescription,
474 'image': tags.image.url,
479 schema['numberOfItems'] = tags.list.numberOfItems
480 schema['thumbnailUrl'] = tags.image.url
484 schema['embedUrl'] = tags.embed.url
485 schema['uploadDate'] = tags.embed.createdAt
487 if (tags.embed.duration) schema['duration'] = tags.embed.duration
488 if (tags.embed.views) schema['iterationCount'] = tags.embed.views
490 schema['thumbnailUrl'] = tags.image.url
491 schema['contentUrl'] = tags.url
497 private static addTags (htmlStringPage: string, tagsValues: Tags) {
498 const openGraphMetaTags = this.generateOpenGraphMetaTags(tagsValues)
499 const standardMetaTags = this.generateStandardMetaTags(tagsValues)
500 const twitterCardMetaTags = this.generateTwitterCardMetaTags(tagsValues)
501 const schemaTags = this.generateSchemaTags(tagsValues)
503 const { url, escapedTitle, embed, originUrl, disallowIndexation } = tagsValues
505 const oembedLinkTags: { type: string, href: string, escapedTitle: string }[] = []
508 oembedLinkTags.push({
509 type: 'application/json+oembed',
510 href: WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(url),
518 Object.keys(openGraphMetaTags).forEach(tagName => {
519 const tagValue = openGraphMetaTags[tagName]
521 tagsStr += `<meta property="${tagName}" content="${tagValue}" />`
525 Object.keys(standardMetaTags).forEach(tagName => {
526 const tagValue = standardMetaTags[tagName]
528 tagsStr += `<meta property="${tagName}" content="${tagValue}" />`
532 Object.keys(twitterCardMetaTags).forEach(tagName => {
533 const tagValue = twitterCardMetaTags[tagName]
535 tagsStr += `<meta property="${tagName}" content="${tagValue}" />`
539 for (const oembedLinkTag of oembedLinkTags) {
540 tagsStr += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.escapedTitle}" />`
545 tagsStr += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>`
548 // SEO, use origin URL
549 tagsStr += `<link rel="canonical" href="${originUrl}" />`
551 if (disallowIndexation) {
552 tagsStr += `<meta name="robots" content="noindex" />`
555 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.META_TAGS, tagsStr)
559 function sendHTML (html: string, res: express.Response, localizedHTML: boolean = false) {
560 res.set('Content-Type', 'text/html; charset=UTF-8')
563 res.set('Vary', 'Accept-Language')
566 return res.send(html)
569 async function serveIndexHTML (req: express.Request, res: express.Response) {
570 if (req.accepts(ACCEPT_HEADERS) === 'html' || !req.headers.accept) {
572 await generateHTMLPage(req, res, req.params.language)
575 logger.error('Cannot generate HTML page.', err)
576 return res.status(HttpStatusCode.INTERNAL_SERVER_ERROR_500).end()
580 return res.status(HttpStatusCode.NOT_ACCEPTABLE_406).end()
583 // ---------------------------------------------------------------------------
591 async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
592 const html = await ClientHtml.getDefaultHTMLPage(req, res, paramLang)
594 return sendHTML(html, res, true)