1 import * as express from 'express'
2 import * as Bluebird from 'bluebird'
3 import { buildFileLocale, getDefaultLocale, is18nLocale, POSSIBLE_LOCALES } from '../../shared/models/i18n/i18n'
4 import { CONFIG, EMBED_SIZE, CUSTOM_HTML_TAG_COMMENTS, STATIC_PATHS } from '../initializers'
5 import { join } from 'path'
6 import { escapeHTML, readFileBufferPromise } from '../helpers/core-utils'
7 import { VideoModel } from '../models/video/video'
8 import * as validator from 'validator'
9 import { VideoPrivacy } from '../../shared/models/videos'
11 export class ClientHtml {
13 private static htmlCache: { [path: string]: string } = {}
15 static invalidCache () {
16 ClientHtml.htmlCache = {}
19 static async getIndexHTML (req: express.Request, res: express.Response, paramLang?: string) {
20 const path = ClientHtml.getIndexPath(req, res, paramLang)
21 if (ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path]
23 const buffer = await readFileBufferPromise(path)
25 let html = buffer.toString()
27 html = ClientHtml.addTitleTag(html)
28 html = ClientHtml.addDescriptionTag(html)
29 html = ClientHtml.addCustomCSS(html)
31 ClientHtml.htmlCache[path] = html
36 static async getWatchHTMLPage (videoId: string, req: express.Request, res: express.Response) {
37 let videoPromise: Bluebird<VideoModel>
39 // Let Angular application handle errors
40 if (validator.isUUID(videoId, 4)) {
41 videoPromise = VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(videoId)
42 } else if (validator.isInt(videoId)) {
43 videoPromise = VideoModel.loadAndPopulateAccountAndServerAndTags(+videoId)
45 return ClientHtml.getIndexHTML(req, res)
48 const [ html, video ] = await Promise.all([
49 ClientHtml.getIndexHTML(req, res),
53 // Let Angular application handle errors
54 if (!video || video.privacy === VideoPrivacy.PRIVATE) {
55 return ClientHtml.getIndexHTML(req, res)
58 return ClientHtml.addOpenGraphAndOEmbedTags(html, video)
61 private static getIndexPath (req: express.Request, res: express.Response, paramLang?: string) {
64 // Check param lang validity
65 if (paramLang && is18nLocale(paramLang)) {
68 // Save locale in cookies
69 res.cookie('clientLanguage', lang, {
70 secure: CONFIG.WEBSERVER.SCHEME === 'https',
72 maxAge: 1000 * 3600 * 24 * 90 // 3 months
75 } else if (req.cookies.clientLanguage && is18nLocale(req.cookies.clientLanguage)) {
76 lang = req.cookies.clientLanguage
78 lang = req.acceptsLanguages(POSSIBLE_LOCALES) || getDefaultLocale()
81 return join(__dirname, '../../../client/dist/' + buildFileLocale(lang) + '/index.html')
84 private static addTitleTag (htmlStringPage: string) {
85 const titleTag = '<title>' + CONFIG.INSTANCE.NAME + '</title>'
87 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.TITLE, titleTag)
90 private static addDescriptionTag (htmlStringPage: string) {
91 const descriptionTag = `<meta name="description" content="${CONFIG.INSTANCE.SHORT_DESCRIPTION}" />`
93 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.DESCRIPTION, descriptionTag)
96 private static addCustomCSS (htmlStringPage: string) {
97 const styleTag = '<style class="custom-css-style">' + CONFIG.INSTANCE.CUSTOMIZATIONS.CSS + '</style>'
99 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.CUSTOM_CSS, styleTag)
102 private static addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoModel) {
103 const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName()
104 const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
106 const videoNameEscaped = escapeHTML(video.name)
107 const videoDescriptionEscaped = escapeHTML(video.description)
108 const embedUrl = CONFIG.WEBSERVER.URL + video.getEmbedStaticPath()
110 const openGraphMetaTags = {
112 'og:title': videoNameEscaped,
113 'og:image': previewUrl,
115 'og:description': videoDescriptionEscaped,
117 'og:video:url': embedUrl,
118 'og:video:secure_url': embedUrl,
119 'og:video:type': 'text/html',
120 'og:video:width': EMBED_SIZE.width,
121 'og:video:height': EMBED_SIZE.height,
123 'name': videoNameEscaped,
124 'description': videoDescriptionEscaped,
127 'twitter:card': CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary_large_image',
128 'twitter:site': CONFIG.SERVICES.TWITTER.USERNAME,
129 'twitter:title': videoNameEscaped,
130 'twitter:description': videoDescriptionEscaped,
131 'twitter:image': previewUrl,
132 'twitter:player': embedUrl,
133 'twitter:player:width': EMBED_SIZE.width,
134 'twitter:player:height': EMBED_SIZE.height
137 const oembedLinkTags = [
139 type: 'application/json+oembed',
140 href: CONFIG.WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(videoUrl),
141 title: videoNameEscaped
146 '@context': 'http://schema.org',
147 '@type': 'VideoObject',
148 name: videoNameEscaped,
149 description: videoDescriptionEscaped,
150 thumbnailUrl: previewUrl,
151 uploadDate: video.createdAt.toISOString(),
152 duration: video.getActivityStreamDuration(),
153 contentUrl: videoUrl,
155 interactionCount: video.views
161 Object.keys(openGraphMetaTags).forEach(tagName => {
162 const tagValue = openGraphMetaTags[tagName]
164 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
168 for (const oembedLinkTag of oembedLinkTags) {
169 tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />`
173 tagsString += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>`
176 tagsString += `<link rel="canonical" href="${videoUrl}" />`
178 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.OPENGRAPH_AND_OEMBED, tagsString)