1 import * as express from 'express'
2 import { buildFileLocale, getDefaultLocale, is18nLocale, POSSIBLE_LOCALES } from '../../shared/core-utils/i18n/i18n'
5 CUSTOM_HTML_TAG_COMMENTS,
7 PLUGIN_GLOBAL_CSS_PATH,
10 } from '../initializers/constants'
11 import { join } from 'path'
12 import { escapeHTML, isTestInstance, sha256 } from '../helpers/core-utils'
13 import { VideoModel } from '../models/video/video'
14 import { VideoPlaylistModel } from '../models/video/video-playlist'
15 import validator from 'validator'
16 import { VideoPrivacy, VideoPlaylistPrivacy } from '../../shared/models/videos'
17 import { readFile } from 'fs-extra'
18 import { getActivityStreamDuration } from '../models/video/video-format-utils'
19 import { AccountModel } from '../models/account/account'
20 import { VideoChannelModel } from '../models/video/video-channel'
21 import * as Bluebird from 'bluebird'
22 import { CONFIG } from '../initializers/config'
23 import { logger } from '../helpers/logger'
24 import { MAccountActor, MChannelActor } from '../types/models'
25 import { HttpStatusCode } from '../../shared/core-utils/miscs/http-error-codes'
29 twitterCard: 'player' | 'summary' | 'summary_large_image'
56 export class ClientHtml {
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, {
120 url, originUrl, siteName, title, description, image, embed, ogType, twitterCard, schemaType
126 static async getWatchPlaylistHTMLPage (videoPlaylistId: string, req: express.Request, res: express.Response) {
127 // Let Angular application handle errors
128 if (!validator.isInt(videoPlaylistId) && !validator.isUUID(videoPlaylistId, 4)) {
129 res.status(HttpStatusCode.NOT_FOUND_404)
130 return ClientHtml.getIndexHTML(req, res)
133 const [ html, videoPlaylist ] = await Promise.all([
134 ClientHtml.getIndexHTML(req, res),
135 VideoPlaylistModel.loadWithAccountAndChannel(videoPlaylistId, null)
138 // Let Angular application handle errors
139 if (!videoPlaylist || videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) {
140 res.status(HttpStatusCode.NOT_FOUND_404)
144 let customHtml = ClientHtml.addTitleTag(html, escapeHTML(videoPlaylist.name))
145 customHtml = ClientHtml.addDescriptionTag(customHtml, escapeHTML(videoPlaylist.description))
147 const url = videoPlaylist.getWatchUrl()
148 const originUrl = videoPlaylist.url
149 const title = escapeHTML(videoPlaylist.name)
150 const siteName = escapeHTML(CONFIG.INSTANCE.NAME)
151 const description = escapeHTML(videoPlaylist.description)
154 url: videoPlaylist.getThumbnailUrl()
158 url: WEBSERVER.URL + videoPlaylist.getEmbedStaticPath(),
159 createdAt: videoPlaylist.createdAt.toISOString()
163 numberOfItems: videoPlaylist.get('videosLength') as number
166 const ogType = 'video'
167 const twitterCard = CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary'
168 const schemaType = 'ItemList'
170 customHtml = ClientHtml.addTags(customHtml, {
171 url, originUrl, siteName, embed, title, description, image, list, ogType, twitterCard, schemaType
177 static async getAccountHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
178 return this.getAccountOrChannelHTMLPage(() => AccountModel.loadByNameWithHost(nameWithHost), req, res)
181 static async getVideoChannelHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
182 return this.getAccountOrChannelHTMLPage(() => VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithHost), req, res)
185 static async getEmbedHTML () {
186 const path = ClientHtml.getEmbedPath()
188 if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path]
190 const buffer = await readFile(path)
192 let html = buffer.toString()
193 html = await ClientHtml.addAsyncPluginCSS(html)
195 ClientHtml.htmlCache[path] = html
200 private static async getAccountOrChannelHTMLPage (
201 loader: () => Bluebird<MAccountActor | MChannelActor>,
202 req: express.Request,
203 res: express.Response
205 const [ html, entity ] = await Promise.all([
206 ClientHtml.getIndexHTML(req, res),
210 // Let Angular application handle errors
212 res.status(HttpStatusCode.NOT_FOUND_404)
213 return ClientHtml.getIndexHTML(req, res)
216 let customHtml = ClientHtml.addTitleTag(html, escapeHTML(entity.getDisplayName()))
217 customHtml = ClientHtml.addDescriptionTag(customHtml, escapeHTML(entity.description))
219 const url = entity.Actor.url
220 const originUrl = entity.Actor.url
221 const siteName = escapeHTML(CONFIG.INSTANCE.NAME)
222 const title = escapeHTML(entity.getDisplayName())
223 const description = escapeHTML(entity.description)
226 url: entity.Actor.getAvatarUrl(),
227 width: AVATARS_SIZE.width,
228 height: AVATARS_SIZE.height
231 const ogType = 'website'
232 const twitterCard = 'summary'
233 const schemaType = 'ProfilePage'
235 customHtml = ClientHtml.addTags(customHtml, { url, originUrl, title, siteName, description, image, ogType, twitterCard, schemaType })
240 private static async getIndexHTML (req: express.Request, res: express.Response, paramLang?: string) {
241 const path = ClientHtml.getIndexPath(req, res, paramLang)
242 if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path]
244 const buffer = await readFile(path)
246 let html = buffer.toString()
248 if (paramLang) html = ClientHtml.addHtmlLang(html, paramLang)
249 html = ClientHtml.addManifestContentHash(html)
250 html = ClientHtml.addFaviconContentHash(html)
251 html = ClientHtml.addLogoContentHash(html)
252 html = ClientHtml.addCustomCSS(html)
253 html = await ClientHtml.addAsyncPluginCSS(html)
255 ClientHtml.htmlCache[path] = html
260 private static getIndexPath (req: express.Request, res: express.Response, paramLang: string) {
263 // Check param lang validity
264 if (paramLang && is18nLocale(paramLang)) {
267 // Save locale in cookies
268 res.cookie('clientLanguage', lang, {
269 secure: WEBSERVER.SCHEME === 'https',
271 maxAge: 1000 * 3600 * 24 * 90 // 3 months
274 } else if (req.cookies.clientLanguage && is18nLocale(req.cookies.clientLanguage)) {
275 lang = req.cookies.clientLanguage
277 lang = req.acceptsLanguages(POSSIBLE_LOCALES) || getDefaultLocale()
280 return join(__dirname, '../../../client/dist/' + buildFileLocale(lang) + '/index.html')
283 private static getEmbedPath () {
284 return join(__dirname, '../../../client/dist/standalone/videos/embed.html')
287 private static addHtmlLang (htmlStringPage: string, paramLang: string) {
288 return htmlStringPage.replace('<html>', `<html lang="${paramLang}">`)
291 private static addManifestContentHash (htmlStringPage: string) {
292 return htmlStringPage.replace('[manifestContentHash]', FILES_CONTENT_HASH.MANIFEST)
295 private static addFaviconContentHash (htmlStringPage: string) {
296 return htmlStringPage.replace('[faviconContentHash]', FILES_CONTENT_HASH.FAVICON)
299 private static addLogoContentHash (htmlStringPage: string) {
300 return htmlStringPage.replace('[logoContentHash]', FILES_CONTENT_HASH.LOGO)
303 private static addTitleTag (htmlStringPage: string, title?: string) {
304 let text = title || CONFIG.INSTANCE.NAME
305 if (title) text += ` - ${CONFIG.INSTANCE.NAME}`
307 const titleTag = `<title>${text}</title>`
309 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.TITLE, titleTag)
312 private static addDescriptionTag (htmlStringPage: string, description?: string) {
313 const content = description || CONFIG.INSTANCE.SHORT_DESCRIPTION
314 const descriptionTag = `<meta name="description" content="${content}" />`
316 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.DESCRIPTION, descriptionTag)
319 private static addCustomCSS (htmlStringPage: string) {
320 const styleTag = `<style class="custom-css-style">${CONFIG.INSTANCE.CUSTOMIZATIONS.CSS}</style>`
322 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.CUSTOM_CSS, styleTag)
325 private static async addAsyncPluginCSS (htmlStringPage: string) {
326 const globalCSSContent = await readFile(PLUGIN_GLOBAL_CSS_PATH)
327 if (globalCSSContent.byteLength === 0) return htmlStringPage
329 const fileHash = sha256(globalCSSContent)
330 const linkTag = `<link rel="stylesheet" href="/plugins/global.css?hash=${fileHash}" />`
332 return htmlStringPage.replace('</head>', linkTag + '</head>')
335 private static generateOpenGraphMetaTags (tags: Tags) {
337 'og:type': tags.ogType,
338 'og:site_name': tags.siteName,
339 'og:title': tags.title,
340 'og:image': tags.image.url
343 if (tags.image.width && tags.image.height) {
344 metaTags['og:image:width'] = tags.image.width
345 metaTags['og:image:height'] = tags.image.height
348 metaTags['og:url'] = tags.url
349 metaTags['og:description'] = tags.description
352 metaTags['og:video:url'] = tags.embed.url
353 metaTags['og:video:secure_url'] = tags.embed.url
354 metaTags['og:video:type'] = 'text/html'
355 metaTags['og:video:width'] = EMBED_SIZE.width
356 metaTags['og:video:height'] = EMBED_SIZE.height
362 private static generateStandardMetaTags (tags: Tags) {
365 description: tags.description,
366 image: tags.image.url
370 private static generateTwitterCardMetaTags (tags: Tags) {
372 'twitter:card': tags.twitterCard,
373 'twitter:site': CONFIG.SERVICES.TWITTER.USERNAME,
374 'twitter:title': tags.title,
375 'twitter:description': tags.description,
376 'twitter:image': tags.image.url
379 if (tags.image.width && tags.image.height) {
380 metaTags['twitter:image:width'] = tags.image.width
381 metaTags['twitter:image:height'] = tags.image.height
384 if (tags.twitterCard === 'player') {
385 metaTags['twitter:player'] = tags.embed.url
386 metaTags['twitter:player:width'] = EMBED_SIZE.width
387 metaTags['twitter:player:height'] = EMBED_SIZE.height
393 private static generateSchemaTags (tags: Tags) {
395 '@context': 'http://schema.org',
396 '@type': tags.schemaType,
398 'description': tags.description,
399 'image': tags.image.url,
404 schema['numberOfItems'] = tags.list.numberOfItems
405 schema['thumbnailUrl'] = tags.image.url
409 schema['embedUrl'] = tags.embed.url
410 schema['uploadDate'] = tags.embed.createdAt
412 if (tags.embed.duration) schema['duration'] = tags.embed.duration
413 if (tags.embed.views) schema['iterationCount'] = tags.embed.views
415 schema['thumbnailUrl'] = tags.image.url
416 schema['contentUrl'] = tags.url
422 private static addTags (htmlStringPage: string, tagsValues: Tags) {
423 const openGraphMetaTags = this.generateOpenGraphMetaTags(tagsValues)
424 const standardMetaTags = this.generateStandardMetaTags(tagsValues)
425 const twitterCardMetaTags = this.generateTwitterCardMetaTags(tagsValues)
426 const schemaTags = this.generateSchemaTags(tagsValues)
428 const { url, title, embed, originUrl } = tagsValues
430 const oembedLinkTags: { type: string, href: string, title: string }[] = []
433 oembedLinkTags.push({
434 type: 'application/json+oembed',
435 href: WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(url),
443 Object.keys(openGraphMetaTags).forEach(tagName => {
444 const tagValue = openGraphMetaTags[tagName]
446 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
450 Object.keys(standardMetaTags).forEach(tagName => {
451 const tagValue = standardMetaTags[tagName]
453 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
457 Object.keys(twitterCardMetaTags).forEach(tagName => {
458 const tagValue = twitterCardMetaTags[tagName]
460 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
464 for (const oembedLinkTag of oembedLinkTags) {
465 tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />`
470 tagsString += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>`
473 // SEO, use origin URL
474 tagsString += `<link rel="canonical" href="${originUrl}" />`
476 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.META_TAGS, tagsString)