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 addHtmlLang (htmlStringPage: string, paramLang: string) {
354 return htmlStringPage.replace('<html>', `<html lang="${paramLang}">`)
357 private static addManifestContentHash (htmlStringPage: string) {
358 return htmlStringPage.replace('[manifestContentHash]', FILES_CONTENT_HASH.MANIFEST)
361 private static addFaviconContentHash (htmlStringPage: string) {
362 return htmlStringPage.replace('[faviconContentHash]', FILES_CONTENT_HASH.FAVICON)
365 private static addLogoContentHash (htmlStringPage: string) {
366 return htmlStringPage.replace('[logoContentHash]', FILES_CONTENT_HASH.LOGO)
369 private static addTitleTag (htmlStringPage: string, title?: string) {
370 let text = title || CONFIG.INSTANCE.NAME
371 if (title) text += ` - ${CONFIG.INSTANCE.NAME}`
373 const titleTag = `<title>${escapeHTML(text)}</title>`
375 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.TITLE, titleTag)
378 private static addDescriptionTag (htmlStringPage: string, description?: string) {
379 const content = description || CONFIG.INSTANCE.SHORT_DESCRIPTION
380 const descriptionTag = `<meta name="description" content="${escapeHTML(content)}" />`
382 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.DESCRIPTION, descriptionTag)
385 private static addCustomCSS (htmlStringPage: string) {
386 const styleTag = `<style class="custom-css-style">${CONFIG.INSTANCE.CUSTOMIZATIONS.CSS}</style>`
388 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.CUSTOM_CSS, styleTag)
391 private static addServerConfig (htmlStringPage: string, serverConfig: HTMLServerConfig) {
392 // Stringify the JSON object, and then stringify the string object so we can inject it into the HTML
393 const serverConfigString = JSON.stringify(JSON.stringify(serverConfig))
394 const configScriptTag = `<script type="application/javascript">window.PeerTubeServerConfig = ${serverConfigString}</script>`
396 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.SERVER_CONFIG, configScriptTag)
399 private static async addAsyncPluginCSS (htmlStringPage: string) {
400 const globalCSSContent = await readFile(PLUGIN_GLOBAL_CSS_PATH)
401 if (globalCSSContent.byteLength === 0) return htmlStringPage
403 const fileHash = sha256(globalCSSContent)
404 const linkTag = `<link rel="stylesheet" href="/plugins/global.css?hash=${fileHash}" />`
406 return htmlStringPage.replace('</head>', linkTag + '</head>')
409 private static generateOpenGraphMetaTags (tags: Tags) {
411 'og:type': tags.ogType,
412 'og:site_name': tags.escapedSiteName,
413 'og:title': tags.escapedTitle,
414 'og:image': tags.image.url
417 if (tags.image.width && tags.image.height) {
418 metaTags['og:image:width'] = tags.image.width
419 metaTags['og:image:height'] = tags.image.height
422 metaTags['og:url'] = tags.url
423 metaTags['og:description'] = tags.escapedDescription
426 metaTags['og:video:url'] = tags.embed.url
427 metaTags['og:video:secure_url'] = tags.embed.url
428 metaTags['og:video:type'] = 'text/html'
429 metaTags['og:video:width'] = EMBED_SIZE.width
430 metaTags['og:video:height'] = EMBED_SIZE.height
436 private static generateStandardMetaTags (tags: Tags) {
438 name: tags.escapedTitle,
439 description: tags.escapedDescription,
440 image: tags.image.url
444 private static generateTwitterCardMetaTags (tags: Tags) {
446 'twitter:card': tags.twitterCard,
447 'twitter:site': CONFIG.SERVICES.TWITTER.USERNAME,
448 'twitter:title': tags.escapedTitle,
449 'twitter:description': tags.escapedDescription,
450 'twitter:image': tags.image.url
453 if (tags.image.width && tags.image.height) {
454 metaTags['twitter:image:width'] = tags.image.width
455 metaTags['twitter:image:height'] = tags.image.height
458 if (tags.twitterCard === 'player') {
459 metaTags['twitter:player'] = tags.embed.url
460 metaTags['twitter:player:width'] = EMBED_SIZE.width
461 metaTags['twitter:player:height'] = EMBED_SIZE.height
467 private static generateSchemaTags (tags: Tags) {
469 '@context': 'http://schema.org',
470 '@type': tags.schemaType,
471 'name': tags.escapedTitle,
472 'description': tags.escapedDescription,
473 'image': tags.image.url,
478 schema['numberOfItems'] = tags.list.numberOfItems
479 schema['thumbnailUrl'] = tags.image.url
483 schema['embedUrl'] = tags.embed.url
484 schema['uploadDate'] = tags.embed.createdAt
486 if (tags.embed.duration) schema['duration'] = tags.embed.duration
487 if (tags.embed.views) schema['iterationCount'] = tags.embed.views
489 schema['thumbnailUrl'] = tags.image.url
490 schema['contentUrl'] = tags.url
496 private static addTags (htmlStringPage: string, tagsValues: Tags) {
497 const openGraphMetaTags = this.generateOpenGraphMetaTags(tagsValues)
498 const standardMetaTags = this.generateStandardMetaTags(tagsValues)
499 const twitterCardMetaTags = this.generateTwitterCardMetaTags(tagsValues)
500 const schemaTags = this.generateSchemaTags(tagsValues)
502 const { url, escapedTitle, embed, originUrl, disallowIndexation } = tagsValues
504 const oembedLinkTags: { type: string, href: string, escapedTitle: string }[] = []
507 oembedLinkTags.push({
508 type: 'application/json+oembed',
509 href: WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(url),
517 Object.keys(openGraphMetaTags).forEach(tagName => {
518 const tagValue = openGraphMetaTags[tagName]
520 tagsStr += `<meta property="${tagName}" content="${tagValue}" />`
524 Object.keys(standardMetaTags).forEach(tagName => {
525 const tagValue = standardMetaTags[tagName]
527 tagsStr += `<meta property="${tagName}" content="${tagValue}" />`
531 Object.keys(twitterCardMetaTags).forEach(tagName => {
532 const tagValue = twitterCardMetaTags[tagName]
534 tagsStr += `<meta property="${tagName}" content="${tagValue}" />`
538 for (const oembedLinkTag of oembedLinkTags) {
539 tagsStr += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.escapedTitle}" />`
544 tagsStr += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>`
547 // SEO, use origin URL
548 tagsStr += `<link rel="canonical" href="${originUrl}" />`
550 if (disallowIndexation) {
551 tagsStr += `<meta name="robots" content="noindex" />`
554 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.META_TAGS, tagsStr)
558 function sendHTML (html: string, res: express.Response, localizedHTML: boolean = false) {
559 res.set('Content-Type', 'text/html; charset=UTF-8')
562 res.set('Vary', 'Accept-Language')
565 return res.send(html)
568 async function serveIndexHTML (req: express.Request, res: express.Response) {
569 if (req.accepts(ACCEPT_HEADERS) === 'html' || !req.headers.accept) {
571 await generateHTMLPage(req, res, req.params.language)
574 logger.error('Cannot generate HTML page.', err)
575 return res.status(HttpStatusCode.INTERNAL_SERVER_ERROR_500).end()
579 return res.status(HttpStatusCode.NOT_ACCEPTABLE_406).end()
582 // ---------------------------------------------------------------------------
590 async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
591 const html = await ClientHtml.getDefaultHTMLPage(req, res, paramLang)
593 return sendHTML(html, res, true)