1 import * as express from 'express'
2 import { buildFileLocale, getDefaultLocale, is18nLocale, POSSIBLE_LOCALES } from '../../shared/models/i18n/i18n'
3 import { CUSTOM_HTML_TAG_COMMENTS, EMBED_SIZE, PLUGIN_GLOBAL_CSS_PATH, WEBSERVER } from '../initializers/constants'
4 import { join } from 'path'
5 import { escapeHTML, sha256 } from '../helpers/core-utils'
6 import { VideoModel } from '../models/video/video'
7 import * as validator from 'validator'
8 import { VideoPrivacy } from '../../shared/models/videos'
9 import { readFile } from 'fs-extra'
10 import { getActivityStreamDuration } from '../models/video/video-format-utils'
11 import { AccountModel } from '../models/account/account'
12 import { VideoChannelModel } from '../models/video/video-channel'
13 import * as Bluebird from 'bluebird'
14 import { CONFIG } from '../initializers/config'
16 export class ClientHtml {
18 private static htmlCache: { [ path: string ]: string } = {}
20 static invalidCache () {
21 ClientHtml.htmlCache = {}
24 static async getDefaultHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
25 const html = await ClientHtml.getIndexHTML(req, res, paramLang)
27 let customHtml = ClientHtml.addTitleTag(html)
28 customHtml = ClientHtml.addDescriptionTag(customHtml)
33 static async getWatchHTMLPage (videoId: string, req: express.Request, res: express.Response) {
34 // Let Angular application handle errors
35 if (!validator.isInt(videoId) && !validator.isUUID(videoId, 4)) {
36 return ClientHtml.getIndexHTML(req, res)
39 const [ html, video ] = await Promise.all([
40 ClientHtml.getIndexHTML(req, res),
41 VideoModel.loadAndPopulateAccountAndServerAndTags(videoId)
44 // Let Angular application handle errors
45 if (!video || video.privacy === VideoPrivacy.PRIVATE) {
46 return ClientHtml.getIndexHTML(req, res)
49 let customHtml = ClientHtml.addTitleTag(html, escapeHTML(video.name))
50 customHtml = ClientHtml.addDescriptionTag(customHtml, escapeHTML(video.description))
51 customHtml = ClientHtml.addVideoOpenGraphAndOEmbedTags(customHtml, video)
56 static async getAccountHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
57 return this.getAccountOrChannelHTMLPage(() => AccountModel.loadByNameWithHost(nameWithHost), req, res)
60 static async getVideoChannelHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) {
61 return this.getAccountOrChannelHTMLPage(() => VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithHost), req, res)
64 private static async getAccountOrChannelHTMLPage (
65 loader: () => Bluebird<AccountModel | VideoChannelModel>,
69 const [ html, entity ] = await Promise.all([
70 ClientHtml.getIndexHTML(req, res),
74 // Let Angular application handle errors
76 return ClientHtml.getIndexHTML(req, res)
79 let customHtml = ClientHtml.addTitleTag(html, escapeHTML(entity.getDisplayName()))
80 customHtml = ClientHtml.addDescriptionTag(customHtml, escapeHTML(entity.description))
81 customHtml = ClientHtml.addAccountOrChannelMetaTags(customHtml, entity)
86 private static async getIndexHTML (req: express.Request, res: express.Response, paramLang?: string) {
87 const path = ClientHtml.getIndexPath(req, res, paramLang)
88 if (ClientHtml.htmlCache[ path ]) return ClientHtml.htmlCache[ path ]
90 const buffer = await readFile(path)
92 let html = buffer.toString()
94 html = ClientHtml.addCustomCSS(html)
95 html = await ClientHtml.addAsyncPluginCSS(html)
97 ClientHtml.htmlCache[ path ] = html
102 private static getIndexPath (req: express.Request, res: express.Response, paramLang?: string) {
105 // Check param lang validity
106 if (paramLang && is18nLocale(paramLang)) {
109 // Save locale in cookies
110 res.cookie('clientLanguage', lang, {
111 secure: WEBSERVER.SCHEME === 'https',
113 maxAge: 1000 * 3600 * 24 * 90 // 3 months
116 } else if (req.cookies.clientLanguage && is18nLocale(req.cookies.clientLanguage)) {
117 lang = req.cookies.clientLanguage
119 lang = req.acceptsLanguages(POSSIBLE_LOCALES) || getDefaultLocale()
122 return join(__dirname, '../../../client/dist/' + buildFileLocale(lang) + '/index.html')
125 private static addTitleTag (htmlStringPage: string, title?: string) {
126 let text = title || CONFIG.INSTANCE.NAME
127 if (title) text += ` - ${CONFIG.INSTANCE.NAME}`
129 const titleTag = `<title>${text}</title>`
131 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.TITLE, titleTag)
134 private static addDescriptionTag (htmlStringPage: string, description?: string) {
135 const content = description || CONFIG.INSTANCE.SHORT_DESCRIPTION
136 const descriptionTag = `<meta name="description" content="${content}" />`
138 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.DESCRIPTION, descriptionTag)
141 private static addCustomCSS (htmlStringPage: string) {
142 const styleTag = `<style class="custom-css-style">${CONFIG.INSTANCE.CUSTOMIZATIONS.CSS}</style>`
144 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.CUSTOM_CSS, styleTag)
147 private static async addAsyncPluginCSS (htmlStringPage: string) {
148 const globalCSSContent = await readFile(PLUGIN_GLOBAL_CSS_PATH)
149 if (!globalCSSContent) return htmlStringPage
151 const fileHash = sha256(globalCSSContent)
152 const linkTag = `<link rel="stylesheet" href="/plugins/global.css?hash=${fileHash}" />`
154 return htmlStringPage.replace('</head>', linkTag + '</head>')
157 private static addVideoOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoModel) {
158 const previewUrl = WEBSERVER.URL + video.getPreviewStaticPath()
159 const videoUrl = WEBSERVER.URL + video.getWatchStaticPath()
161 const videoNameEscaped = escapeHTML(video.name)
162 const videoDescriptionEscaped = escapeHTML(video.description)
163 const embedUrl = WEBSERVER.URL + video.getEmbedStaticPath()
165 const openGraphMetaTags = {
167 'og:title': videoNameEscaped,
168 'og:image': previewUrl,
170 'og:description': videoDescriptionEscaped,
172 'og:video:url': embedUrl,
173 'og:video:secure_url': embedUrl,
174 'og:video:type': 'text/html',
175 'og:video:width': EMBED_SIZE.width,
176 'og:video:height': EMBED_SIZE.height,
178 'name': videoNameEscaped,
179 'description': videoDescriptionEscaped,
182 'twitter:card': CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary_large_image',
183 'twitter:site': CONFIG.SERVICES.TWITTER.USERNAME,
184 'twitter:title': videoNameEscaped,
185 'twitter:description': videoDescriptionEscaped,
186 'twitter:image': previewUrl,
187 'twitter:player': embedUrl,
188 'twitter:player:width': EMBED_SIZE.width,
189 'twitter:player:height': EMBED_SIZE.height
192 const oembedLinkTags = [
194 type: 'application/json+oembed',
195 href: WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(videoUrl),
196 title: videoNameEscaped
201 '@context': 'http://schema.org',
202 '@type': 'VideoObject',
203 name: videoNameEscaped,
204 description: videoDescriptionEscaped,
205 thumbnailUrl: previewUrl,
206 uploadDate: video.createdAt.toISOString(),
207 duration: getActivityStreamDuration(video.duration),
208 contentUrl: videoUrl,
210 interactionCount: video.views
216 Object.keys(openGraphMetaTags).forEach(tagName => {
217 const tagValue = openGraphMetaTags[ tagName ]
219 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
223 for (const oembedLinkTag of oembedLinkTags) {
224 tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />`
228 tagsString += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>`
230 // SEO, use origin video url so Google does not index remote videos
231 tagsString += `<link rel="canonical" href="${video.url}" />`
233 return this.addOpenGraphAndOEmbedTags(htmlStringPage, tagsString)
236 private static addAccountOrChannelMetaTags (htmlStringPage: string, entity: AccountModel | VideoChannelModel) {
237 // SEO, use origin account or channel URL
238 const metaTags = `<link rel="canonical" href="${entity.Actor.url}" />`
240 return this.addOpenGraphAndOEmbedTags(htmlStringPage, metaTags)
243 private static addOpenGraphAndOEmbedTags (htmlStringPage: string, metaTags: string) {
244 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.META_TAGS, metaTags)