1 import * as express from 'express'
2 import { readFile } from 'fs-extra'
3 import { join } from 'path'
4 import validator from 'validator'
5 import { buildFileLocale, getDefaultLocale, is18nLocale, POSSIBLE_LOCALES } from '../../shared/core-utils/i18n/i18n'
6 import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes'
7 import { VideoPlaylistPrivacy, VideoPrivacy } from '../../shared/models/videos'
8 import { isTestInstance, sha256 } from '../helpers/core-utils'
9 import { escapeHTML } from '@shared/core-utils/renderer'
10 import { logger } from '../helpers/logger'
11 import { CONFIG } from '../initializers/config'
15 CUSTOM_HTML_TAG_COMMENTS,
18 PLUGIN_GLOBAL_CSS_PATH,
20 } from '../initializers/constants'
21 import { AccountModel } from '../models/account/account'
22 import { VideoModel } from '../models/video/video'
23 import { VideoChannelModel } from '../models/video/video-channel'
24 import { getActivityStreamDuration } from '../models/video/video-format-utils'
25 import { VideoPlaylistModel } from '../models/video/video-playlist'
26 import { MAccountActor, MChannelActor } from '../types/models'
27 import { mdToPlainText } from '../helpers/markdown'
31 twitterCard: 'player' | 'summary' | 'summary_large_image'
60 private static htmlCache: { [path: string]: string } = {}
62 static invalidCache () {
63 logger.info('Cleaning HTML cache.')
65 ClientHtml.htmlCache = {}
68 static async getDefaultHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
69 const html = paramLang
70 ? await ClientHtml.getIndexHTML(req, res, paramLang)
71 : await ClientHtml.getIndexHTML(req, res)
73 let customHtml = ClientHtml.addTitleTag(html)
74 customHtml = ClientHtml.addDescriptionTag(customHtml)
79 static async getWatchHTMLPage (videoId: string, req: express.Request, res: express.Response) {
80 // Let Angular application handle errors
81 if (!validator.isInt(videoId) && !validator.isUUID(videoId, 4)) {
82 res.status(HttpStatusCode.NOT_FOUND_404)
83 return ClientHtml.getIndexHTML(req, res)
86 const [ html, video ] = await Promise.all([
87 ClientHtml.getIndexHTML(req, res),
88 VideoModel.loadWithBlacklist(videoId)
91 // Let Angular application handle errors
92 if (!video || video.privacy === VideoPrivacy.PRIVATE || video.privacy === VideoPrivacy.INTERNAL || video.VideoBlacklist) {
93 res.status(HttpStatusCode.NOT_FOUND_404)
97 let customHtml = ClientHtml.addTitleTag(html, escapeHTML(video.name))
98 customHtml = ClientHtml.addDescriptionTag(customHtml, mdToPlainText(video.description))
100 const url = WEBSERVER.URL + video.getWatchStaticPath()
101 const originUrl = video.url
102 const title = escapeHTML(video.name)
103 const siteName = escapeHTML(CONFIG.INSTANCE.NAME)
104 const description = mdToPlainText(video.description)
107 url: WEBSERVER.URL + video.getPreviewStaticPath()
111 url: WEBSERVER.URL + video.getEmbedStaticPath(),
112 createdAt: video.createdAt.toISOString(),
113 duration: getActivityStreamDuration(video.duration),
117 const ogType = 'video'
118 const twitterCard = CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary_large_image'
119 const schemaType = 'VideoObject'
121 customHtml = ClientHtml.addTags(customHtml, {
137 static async getWatchPlaylistHTMLPage (videoPlaylistId: string, req: express.Request, res: express.Response) {
138 // Let Angular application handle errors
139 if (!validator.isInt(videoPlaylistId) && !validator.isUUID(videoPlaylistId, 4)) {
140 res.status(HttpStatusCode.NOT_FOUND_404)
141 return ClientHtml.getIndexHTML(req, res)
144 const [ html, videoPlaylist ] = await Promise.all([
145 ClientHtml.getIndexHTML(req, res),
146 VideoPlaylistModel.loadWithAccountAndChannel(videoPlaylistId, null)
149 // Let Angular application handle errors
150 if (!videoPlaylist || videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) {
151 res.status(HttpStatusCode.NOT_FOUND_404)
155 let customHtml = ClientHtml.addTitleTag(html, escapeHTML(videoPlaylist.name))
156 customHtml = ClientHtml.addDescriptionTag(customHtml, mdToPlainText(videoPlaylist.description))
158 const url = videoPlaylist.getWatchUrl()
159 const originUrl = videoPlaylist.url
160 const title = escapeHTML(videoPlaylist.name)
161 const siteName = escapeHTML(CONFIG.INSTANCE.NAME)
162 const description = mdToPlainText(videoPlaylist.description)
165 url: videoPlaylist.getThumbnailUrl()
169 url: WEBSERVER.URL + videoPlaylist.getEmbedStaticPath(),
170 createdAt: videoPlaylist.createdAt.toISOString()
174 numberOfItems: videoPlaylist.get('videosLength') as number
177 const ogType = 'video'
178 const twitterCard = CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary'
179 const schemaType = 'ItemList'
181 customHtml = ClientHtml.addTags(customHtml, {
198 static async getAccountHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
199 return this.getAccountOrChannelHTMLPage(() => AccountModel.loadByNameWithHost(nameWithHost), req, res)
202 static async getVideoChannelHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
203 return this.getAccountOrChannelHTMLPage(() => VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithHost), req, res)
206 static async getEmbedHTML () {
207 const path = ClientHtml.getEmbedPath()
209 if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path]
211 const buffer = await readFile(path)
213 let html = buffer.toString()
214 html = await ClientHtml.addAsyncPluginCSS(html)
215 html = ClientHtml.addCustomCSS(html)
216 html = ClientHtml.addTitleTag(html)
218 ClientHtml.htmlCache[path] = html
223 private static async getAccountOrChannelHTMLPage (
224 loader: () => Promise<MAccountActor | MChannelActor>,
225 req: express.Request,
226 res: express.Response
228 const [ html, entity ] = await Promise.all([
229 ClientHtml.getIndexHTML(req, res),
233 // Let Angular application handle errors
235 res.status(HttpStatusCode.NOT_FOUND_404)
236 return ClientHtml.getIndexHTML(req, res)
239 let customHtml = ClientHtml.addTitleTag(html, escapeHTML(entity.getDisplayName()))
240 customHtml = ClientHtml.addDescriptionTag(customHtml, mdToPlainText(entity.description))
242 const url = entity.getLocalUrl()
243 const originUrl = entity.Actor.url
244 const siteName = escapeHTML(CONFIG.INSTANCE.NAME)
245 const title = escapeHTML(entity.getDisplayName())
246 const description = mdToPlainText(entity.description)
249 url: entity.Actor.getAvatarUrl(),
250 width: ACTOR_IMAGES_SIZE.AVATARS.width,
251 height: ACTOR_IMAGES_SIZE.AVATARS.height
254 const ogType = 'website'
255 const twitterCard = 'summary'
256 const schemaType = 'ProfilePage'
258 customHtml = ClientHtml.addTags(customHtml, {
273 private static async getIndexHTML (req: express.Request, res: express.Response, paramLang?: string) {
274 const path = ClientHtml.getIndexPath(req, res, paramLang)
275 if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path]
277 const buffer = await readFile(path)
279 let html = buffer.toString()
281 if (paramLang) html = ClientHtml.addHtmlLang(html, paramLang)
282 html = ClientHtml.addManifestContentHash(html)
283 html = ClientHtml.addFaviconContentHash(html)
284 html = ClientHtml.addLogoContentHash(html)
285 html = ClientHtml.addCustomCSS(html)
286 html = await ClientHtml.addAsyncPluginCSS(html)
288 ClientHtml.htmlCache[path] = html
293 private static getIndexPath (req: express.Request, res: express.Response, paramLang: string) {
296 // Check param lang validity
297 if (paramLang && is18nLocale(paramLang)) {
300 // Save locale in cookies
301 res.cookie('clientLanguage', lang, {
302 secure: WEBSERVER.SCHEME === 'https',
304 maxAge: 1000 * 3600 * 24 * 90 // 3 months
307 } else if (req.cookies.clientLanguage && is18nLocale(req.cookies.clientLanguage)) {
308 lang = req.cookies.clientLanguage
310 lang = req.acceptsLanguages(POSSIBLE_LOCALES) || getDefaultLocale()
313 return join(__dirname, '../../../client/dist/' + buildFileLocale(lang) + '/index.html')
316 private static getEmbedPath () {
317 return join(__dirname, '../../../client/dist/standalone/videos/embed.html')
320 private static addHtmlLang (htmlStringPage: string, paramLang: string) {
321 return htmlStringPage.replace('<html>', `<html lang="${paramLang}">`)
324 private static addManifestContentHash (htmlStringPage: string) {
325 return htmlStringPage.replace('[manifestContentHash]', FILES_CONTENT_HASH.MANIFEST)
328 private static addFaviconContentHash (htmlStringPage: string) {
329 return htmlStringPage.replace('[faviconContentHash]', FILES_CONTENT_HASH.FAVICON)
332 private static addLogoContentHash (htmlStringPage: string) {
333 return htmlStringPage.replace('[logoContentHash]', FILES_CONTENT_HASH.LOGO)
336 private static addTitleTag (htmlStringPage: string, title?: string) {
337 let text = title || CONFIG.INSTANCE.NAME
338 if (title) text += ` - ${CONFIG.INSTANCE.NAME}`
340 const titleTag = `<title>${text}</title>`
342 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.TITLE, titleTag)
345 private static addDescriptionTag (htmlStringPage: string, description?: string) {
346 const content = description || CONFIG.INSTANCE.SHORT_DESCRIPTION
347 const descriptionTag = `<meta name="description" content="${content}" />`
349 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.DESCRIPTION, descriptionTag)
352 private static addCustomCSS (htmlStringPage: string) {
353 const styleTag = `<style class="custom-css-style">${CONFIG.INSTANCE.CUSTOMIZATIONS.CSS}</style>`
355 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.CUSTOM_CSS, styleTag)
358 private static async addAsyncPluginCSS (htmlStringPage: string) {
359 const globalCSSContent = await readFile(PLUGIN_GLOBAL_CSS_PATH)
360 if (globalCSSContent.byteLength === 0) return htmlStringPage
362 const fileHash = sha256(globalCSSContent)
363 const linkTag = `<link rel="stylesheet" href="/plugins/global.css?hash=${fileHash}" />`
365 return htmlStringPage.replace('</head>', linkTag + '</head>')
368 private static generateOpenGraphMetaTags (tags: Tags) {
370 'og:type': tags.ogType,
371 'og:site_name': tags.siteName,
372 'og:title': tags.title,
373 'og:image': tags.image.url
376 if (tags.image.width && tags.image.height) {
377 metaTags['og:image:width'] = tags.image.width
378 metaTags['og:image:height'] = tags.image.height
381 metaTags['og:url'] = tags.url
382 metaTags['og:description'] = mdToPlainText(tags.description)
385 metaTags['og:video:url'] = tags.embed.url
386 metaTags['og:video:secure_url'] = tags.embed.url
387 metaTags['og:video:type'] = 'text/html'
388 metaTags['og:video:width'] = EMBED_SIZE.width
389 metaTags['og:video:height'] = EMBED_SIZE.height
395 private static generateStandardMetaTags (tags: Tags) {
398 description: mdToPlainText(tags.description),
399 image: tags.image.url
403 private static generateTwitterCardMetaTags (tags: Tags) {
405 'twitter:card': tags.twitterCard,
406 'twitter:site': CONFIG.SERVICES.TWITTER.USERNAME,
407 'twitter:title': tags.title,
408 'twitter:description': tags.description,
409 'twitter:image': tags.image.url
412 if (tags.image.width && tags.image.height) {
413 metaTags['twitter:image:width'] = tags.image.width
414 metaTags['twitter:image:height'] = tags.image.height
417 if (tags.twitterCard === 'player') {
418 metaTags['twitter:player'] = tags.embed.url
419 metaTags['twitter:player:width'] = EMBED_SIZE.width
420 metaTags['twitter:player:height'] = EMBED_SIZE.height
426 private static generateSchemaTags (tags: Tags) {
428 '@context': 'http://schema.org',
429 '@type': tags.schemaType,
431 'description': tags.description,
432 'image': tags.image.url,
437 schema['numberOfItems'] = tags.list.numberOfItems
438 schema['thumbnailUrl'] = tags.image.url
442 schema['embedUrl'] = tags.embed.url
443 schema['uploadDate'] = tags.embed.createdAt
445 if (tags.embed.duration) schema['duration'] = tags.embed.duration
446 if (tags.embed.views) schema['iterationCount'] = tags.embed.views
448 schema['thumbnailUrl'] = tags.image.url
449 schema['contentUrl'] = tags.url
455 private static addTags (htmlStringPage: string, tagsValues: Tags) {
456 const openGraphMetaTags = this.generateOpenGraphMetaTags(tagsValues)
457 const standardMetaTags = this.generateStandardMetaTags(tagsValues)
458 const twitterCardMetaTags = this.generateTwitterCardMetaTags(tagsValues)
459 const schemaTags = this.generateSchemaTags(tagsValues)
461 const { url, title, embed, originUrl } = tagsValues
463 const oembedLinkTags: { type: string, href: string, title: string }[] = []
466 oembedLinkTags.push({
467 type: 'application/json+oembed',
468 href: WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(url),
476 Object.keys(openGraphMetaTags).forEach(tagName => {
477 const tagValue = openGraphMetaTags[tagName]
479 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
483 Object.keys(standardMetaTags).forEach(tagName => {
484 const tagValue = standardMetaTags[tagName]
486 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
490 Object.keys(twitterCardMetaTags).forEach(tagName => {
491 const tagValue = twitterCardMetaTags[tagName]
493 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
497 for (const oembedLinkTag of oembedLinkTags) {
498 tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />`
503 tagsString += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>`
506 // SEO, use origin URL
507 tagsString += `<link rel="canonical" href="${originUrl}" />`
509 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.META_TAGS, tagsString)
513 function sendHTML (html: string, res: express.Response) {
514 res.set('Content-Type', 'text/html; charset=UTF-8')
516 return res.send(html)
519 async function serveIndexHTML (req: express.Request, res: express.Response) {
520 if (req.accepts(ACCEPT_HEADERS) === 'html' ||
521 !req.headers.accept) {
523 await generateHTMLPage(req, res, req.params.language)
526 logger.error('Cannot generate HTML page.', err)
527 return res.sendStatus(HttpStatusCode.INTERNAL_SERVER_ERROR_500)
531 return res.sendStatus(HttpStatusCode.NOT_ACCEPTABLE_406)
534 // ---------------------------------------------------------------------------
542 async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
543 const html = await ClientHtml.getDefaultHTMLPage(req, res, paramLang)
545 return sendHTML(html, res)