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 { escapeHTML, isTestInstance, sha256 } from '../helpers/core-utils'
9 import { logger } from '../helpers/logger'
10 import { CONFIG } from '../initializers/config'
14 CUSTOM_HTML_TAG_COMMENTS,
17 PLUGIN_GLOBAL_CSS_PATH,
19 } from '../initializers/constants'
20 import { AccountModel } from '../models/account/account'
21 import { VideoModel } from '../models/video/video'
22 import { VideoChannelModel } from '../models/video/video-channel'
23 import { getActivityStreamDuration } from '../models/video/video-format-utils'
24 import { VideoPlaylistModel } from '../models/video/video-playlist'
25 import { MAccountActor, MChannelActor } from '../types/models'
29 twitterCard: 'player' | 'summary' | 'summary_large_image'
58 private static htmlCache: { [path: string]: string } = {}
60 static invalidCache () {
61 logger.info('Cleaning HTML cache.')
63 ClientHtml.htmlCache = {}
66 static async getDefaultHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
67 const html = paramLang
68 ? await ClientHtml.getIndexHTML(req, res, paramLang)
69 : await ClientHtml.getIndexHTML(req, res)
71 let customHtml = ClientHtml.addTitleTag(html)
72 customHtml = ClientHtml.addDescriptionTag(customHtml)
77 static async getWatchHTMLPage (videoId: string, req: express.Request, res: express.Response) {
78 // Let Angular application handle errors
79 if (!validator.isInt(videoId) && !validator.isUUID(videoId, 4)) {
80 res.status(HttpStatusCode.NOT_FOUND_404)
81 return ClientHtml.getIndexHTML(req, res)
84 const [ html, video ] = await Promise.all([
85 ClientHtml.getIndexHTML(req, res),
86 VideoModel.loadWithBlacklist(videoId)
89 // Let Angular application handle errors
90 if (!video || video.privacy === VideoPrivacy.PRIVATE || video.privacy === VideoPrivacy.INTERNAL || video.VideoBlacklist) {
91 res.status(HttpStatusCode.NOT_FOUND_404)
95 let customHtml = ClientHtml.addTitleTag(html, escapeHTML(video.name))
96 customHtml = ClientHtml.addDescriptionTag(customHtml, escapeHTML(video.description))
98 const url = WEBSERVER.URL + video.getWatchStaticPath()
99 const originUrl = video.url
100 const title = escapeHTML(video.name)
101 const siteName = escapeHTML(CONFIG.INSTANCE.NAME)
102 const description = escapeHTML(video.description)
105 url: WEBSERVER.URL + video.getPreviewStaticPath()
109 url: WEBSERVER.URL + video.getEmbedStaticPath(),
110 createdAt: video.createdAt.toISOString(),
111 duration: getActivityStreamDuration(video.duration),
115 const ogType = 'video'
116 const twitterCard = CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary_large_image'
117 const schemaType = 'VideoObject'
119 customHtml = ClientHtml.addTags(customHtml, {
135 static async getWatchPlaylistHTMLPage (videoPlaylistId: string, req: express.Request, res: express.Response) {
136 // Let Angular application handle errors
137 if (!validator.isInt(videoPlaylistId) && !validator.isUUID(videoPlaylistId, 4)) {
138 res.status(HttpStatusCode.NOT_FOUND_404)
139 return ClientHtml.getIndexHTML(req, res)
142 const [ html, videoPlaylist ] = await Promise.all([
143 ClientHtml.getIndexHTML(req, res),
144 VideoPlaylistModel.loadWithAccountAndChannel(videoPlaylistId, null)
147 // Let Angular application handle errors
148 if (!videoPlaylist || videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) {
149 res.status(HttpStatusCode.NOT_FOUND_404)
153 let customHtml = ClientHtml.addTitleTag(html, escapeHTML(videoPlaylist.name))
154 customHtml = ClientHtml.addDescriptionTag(customHtml, escapeHTML(videoPlaylist.description))
156 const url = videoPlaylist.getWatchUrl()
157 const originUrl = videoPlaylist.url
158 const title = escapeHTML(videoPlaylist.name)
159 const siteName = escapeHTML(CONFIG.INSTANCE.NAME)
160 const description = escapeHTML(videoPlaylist.description)
163 url: videoPlaylist.getThumbnailUrl()
167 url: WEBSERVER.URL + videoPlaylist.getEmbedStaticPath(),
168 createdAt: videoPlaylist.createdAt.toISOString()
172 numberOfItems: videoPlaylist.get('videosLength') as number
175 const ogType = 'video'
176 const twitterCard = CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary'
177 const schemaType = 'ItemList'
179 customHtml = ClientHtml.addTags(customHtml, {
196 static async getAccountHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
197 return this.getAccountOrChannelHTMLPage(() => AccountModel.loadByNameWithHost(nameWithHost), req, res)
200 static async getVideoChannelHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
201 return this.getAccountOrChannelHTMLPage(() => VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithHost), req, res)
204 static async getEmbedHTML () {
205 const path = ClientHtml.getEmbedPath()
207 if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path]
209 const buffer = await readFile(path)
211 let html = buffer.toString()
212 html = await ClientHtml.addAsyncPluginCSS(html)
213 html = ClientHtml.addCustomCSS(html)
215 ClientHtml.htmlCache[path] = html
220 private static async getAccountOrChannelHTMLPage (
221 loader: () => Promise<MAccountActor | MChannelActor>,
222 req: express.Request,
223 res: express.Response
225 const [ html, entity ] = await Promise.all([
226 ClientHtml.getIndexHTML(req, res),
230 // Let Angular application handle errors
232 res.status(HttpStatusCode.NOT_FOUND_404)
233 return ClientHtml.getIndexHTML(req, res)
236 let customHtml = ClientHtml.addTitleTag(html, escapeHTML(entity.getDisplayName()))
237 customHtml = ClientHtml.addDescriptionTag(customHtml, escapeHTML(entity.description))
239 const url = entity.getLocalUrl()
240 const originUrl = entity.Actor.url
241 const siteName = escapeHTML(CONFIG.INSTANCE.NAME)
242 const title = escapeHTML(entity.getDisplayName())
243 const description = escapeHTML(entity.description)
246 url: entity.Actor.getAvatarUrl(),
247 width: AVATARS_SIZE.width,
248 height: AVATARS_SIZE.height
251 const ogType = 'website'
252 const twitterCard = 'summary'
253 const schemaType = 'ProfilePage'
255 customHtml = ClientHtml.addTags(customHtml, {
270 private static async getIndexHTML (req: express.Request, res: express.Response, paramLang?: string) {
271 const path = ClientHtml.getIndexPath(req, res, paramLang)
272 if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path]
274 const buffer = await readFile(path)
276 let html = buffer.toString()
278 if (paramLang) html = ClientHtml.addHtmlLang(html, paramLang)
279 html = ClientHtml.addManifestContentHash(html)
280 html = ClientHtml.addFaviconContentHash(html)
281 html = ClientHtml.addLogoContentHash(html)
282 html = ClientHtml.addCustomCSS(html)
283 html = await ClientHtml.addAsyncPluginCSS(html)
285 ClientHtml.htmlCache[path] = html
290 private static getIndexPath (req: express.Request, res: express.Response, paramLang: string) {
293 // Check param lang validity
294 if (paramLang && is18nLocale(paramLang)) {
297 // Save locale in cookies
298 res.cookie('clientLanguage', lang, {
299 secure: WEBSERVER.SCHEME === 'https',
301 maxAge: 1000 * 3600 * 24 * 90 // 3 months
304 } else if (req.cookies.clientLanguage && is18nLocale(req.cookies.clientLanguage)) {
305 lang = req.cookies.clientLanguage
307 lang = req.acceptsLanguages(POSSIBLE_LOCALES) || getDefaultLocale()
310 return join(__dirname, '../../../client/dist/' + buildFileLocale(lang) + '/index.html')
313 private static getEmbedPath () {
314 return join(__dirname, '../../../client/dist/standalone/videos/embed.html')
317 private static addHtmlLang (htmlStringPage: string, paramLang: string) {
318 return htmlStringPage.replace('<html>', `<html lang="${paramLang}">`)
321 private static addManifestContentHash (htmlStringPage: string) {
322 return htmlStringPage.replace('[manifestContentHash]', FILES_CONTENT_HASH.MANIFEST)
325 private static addFaviconContentHash (htmlStringPage: string) {
326 return htmlStringPage.replace('[faviconContentHash]', FILES_CONTENT_HASH.FAVICON)
329 private static addLogoContentHash (htmlStringPage: string) {
330 return htmlStringPage.replace('[logoContentHash]', FILES_CONTENT_HASH.LOGO)
333 private static addTitleTag (htmlStringPage: string, title?: string) {
334 let text = title || CONFIG.INSTANCE.NAME
335 if (title) text += ` - ${CONFIG.INSTANCE.NAME}`
337 const titleTag = `<title>${text}</title>`
339 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.TITLE, titleTag)
342 private static addDescriptionTag (htmlStringPage: string, description?: string) {
343 const content = description || CONFIG.INSTANCE.SHORT_DESCRIPTION
344 const descriptionTag = `<meta name="description" content="${content}" />`
346 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.DESCRIPTION, descriptionTag)
349 private static addCustomCSS (htmlStringPage: string) {
350 const styleTag = `<style class="custom-css-style">${CONFIG.INSTANCE.CUSTOMIZATIONS.CSS}</style>`
352 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.CUSTOM_CSS, styleTag)
355 private static async addAsyncPluginCSS (htmlStringPage: string) {
356 const globalCSSContent = await readFile(PLUGIN_GLOBAL_CSS_PATH)
357 if (globalCSSContent.byteLength === 0) return htmlStringPage
359 const fileHash = sha256(globalCSSContent)
360 const linkTag = `<link rel="stylesheet" href="/plugins/global.css?hash=${fileHash}" />`
362 return htmlStringPage.replace('</head>', linkTag + '</head>')
365 private static generateOpenGraphMetaTags (tags: Tags) {
367 'og:type': tags.ogType,
368 'og:site_name': tags.siteName,
369 'og:title': tags.title,
370 'og:image': tags.image.url
373 if (tags.image.width && tags.image.height) {
374 metaTags['og:image:width'] = tags.image.width
375 metaTags['og:image:height'] = tags.image.height
378 metaTags['og:url'] = tags.url
379 metaTags['og:description'] = tags.description
382 metaTags['og:video:url'] = tags.embed.url
383 metaTags['og:video:secure_url'] = tags.embed.url
384 metaTags['og:video:type'] = 'text/html'
385 metaTags['og:video:width'] = EMBED_SIZE.width
386 metaTags['og:video:height'] = EMBED_SIZE.height
392 private static generateStandardMetaTags (tags: Tags) {
395 description: tags.description,
396 image: tags.image.url
400 private static generateTwitterCardMetaTags (tags: Tags) {
402 'twitter:card': tags.twitterCard,
403 'twitter:site': CONFIG.SERVICES.TWITTER.USERNAME,
404 'twitter:title': tags.title,
405 'twitter:description': tags.description,
406 'twitter:image': tags.image.url
409 if (tags.image.width && tags.image.height) {
410 metaTags['twitter:image:width'] = tags.image.width
411 metaTags['twitter:image:height'] = tags.image.height
414 if (tags.twitterCard === 'player') {
415 metaTags['twitter:player'] = tags.embed.url
416 metaTags['twitter:player:width'] = EMBED_SIZE.width
417 metaTags['twitter:player:height'] = EMBED_SIZE.height
423 private static generateSchemaTags (tags: Tags) {
425 '@context': 'http://schema.org',
426 '@type': tags.schemaType,
428 'description': tags.description,
429 'image': tags.image.url,
434 schema['numberOfItems'] = tags.list.numberOfItems
435 schema['thumbnailUrl'] = tags.image.url
439 schema['embedUrl'] = tags.embed.url
440 schema['uploadDate'] = tags.embed.createdAt
442 if (tags.embed.duration) schema['duration'] = tags.embed.duration
443 if (tags.embed.views) schema['iterationCount'] = tags.embed.views
445 schema['thumbnailUrl'] = tags.image.url
446 schema['contentUrl'] = tags.url
452 private static addTags (htmlStringPage: string, tagsValues: Tags) {
453 const openGraphMetaTags = this.generateOpenGraphMetaTags(tagsValues)
454 const standardMetaTags = this.generateStandardMetaTags(tagsValues)
455 const twitterCardMetaTags = this.generateTwitterCardMetaTags(tagsValues)
456 const schemaTags = this.generateSchemaTags(tagsValues)
458 const { url, title, embed, originUrl } = tagsValues
460 const oembedLinkTags: { type: string, href: string, title: string }[] = []
463 oembedLinkTags.push({
464 type: 'application/json+oembed',
465 href: WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(url),
473 Object.keys(openGraphMetaTags).forEach(tagName => {
474 const tagValue = openGraphMetaTags[tagName]
476 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
480 Object.keys(standardMetaTags).forEach(tagName => {
481 const tagValue = standardMetaTags[tagName]
483 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
487 Object.keys(twitterCardMetaTags).forEach(tagName => {
488 const tagValue = twitterCardMetaTags[tagName]
490 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
494 for (const oembedLinkTag of oembedLinkTags) {
495 tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />`
500 tagsString += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>`
503 // SEO, use origin URL
504 tagsString += `<link rel="canonical" href="${originUrl}" />`
506 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.META_TAGS, tagsString)
510 function sendHTML (html: string, res: express.Response) {
511 res.set('Content-Type', 'text/html; charset=UTF-8')
513 return res.send(html)
516 async function serveIndexHTML (req: express.Request, res: express.Response) {
517 if (req.accepts(ACCEPT_HEADERS) === 'html' ||
518 !req.headers.accept) {
520 await generateHTMLPage(req, res, req.params.language)
523 logger.error('Cannot generate HTML page.', err)
524 return res.sendStatus(HttpStatusCode.INTERNAL_SERVER_ERROR_500)
528 return res.sendStatus(HttpStatusCode.NOT_ACCEPTABLE_406)
531 // ---------------------------------------------------------------------------
539 async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
540 const html = await ClientHtml.getDefaultHTMLPage(req, res, paramLang)
542 return sendHTML(html, res)