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 { getActivityStreamDuration } from '../models/video/formatter/video-format-utils'
27 import { VideoModel } from '../models/video/video'
28 import { VideoChannelModel } from '../models/video/video-channel'
29 import { VideoPlaylistModel } from '../models/video/video-playlist'
30 import { MAccountActor, MChannelActor } from '../types/models'
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),
147 static async getWatchPlaylistHTMLPage (videoPlaylistIdArg: string, req: express.Request, res: express.Response) {
148 const videoPlaylistId = toCompleteUUID(videoPlaylistIdArg)
150 // Let Angular application handle errors
151 if (!validator.isInt(videoPlaylistId) && !validator.isUUID(videoPlaylistId, 4)) {
152 res.status(HttpStatusCode.NOT_FOUND_404)
153 return ClientHtml.getIndexHTML(req, res)
156 const [ html, videoPlaylist ] = await Promise.all([
157 ClientHtml.getIndexHTML(req, res),
158 VideoPlaylistModel.loadWithAccountAndChannel(videoPlaylistId, null)
161 // Let Angular application handle errors
162 if (!videoPlaylist || videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) {
163 res.status(HttpStatusCode.NOT_FOUND_404)
167 const description = mdToOneLinePlainText(videoPlaylist.description)
169 let customHtml = ClientHtml.addTitleTag(html, videoPlaylist.name)
170 customHtml = ClientHtml.addDescriptionTag(customHtml, description)
172 const url = WEBSERVER.URL + videoPlaylist.getWatchStaticPath()
173 const originUrl = videoPlaylist.url
174 const title = videoPlaylist.name
175 const siteName = CONFIG.INSTANCE.NAME
178 url: videoPlaylist.getThumbnailUrl()
182 url: WEBSERVER.URL + videoPlaylist.getEmbedStaticPath(),
183 createdAt: videoPlaylist.createdAt.toISOString()
187 numberOfItems: videoPlaylist.get('videosLength') as number
190 const ogType = 'video'
191 const twitterCard = CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary'
192 const schemaType = 'ItemList'
194 customHtml = ClientHtml.addTags(customHtml, {
197 escapedSiteName: escapeHTML(siteName),
198 escapedTitle: escapeHTML(title),
199 escapedDescription: escapeHTML(description),
211 static async getAccountHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
212 const accountModelPromise = AccountModel.loadByNameWithHost(nameWithHost)
213 return this.getAccountOrChannelHTMLPage(() => accountModelPromise, req, res)
216 static async getVideoChannelHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
217 const videoChannelModelPromise = VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithHost)
218 return this.getAccountOrChannelHTMLPage(() => videoChannelModelPromise, req, res)
221 static async getActorHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
222 const [ account, channel ] = await Promise.all([
223 AccountModel.loadByNameWithHost(nameWithHost),
224 VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithHost)
227 return this.getAccountOrChannelHTMLPage(() => Promise.resolve(account || channel), req, res)
230 static async getEmbedHTML () {
231 const path = ClientHtml.getEmbedPath()
233 if (ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path]
235 const buffer = await readFile(path)
236 const serverConfig = await ServerConfigManager.Instance.getHTMLServerConfig()
238 let html = buffer.toString()
239 html = await ClientHtml.addAsyncPluginCSS(html)
240 html = ClientHtml.addCustomCSS(html)
241 html = ClientHtml.addTitleTag(html)
242 html = ClientHtml.addDescriptionTag(html)
243 html = ClientHtml.addServerConfig(html, serverConfig)
245 ClientHtml.htmlCache[path] = html
250 private static async getAccountOrChannelHTMLPage (
251 loader: () => Promise<MAccountActor | MChannelActor>,
252 req: express.Request,
253 res: express.Response
255 const [ html, entity ] = await Promise.all([
256 ClientHtml.getIndexHTML(req, res),
260 // Let Angular application handle errors
262 res.status(HttpStatusCode.NOT_FOUND_404)
263 return ClientHtml.getIndexHTML(req, res)
266 const description = mdToOneLinePlainText(entity.description)
268 let customHtml = ClientHtml.addTitleTag(html, entity.getDisplayName())
269 customHtml = ClientHtml.addDescriptionTag(customHtml, description)
271 const url = entity.getLocalUrl()
272 const originUrl = entity.Actor.url
273 const siteName = CONFIG.INSTANCE.NAME
274 const title = entity.getDisplayName()
276 const avatar = getBiggestActorImage(entity.Actor.Avatars)
278 url: ActorImageModel.getImageUrl(avatar),
279 width: avatar?.width,
280 height: avatar?.height
283 const ogType = 'website'
284 const twitterCard = 'summary'
285 const schemaType = 'ProfilePage'
287 customHtml = ClientHtml.addTags(customHtml, {
290 escapedTitle: escapeHTML(title),
291 escapedSiteName: escapeHTML(siteName),
292 escapedDescription: escapeHTML(description),
297 disallowIndexation: !entity.Actor.isOwned()
303 private static async getIndexHTML (req: express.Request, res: express.Response, paramLang?: string) {
304 const path = ClientHtml.getIndexPath(req, res, paramLang)
305 if (ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path]
307 const buffer = await readFile(path)
308 const serverConfig = await ServerConfigManager.Instance.getHTMLServerConfig()
310 let html = buffer.toString()
312 html = ClientHtml.addManifestContentHash(html)
313 html = ClientHtml.addFaviconContentHash(html)
314 html = ClientHtml.addLogoContentHash(html)
315 html = ClientHtml.addCustomCSS(html)
316 html = ClientHtml.addServerConfig(html, serverConfig)
317 html = await ClientHtml.addAsyncPluginCSS(html)
319 ClientHtml.htmlCache[path] = html
324 private static getIndexPath (req: express.Request, res: express.Response, paramLang: string) {
327 // Check param lang validity
328 if (paramLang && is18nLocale(paramLang)) {
331 // Save locale in cookies
332 res.cookie('clientLanguage', lang, {
333 secure: WEBSERVER.SCHEME === 'https',
335 maxAge: 1000 * 3600 * 24 * 90 // 3 months
338 } else if (req.cookies.clientLanguage && is18nLocale(req.cookies.clientLanguage)) {
339 lang = req.cookies.clientLanguage
341 lang = req.acceptsLanguages(POSSIBLE_LOCALES) || getDefaultLocale()
345 'Serving %s HTML language', buildFileLocale(lang),
346 { cookie: req.cookies?.clientLanguage, paramLang, acceptLanguage: req.headers['accept-language'] }
349 return join(root(), 'client', 'dist', buildFileLocale(lang), 'index.html')
352 private static getEmbedPath () {
353 return join(root(), 'client', 'dist', 'standalone', 'videos', 'embed.html')
356 private static addManifestContentHash (htmlStringPage: string) {
357 return htmlStringPage.replace('[manifestContentHash]', FILES_CONTENT_HASH.MANIFEST)
360 private static addFaviconContentHash (htmlStringPage: string) {
361 return htmlStringPage.replace('[faviconContentHash]', FILES_CONTENT_HASH.FAVICON)
364 private static addLogoContentHash (htmlStringPage: string) {
365 return htmlStringPage.replace('[logoContentHash]', FILES_CONTENT_HASH.LOGO)
368 private static addTitleTag (htmlStringPage: string, title?: string) {
369 let text = title || CONFIG.INSTANCE.NAME
370 if (title) text += ` - ${CONFIG.INSTANCE.NAME}`
372 const titleTag = `<title>${escapeHTML(text)}</title>`
374 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.TITLE, titleTag)
377 private static addDescriptionTag (htmlStringPage: string, description?: string) {
378 const content = description || CONFIG.INSTANCE.SHORT_DESCRIPTION
379 const descriptionTag = `<meta name="description" content="${escapeHTML(content)}" />`
381 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.DESCRIPTION, descriptionTag)
384 private static addCustomCSS (htmlStringPage: string) {
385 const styleTag = `<style class="custom-css-style">${CONFIG.INSTANCE.CUSTOMIZATIONS.CSS}</style>`
387 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.CUSTOM_CSS, styleTag)
390 private static addServerConfig (htmlStringPage: string, serverConfig: HTMLServerConfig) {
391 // Stringify the JSON object, and then stringify the string object so we can inject it into the HTML
392 const serverConfigString = JSON.stringify(JSON.stringify(serverConfig))
393 const configScriptTag = `<script type="application/javascript">window.PeerTubeServerConfig = ${serverConfigString}</script>`
395 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.SERVER_CONFIG, configScriptTag)
398 private static async addAsyncPluginCSS (htmlStringPage: string) {
399 const globalCSSContent = await readFile(PLUGIN_GLOBAL_CSS_PATH)
400 if (globalCSSContent.byteLength === 0) return htmlStringPage
402 const fileHash = sha256(globalCSSContent)
403 const linkTag = `<link rel="stylesheet" href="/plugins/global.css?hash=${fileHash}" />`
405 return htmlStringPage.replace('</head>', linkTag + '</head>')
408 private static generateOpenGraphMetaTags (tags: Tags) {
410 'og:type': tags.ogType,
411 'og:site_name': tags.escapedSiteName,
412 'og:title': tags.escapedTitle,
413 'og:image': tags.image.url
416 if (tags.image.width && tags.image.height) {
417 metaTags['og:image:width'] = tags.image.width
418 metaTags['og:image:height'] = tags.image.height
421 metaTags['og:url'] = tags.url
422 metaTags['og:description'] = tags.escapedDescription
425 metaTags['og:video:url'] = tags.embed.url
426 metaTags['og:video:secure_url'] = tags.embed.url
427 metaTags['og:video:type'] = 'text/html'
428 metaTags['og:video:width'] = EMBED_SIZE.width
429 metaTags['og:video:height'] = EMBED_SIZE.height
435 private static generateStandardMetaTags (tags: Tags) {
437 name: tags.escapedTitle,
438 description: tags.escapedDescription,
439 image: tags.image.url
443 private static generateTwitterCardMetaTags (tags: Tags) {
445 'twitter:card': tags.twitterCard,
446 'twitter:site': CONFIG.SERVICES.TWITTER.USERNAME,
447 'twitter:title': tags.escapedTitle,
448 'twitter:description': tags.escapedDescription,
449 'twitter:image': tags.image.url
452 if (tags.image.width && tags.image.height) {
453 metaTags['twitter:image:width'] = tags.image.width
454 metaTags['twitter:image:height'] = tags.image.height
457 if (tags.twitterCard === 'player') {
458 metaTags['twitter:player'] = tags.embed.url
459 metaTags['twitter:player:width'] = EMBED_SIZE.width
460 metaTags['twitter:player:height'] = EMBED_SIZE.height
466 private static generateSchemaTags (tags: Tags) {
468 '@context': 'http://schema.org',
469 '@type': tags.schemaType,
470 'name': tags.escapedTitle,
471 'description': tags.escapedDescription,
472 'image': tags.image.url,
477 schema['numberOfItems'] = tags.list.numberOfItems
478 schema['thumbnailUrl'] = tags.image.url
482 schema['embedUrl'] = tags.embed.url
483 schema['uploadDate'] = tags.embed.createdAt
485 if (tags.embed.duration) schema['duration'] = tags.embed.duration
486 if (tags.embed.views) schema['iterationCount'] = tags.embed.views
488 schema['thumbnailUrl'] = tags.image.url
489 schema['contentUrl'] = tags.url
495 private static addTags (htmlStringPage: string, tagsValues: Tags) {
496 const openGraphMetaTags = this.generateOpenGraphMetaTags(tagsValues)
497 const standardMetaTags = this.generateStandardMetaTags(tagsValues)
498 const twitterCardMetaTags = this.generateTwitterCardMetaTags(tagsValues)
499 const schemaTags = this.generateSchemaTags(tagsValues)
501 const { url, escapedTitle, embed, originUrl, disallowIndexation } = tagsValues
503 const oembedLinkTags: { type: string, href: string, escapedTitle: string }[] = []
506 oembedLinkTags.push({
507 type: 'application/json+oembed',
508 href: WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(url),
516 Object.keys(openGraphMetaTags).forEach(tagName => {
517 const tagValue = openGraphMetaTags[tagName]
519 tagsStr += `<meta property="${tagName}" content="${tagValue}" />`
523 Object.keys(standardMetaTags).forEach(tagName => {
524 const tagValue = standardMetaTags[tagName]
526 tagsStr += `<meta property="${tagName}" content="${tagValue}" />`
530 Object.keys(twitterCardMetaTags).forEach(tagName => {
531 const tagValue = twitterCardMetaTags[tagName]
533 tagsStr += `<meta property="${tagName}" content="${tagValue}" />`
537 for (const oembedLinkTag of oembedLinkTags) {
538 tagsStr += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.escapedTitle}" />`
543 tagsStr += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>`
546 // SEO, use origin URL
547 tagsStr += `<link rel="canonical" href="${originUrl}" />`
549 if (disallowIndexation) {
550 tagsStr += `<meta name="robots" content="noindex" />`
553 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.META_TAGS, tagsStr)
557 function sendHTML (html: string, res: express.Response, localizedHTML: boolean = false) {
558 res.set('Content-Type', 'text/html; charset=UTF-8')
561 res.set('Vary', 'Accept-Language')
564 return res.send(html)
567 async function serveIndexHTML (req: express.Request, res: express.Response) {
568 if (req.accepts(ACCEPT_HEADERS) === 'html' || !req.headers.accept) {
570 await generateHTMLPage(req, res, req.params.language)
573 logger.error('Cannot generate HTML page.', err)
574 return res.status(HttpStatusCode.INTERNAL_SERVER_ERROR_500).end()
578 return res.status(HttpStatusCode.NOT_ACCEPTABLE_406).end()
581 // ---------------------------------------------------------------------------
589 async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
590 const html = await ClientHtml.getDefaultHTMLPage(req, res, paramLang)
592 return sendHTML(html, res, true)