]>
Commit | Line | Data |
---|---|---|
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' | |
12 | ||
13 | export class ClientHtml { | |
14 | ||
15 | private static htmlCache: { [path: string]: string } = {} | |
16 | ||
17 | static invalidCache () { | |
18 | ClientHtml.htmlCache = {} | |
19 | } | |
20 | ||
21 | static async getDefaultHTMLPage (req: express.Request, res: express.Response, paramLang?: string) { | |
22 | const html = await ClientHtml.getIndexHTML(req, res, paramLang) | |
23 | ||
24 | let customHtml = ClientHtml.addTitleTag(html) | |
25 | customHtml = ClientHtml.addDescriptionTag(customHtml) | |
26 | ||
27 | return customHtml | |
28 | } | |
29 | ||
30 | static async getWatchHTMLPage (videoId: string, req: express.Request, res: express.Response) { | |
31 | let videoPromise: Bluebird<VideoModel> | |
32 | ||
33 | // Let Angular application handle errors | |
34 | if (validator.isInt(videoId) || validator.isUUID(videoId, 4)) { | |
35 | videoPromise = VideoModel.loadAndPopulateAccountAndServerAndTags(videoId) | |
36 | } else { | |
37 | return ClientHtml.getIndexHTML(req, res) | |
38 | } | |
39 | ||
40 | const [ html, video ] = await Promise.all([ | |
41 | ClientHtml.getIndexHTML(req, res), | |
42 | videoPromise | |
43 | ]) | |
44 | ||
45 | // Let Angular application handle errors | |
46 | if (!video || video.privacy === VideoPrivacy.PRIVATE) { | |
47 | return ClientHtml.getIndexHTML(req, res) | |
48 | } | |
49 | ||
50 | let customHtml = ClientHtml.addTitleTag(html, escapeHTML(video.name)) | |
51 | customHtml = ClientHtml.addDescriptionTag(customHtml, escapeHTML(video.description)) | |
52 | customHtml = ClientHtml.addOpenGraphAndOEmbedTags(customHtml, video) | |
53 | ||
54 | return customHtml | |
55 | } | |
56 | ||
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] | |
60 | ||
61 | const buffer = await readFile(path) | |
62 | ||
63 | let html = buffer.toString() | |
64 | ||
65 | html = ClientHtml.addCustomCSS(html) | |
66 | ||
67 | ClientHtml.htmlCache[path] = html | |
68 | ||
69 | return html | |
70 | } | |
71 | ||
72 | private static getIndexPath (req: express.Request, res: express.Response, paramLang?: string) { | |
73 | let lang: string | |
74 | ||
75 | // Check param lang validity | |
76 | if (paramLang && is18nLocale(paramLang)) { | |
77 | lang = paramLang | |
78 | ||
79 | // Save locale in cookies | |
80 | res.cookie('clientLanguage', lang, { | |
81 | secure: CONFIG.WEBSERVER.SCHEME === 'https', | |
82 | sameSite: true, | |
83 | maxAge: 1000 * 3600 * 24 * 90 // 3 months | |
84 | }) | |
85 | ||
86 | } else if (req.cookies.clientLanguage && is18nLocale(req.cookies.clientLanguage)) { | |
87 | lang = req.cookies.clientLanguage | |
88 | } else { | |
89 | lang = req.acceptsLanguages(POSSIBLE_LOCALES) || getDefaultLocale() | |
90 | } | |
91 | ||
92 | return join(__dirname, '../../../client/dist/' + buildFileLocale(lang) + '/index.html') | |
93 | } | |
94 | ||
95 | private static addTitleTag (htmlStringPage: string, title?: string) { | |
96 | let text = title || CONFIG.INSTANCE.NAME | |
97 | if (title) text += ` - ${CONFIG.INSTANCE.NAME}` | |
98 | ||
99 | const titleTag = `<title>${text}</title>` | |
100 | ||
101 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.TITLE, titleTag) | |
102 | } | |
103 | ||
104 | private static addDescriptionTag (htmlStringPage: string, description?: string) { | |
105 | const content = description || CONFIG.INSTANCE.SHORT_DESCRIPTION | |
106 | const descriptionTag = `<meta name="description" content="${content}" />` | |
107 | ||
108 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.DESCRIPTION, descriptionTag) | |
109 | } | |
110 | ||
111 | private static addCustomCSS (htmlStringPage: string) { | |
112 | const styleTag = '<style class="custom-css-style">' + CONFIG.INSTANCE.CUSTOMIZATIONS.CSS + '</style>' | |
113 | ||
114 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.CUSTOM_CSS, styleTag) | |
115 | } | |
116 | ||
117 | private static addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoModel) { | |
118 | const previewUrl = CONFIG.WEBSERVER.URL + video.getPreviewStaticPath() | |
119 | const videoUrl = CONFIG.WEBSERVER.URL + video.getWatchStaticPath() | |
120 | ||
121 | const videoNameEscaped = escapeHTML(video.name) | |
122 | const videoDescriptionEscaped = escapeHTML(video.description) | |
123 | const embedUrl = CONFIG.WEBSERVER.URL + video.getEmbedStaticPath() | |
124 | ||
125 | const openGraphMetaTags = { | |
126 | 'og:type': 'video', | |
127 | 'og:title': videoNameEscaped, | |
128 | 'og:image': previewUrl, | |
129 | 'og:url': videoUrl, | |
130 | 'og:description': videoDescriptionEscaped, | |
131 | ||
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, | |
137 | ||
138 | 'name': videoNameEscaped, | |
139 | 'description': videoDescriptionEscaped, | |
140 | 'image': previewUrl, | |
141 | ||
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 | |
150 | } | |
151 | ||
152 | const oembedLinkTags = [ | |
153 | { | |
154 | type: 'application/json+oembed', | |
155 | href: CONFIG.WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(videoUrl), | |
156 | title: videoNameEscaped | |
157 | } | |
158 | ] | |
159 | ||
160 | const schemaTags = { | |
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, | |
169 | embedUrl: embedUrl, | |
170 | interactionCount: video.views | |
171 | } | |
172 | ||
173 | let tagsString = '' | |
174 | ||
175 | // Opengraph | |
176 | Object.keys(openGraphMetaTags).forEach(tagName => { | |
177 | const tagValue = openGraphMetaTags[tagName] | |
178 | ||
179 | tagsString += `<meta property="${tagName}" content="${tagValue}" />` | |
180 | }) | |
181 | ||
182 | // OEmbed | |
183 | for (const oembedLinkTag of oembedLinkTags) { | |
184 | tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />` | |
185 | } | |
186 | ||
187 | // Schema.org | |
188 | tagsString += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>` | |
189 | ||
190 | // SEO | |
191 | tagsString += `<link rel="canonical" href="${videoUrl}" />` | |
192 | ||
193 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.OPENGRAPH_AND_OEMBED, tagsString) | |
194 | } | |
195 | } |