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, CUSTOM_HTML_TAG_COMMENTS, EMBED_SIZE, STATIC_PATHS } from '../initializers'
5 import { join } from 'path'
6 import { escapeHTML } from '../helpers/core-utils'
7 import { VideoModel } from '../models/video/video'
8 import * as validator from 'validator'
9 import { VideoPrivacy } from '../../shared/models/videos'
10 import { readFile } from 'fs-extra'
11 import { getActivityStreamDuration } from '../models/video/video-format-utils'
13 export class ClientHtml {
15 private static htmlCache: { [path: string]: string } = {}
17 static invalidCache () {
18 ClientHtml.htmlCache = {}
21 static async getDefaultHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
22 const html = await ClientHtml.getIndexHTML(req, res, paramLang)
24 let customHtml = ClientHtml.addTitleTag(html)
25 customHtml = ClientHtml.addDescriptionTag(customHtml)
30 static async getWatchHTMLPage (videoId: string, req: express.Request, res: express.Response) {
31 let videoPromise: Bluebird<VideoModel>
33 // Let Angular application handle errors
34 if (validator.isInt(videoId) || validator.isUUID(videoId, 4)) {
35 videoPromise = VideoModel.loadAndPopulateAccountAndServerAndTags(videoId)
37 return ClientHtml.getIndexHTML(req, res)
40 const [ html, video ] = await Promise.all([
41 ClientHtml.getIndexHTML(req, res),
45 // Let Angular application handle errors
46 if (!video || video.privacy === VideoPrivacy.PRIVATE) {
47 return ClientHtml.getIndexHTML(req, res)
50 let customHtml = ClientHtml.addTitleTag(html, escapeHTML(video.name))
51 customHtml = ClientHtml.addDescriptionTag(customHtml, escapeHTML(video.description))
52 customHtml = ClientHtml.addOpenGraphAndOEmbedTags(customHtml, video)
57 private static async getIndexHTML (req: express.Request, res: express.Response, paramLang?: string) {
58 const path = ClientHtml.getIndexPath(req, res, paramLang)
59 if (ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path]
61 const buffer = await readFile(path)
63 let html = buffer.toString()
65 html = ClientHtml.addCustomCSS(html)
67 ClientHtml.htmlCache[path] = html
72 private static getIndexPath (req: express.Request, res: express.Response, paramLang?: string) {
75 // Check param lang validity
76 if (paramLang && is18nLocale(paramLang)) {
79 // Save locale in cookies
80 res.cookie('clientLanguage', lang, {
81 secure: CONFIG.WEBSERVER.SCHEME === 'https',
83 maxAge: 1000 * 3600 * 24 * 90 // 3 months
86 } else if (req.cookies.clientLanguage && is18nLocale(req.cookies.clientLanguage)) {
87 lang = req.cookies.clientLanguage
89 lang = req.acceptsLanguages(POSSIBLE_LOCALES) || getDefaultLocale()
92 return join(__dirname, '../../../client/dist/' + buildFileLocale(lang) + '/index.html')
95 private static addTitleTag (htmlStringPage: string, title?: string) {
96 let text = title || CONFIG.INSTANCE.NAME
97 if (title) text += ` - ${CONFIG.INSTANCE.NAME}`
99 const titleTag = `<title>${text}</title>`
101 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.TITLE, titleTag)
104 private static addDescriptionTag (htmlStringPage: string, description?: string) {
105 const content = description || CONFIG.INSTANCE.SHORT_DESCRIPTION
106 const descriptionTag = `<meta name="description" content="${content}" />`
108 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.DESCRIPTION, descriptionTag)
111 private static addCustomCSS (htmlStringPage: string) {
112 const styleTag = '<style class="custom-css-style">' + CONFIG.INSTANCE.CUSTOMIZATIONS.CSS + '</style>'
114 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.CUSTOM_CSS, styleTag)
117 private static addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoModel) {
118 const previewUrl = CONFIG.WEBSERVER.URL + video.getPreviewStaticPath()
119 const videoUrl = CONFIG.WEBSERVER.URL + video.getWatchStaticPath()
121 const videoNameEscaped = escapeHTML(video.name)
122 const videoDescriptionEscaped = escapeHTML(video.description)
123 const embedUrl = CONFIG.WEBSERVER.URL + video.getEmbedStaticPath()
125 const openGraphMetaTags = {
127 'og:title': videoNameEscaped,
128 'og:image': previewUrl,
130 'og:description': videoDescriptionEscaped,
132 'og:video:url': embedUrl,
133 'og:video:secure_url': embedUrl,
134 'og:video:type': 'text/html',
135 'og:video:width': EMBED_SIZE.width,
136 'og:video:height': EMBED_SIZE.height,
138 'name': videoNameEscaped,
139 'description': videoDescriptionEscaped,
142 'twitter:card': CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary_large_image',
143 'twitter:site': CONFIG.SERVICES.TWITTER.USERNAME,
144 'twitter:title': videoNameEscaped,
145 'twitter:description': videoDescriptionEscaped,
146 'twitter:image': previewUrl,
147 'twitter:player': embedUrl,
148 'twitter:player:width': EMBED_SIZE.width,
149 'twitter:player:height': EMBED_SIZE.height
152 const oembedLinkTags = [
154 type: 'application/json+oembed',
155 href: CONFIG.WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(videoUrl),
156 title: videoNameEscaped
161 '@context': 'http://schema.org',
162 '@type': 'VideoObject',
163 name: videoNameEscaped,
164 description: videoDescriptionEscaped,
165 thumbnailUrl: previewUrl,
166 uploadDate: video.createdAt.toISOString(),
167 duration: getActivityStreamDuration(video.duration),
168 contentUrl: videoUrl,
170 interactionCount: video.views
176 Object.keys(openGraphMetaTags).forEach(tagName => {
177 const tagValue = openGraphMetaTags[tagName]
179 tagsString += `<meta property="${tagName}" content="${tagValue}" />`
183 for (const oembedLinkTag of oembedLinkTags) {
184 tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />`
188 tagsString += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>`
191 tagsString += `<link rel="canonical" href="${videoUrl}" />`
193 return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.OPENGRAPH_AND_OEMBED, tagsString)