]>
Commit | Line | Data |
---|---|---|
e032aec9 | 1 | import * as express from 'express' |
e032aec9 | 2 | import { buildFileLocale, getDefaultLocale, is18nLocale, POSSIBLE_LOCALES } from '../../shared/models/i18n/i18n' |
8d987ec6 K |
3 | import { |
4 | AVATARS_SIZE, | |
5 | CUSTOM_HTML_TAG_COMMENTS, | |
6 | EMBED_SIZE, | |
7 | PLUGIN_GLOBAL_CSS_PATH, | |
8 | WEBSERVER, | |
9 | FILES_CONTENT_HASH | |
10 | } from '../initializers/constants' | |
e032aec9 | 11 | import { join } from 'path' |
a8b666e9 | 12 | import { escapeHTML, sha256 } from '../helpers/core-utils' |
e032aec9 | 13 | import { VideoModel } from '../models/video/video' |
8d987ec6 | 14 | import { VideoPlaylistModel } from '../models/video/video-playlist' |
7cde3b9c | 15 | import validator from 'validator' |
8d987ec6 | 16 | import { VideoPrivacy, VideoPlaylistPrivacy } from '../../shared/models/videos' |
62689b94 | 17 | import { readFile } from 'fs-extra' |
098eb377 | 18 | import { getActivityStreamDuration } from '../models/video/video-format-utils' |
92bf2f62 C |
19 | import { AccountModel } from '../models/account/account' |
20 | import { VideoChannelModel } from '../models/video/video-channel' | |
21 | import * as Bluebird from 'bluebird' | |
6dd9de95 | 22 | import { CONFIG } from '../initializers/config' |
3e753302 | 23 | import { logger } from '../helpers/logger' |
8d987ec6 | 24 | import { MAccountActor, MChannelActor } from '../types/models' |
e032aec9 C |
25 | |
26 | export class ClientHtml { | |
27 | ||
a1587156 | 28 | private static htmlCache: { [path: string]: string } = {} |
e032aec9 C |
29 | |
30 | static invalidCache () { | |
3e753302 C |
31 | logger.info('Cleaning HTML cache.') |
32 | ||
e032aec9 C |
33 | ClientHtml.htmlCache = {} |
34 | } | |
35 | ||
9aac4423 | 36 | static async getDefaultHTMLPage (req: express.Request, res: express.Response, paramLang?: string) { |
7738273b RK |
37 | const html = paramLang |
38 | ? await ClientHtml.getIndexHTML(req, res, paramLang) | |
39 | : await ClientHtml.getIndexHTML(req, res) | |
e032aec9 | 40 | |
9aac4423 C |
41 | let customHtml = ClientHtml.addTitleTag(html) |
42 | customHtml = ClientHtml.addDescriptionTag(customHtml) | |
e032aec9 | 43 | |
9aac4423 | 44 | return customHtml |
e032aec9 C |
45 | } |
46 | ||
47 | static async getWatchHTMLPage (videoId: string, req: express.Request, res: express.Response) { | |
e032aec9 | 48 | // Let Angular application handle errors |
92bf2f62 | 49 | if (!validator.isInt(videoId) && !validator.isUUID(videoId, 4)) { |
c08579e1 | 50 | res.status(404) |
e032aec9 C |
51 | return ClientHtml.getIndexHTML(req, res) |
52 | } | |
53 | ||
54 | const [ html, video ] = await Promise.all([ | |
55 | ClientHtml.getIndexHTML(req, res), | |
d636ab58 | 56 | VideoModel.loadWithBlacklist(videoId) |
e032aec9 C |
57 | ]) |
58 | ||
59 | // Let Angular application handle errors | |
22a73cb8 | 60 | if (!video || video.privacy === VideoPrivacy.PRIVATE || video.privacy === VideoPrivacy.INTERNAL || video.VideoBlacklist) { |
c08579e1 C |
61 | res.status(404) |
62 | return html | |
e032aec9 C |
63 | } |
64 | ||
9aac4423 C |
65 | let customHtml = ClientHtml.addTitleTag(html, escapeHTML(video.name)) |
66 | customHtml = ClientHtml.addDescriptionTag(customHtml, escapeHTML(video.description)) | |
8d987ec6 K |
67 | |
68 | const url = WEBSERVER.URL + video.getWatchStaticPath() | |
69 | const title = escapeHTML(video.name) | |
70 | const description = escapeHTML(video.description) | |
71 | ||
72 | const image = { | |
73 | url: WEBSERVER.URL + video.getPreviewStaticPath() | |
74 | } | |
75 | ||
76 | const embed = { | |
77 | url: WEBSERVER.URL + video.getEmbedStaticPath(), | |
78 | createdAt: video.createdAt.toISOString(), | |
79 | duration: getActivityStreamDuration(video.duration), | |
80 | views: video.views | |
81 | } | |
82 | ||
83 | const ogType = 'video' | |
84 | const twitterCard = CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary_large_image' | |
85 | const schemaType = 'VideoObject' | |
86 | ||
87 | customHtml = ClientHtml.addTags(customHtml, { url, title, description, image, embed, ogType, twitterCard, schemaType }) | |
88 | ||
89 | return customHtml | |
90 | } | |
91 | ||
92 | static async getWatchPlaylistHTMLPage (videoPlaylistId: string, req: express.Request, res: express.Response) { | |
93 | // Let Angular application handle errors | |
94 | if (!validator.isInt(videoPlaylistId) && !validator.isUUID(videoPlaylistId, 4)) { | |
95 | res.status(404) | |
96 | return ClientHtml.getIndexHTML(req, res) | |
97 | } | |
98 | ||
99 | const [ html, videoPlaylist ] = await Promise.all([ | |
100 | ClientHtml.getIndexHTML(req, res), | |
101 | VideoPlaylistModel.loadWithAccountAndChannel(videoPlaylistId, null) | |
102 | ]) | |
103 | ||
104 | // Let Angular application handle errors | |
105 | if (!videoPlaylist || videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) { | |
106 | res.status(404) | |
107 | return html | |
108 | } | |
109 | ||
110 | let customHtml = ClientHtml.addTitleTag(html, escapeHTML(videoPlaylist.name)) | |
111 | customHtml = ClientHtml.addDescriptionTag(customHtml, escapeHTML(videoPlaylist.description)) | |
112 | ||
113 | const url = videoPlaylist.getWatchUrl() | |
114 | const title = escapeHTML(videoPlaylist.name) | |
115 | const description = escapeHTML(videoPlaylist.description) | |
116 | ||
117 | const image = { | |
118 | url: videoPlaylist.getThumbnailUrl() | |
119 | } | |
120 | ||
121 | const list = { | |
122 | numberOfItems: videoPlaylist.get('videosLength') | |
123 | } | |
124 | ||
125 | const ogType = 'video' | |
126 | const twitterCard = 'summary' | |
127 | const schemaType = 'ItemList' | |
128 | ||
129 | customHtml = ClientHtml.addTags(customHtml, { url, title, description, image, list, ogType, twitterCard, schemaType }) | |
92bf2f62 C |
130 | |
131 | return customHtml | |
132 | } | |
133 | ||
134 | static async getAccountHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) { | |
135 | return this.getAccountOrChannelHTMLPage(() => AccountModel.loadByNameWithHost(nameWithHost), req, res) | |
136 | } | |
137 | ||
138 | static async getVideoChannelHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) { | |
139 | return this.getAccountOrChannelHTMLPage(() => VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithHost), req, res) | |
140 | } | |
141 | ||
142 | private static async getAccountOrChannelHTMLPage ( | |
453e83ea | 143 | loader: () => Bluebird<MAccountActor | MChannelActor>, |
92bf2f62 C |
144 | req: express.Request, |
145 | res: express.Response | |
146 | ) { | |
147 | const [ html, entity ] = await Promise.all([ | |
148 | ClientHtml.getIndexHTML(req, res), | |
149 | loader() | |
150 | ]) | |
151 | ||
152 | // Let Angular application handle errors | |
153 | if (!entity) { | |
c08579e1 | 154 | res.status(404) |
92bf2f62 C |
155 | return ClientHtml.getIndexHTML(req, res) |
156 | } | |
157 | ||
158 | let customHtml = ClientHtml.addTitleTag(html, escapeHTML(entity.getDisplayName())) | |
159 | customHtml = ClientHtml.addDescriptionTag(customHtml, escapeHTML(entity.description)) | |
8d987ec6 K |
160 | |
161 | const url = entity.Actor.url | |
162 | const title = escapeHTML(entity.getDisplayName()) | |
163 | const description = escapeHTML(entity.description) | |
164 | ||
165 | const image = { | |
166 | url: entity.Actor.getAvatarUrl(), | |
167 | width: AVATARS_SIZE.width, | |
168 | height: AVATARS_SIZE.height | |
169 | } | |
170 | ||
171 | const ogType = 'website' | |
172 | const twitterCard = 'summary' | |
173 | const schemaType = 'ProfilePage' | |
174 | ||
175 | customHtml = ClientHtml.addTags(customHtml, { url, title, description, image, ogType, twitterCard, schemaType }) | |
9aac4423 C |
176 | |
177 | return customHtml | |
178 | } | |
179 | ||
180 | private static async getIndexHTML (req: express.Request, res: express.Response, paramLang?: string) { | |
181 | const path = ClientHtml.getIndexPath(req, res, paramLang) | |
a1587156 | 182 | if (ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path] |
9aac4423 C |
183 | |
184 | const buffer = await readFile(path) | |
185 | ||
186 | let html = buffer.toString() | |
187 | ||
7738273b | 188 | if (paramLang) html = ClientHtml.addHtmlLang(html, paramLang) |
caf2aaf4 K |
189 | html = ClientHtml.addManifestContentHash(html) |
190 | html = ClientHtml.addFaviconContentHash(html) | |
191 | html = ClientHtml.addLogoContentHash(html) | |
9aac4423 | 192 | html = ClientHtml.addCustomCSS(html) |
a8b666e9 | 193 | html = await ClientHtml.addAsyncPluginCSS(html) |
9aac4423 | 194 | |
a1587156 | 195 | ClientHtml.htmlCache[path] = html |
9aac4423 C |
196 | |
197 | return html | |
e032aec9 C |
198 | } |
199 | ||
7738273b | 200 | private static getIndexPath (req: express.Request, res: express.Response, paramLang: string) { |
e032aec9 C |
201 | let lang: string |
202 | ||
203 | // Check param lang validity | |
204 | if (paramLang && is18nLocale(paramLang)) { | |
205 | lang = paramLang | |
206 | ||
207 | // Save locale in cookies | |
208 | res.cookie('clientLanguage', lang, { | |
6dd9de95 | 209 | secure: WEBSERVER.SCHEME === 'https', |
bc90883f | 210 | sameSite: 'none', |
e032aec9 C |
211 | maxAge: 1000 * 3600 * 24 * 90 // 3 months |
212 | }) | |
213 | ||
214 | } else if (req.cookies.clientLanguage && is18nLocale(req.cookies.clientLanguage)) { | |
215 | lang = req.cookies.clientLanguage | |
216 | } else { | |
217 | lang = req.acceptsLanguages(POSSIBLE_LOCALES) || getDefaultLocale() | |
218 | } | |
219 | ||
220 | return join(__dirname, '../../../client/dist/' + buildFileLocale(lang) + '/index.html') | |
221 | } | |
222 | ||
7738273b RK |
223 | private static addHtmlLang (htmlStringPage: string, paramLang: string) { |
224 | return htmlStringPage.replace('<html>', `<html lang="${paramLang}">`) | |
225 | } | |
226 | ||
caf2aaf4 K |
227 | private static addManifestContentHash (htmlStringPage: string) { |
228 | return htmlStringPage.replace('[manifestContentHash]', FILES_CONTENT_HASH.MANIFEST) | |
229 | } | |
230 | ||
1fdf8edb | 231 | private static addFaviconContentHash (htmlStringPage: string) { |
caf2aaf4 K |
232 | return htmlStringPage.replace('[faviconContentHash]', FILES_CONTENT_HASH.FAVICON) |
233 | } | |
234 | ||
1fdf8edb | 235 | private static addLogoContentHash (htmlStringPage: string) { |
caf2aaf4 K |
236 | return htmlStringPage.replace('[logoContentHash]', FILES_CONTENT_HASH.LOGO) |
237 | } | |
238 | ||
9aac4423 C |
239 | private static addTitleTag (htmlStringPage: string, title?: string) { |
240 | let text = title || CONFIG.INSTANCE.NAME | |
241 | if (title) text += ` - ${CONFIG.INSTANCE.NAME}` | |
242 | ||
243 | const titleTag = `<title>${text}</title>` | |
e032aec9 C |
244 | |
245 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.TITLE, titleTag) | |
246 | } | |
247 | ||
9aac4423 C |
248 | private static addDescriptionTag (htmlStringPage: string, description?: string) { |
249 | const content = description || CONFIG.INSTANCE.SHORT_DESCRIPTION | |
250 | const descriptionTag = `<meta name="description" content="${content}" />` | |
e032aec9 C |
251 | |
252 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.DESCRIPTION, descriptionTag) | |
253 | } | |
254 | ||
255 | private static addCustomCSS (htmlStringPage: string) { | |
ffb321be | 256 | const styleTag = `<style class="custom-css-style">${CONFIG.INSTANCE.CUSTOMIZATIONS.CSS}</style>` |
e032aec9 C |
257 | |
258 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.CUSTOM_CSS, styleTag) | |
259 | } | |
260 | ||
a8b666e9 C |
261 | private static async addAsyncPluginCSS (htmlStringPage: string) { |
262 | const globalCSSContent = await readFile(PLUGIN_GLOBAL_CSS_PATH) | |
3e753302 | 263 | if (globalCSSContent.byteLength === 0) return htmlStringPage |
a8b666e9 C |
264 | |
265 | const fileHash = sha256(globalCSSContent) | |
266 | const linkTag = `<link rel="stylesheet" href="/plugins/global.css?hash=${fileHash}" />` | |
ffb321be C |
267 | |
268 | return htmlStringPage.replace('</head>', linkTag + '</head>') | |
269 | } | |
270 | ||
8d987ec6 K |
271 | private static generateOpenGraphMetaTags (tags) { |
272 | const metaTags = { | |
273 | 'og:type': tags.ogType, | |
274 | 'og:title': tags.title, | |
275 | 'og:image': tags.image.url | |
276 | } | |
277 | ||
278 | if (tags.image.width && tags.image.height) { | |
279 | metaTags['og:image:width'] = tags.image.width | |
280 | metaTags['og:image:height'] = tags.image.height | |
281 | } | |
e032aec9 | 282 | |
8d987ec6 K |
283 | metaTags['og:url'] = tags.url |
284 | metaTags['og:description'] = tags.description | |
e032aec9 | 285 | |
8d987ec6 K |
286 | if (tags.embed) { |
287 | metaTags['og:video:url'] = tags.embed.url | |
288 | metaTags['og:video:secure_url'] = tags.embed.url | |
289 | metaTags['og:video:type'] = 'text/html' | |
290 | metaTags['og:video:width'] = EMBED_SIZE.width | |
291 | metaTags['og:video:height'] = EMBED_SIZE.height | |
292 | } | |
e032aec9 | 293 | |
8d987ec6 K |
294 | return metaTags |
295 | } | |
e032aec9 | 296 | |
8d987ec6 K |
297 | private static generateStandardMetaTags (tags) { |
298 | return { | |
299 | name: tags.title, | |
300 | description: tags.description, | |
301 | image: tags.image.url | |
302 | } | |
303 | } | |
e032aec9 | 304 | |
8d987ec6 K |
305 | private static generateTwitterCardMetaTags (tags) { |
306 | const metaTags = { | |
307 | 'twitter:card': tags.twitterCard, | |
e032aec9 | 308 | 'twitter:site': CONFIG.SERVICES.TWITTER.USERNAME, |
8d987ec6 K |
309 | 'twitter:title': tags.title, |
310 | 'twitter:description': tags.description, | |
311 | 'twitter:image': tags.image.url | |
e032aec9 C |
312 | } |
313 | ||
8d987ec6 K |
314 | if (tags.image.width && tags.image.height) { |
315 | metaTags['twitter:image:width'] = tags.image.width | |
316 | metaTags['twitter:image:height'] = tags.image.height | |
317 | } | |
318 | ||
319 | return metaTags | |
320 | } | |
e032aec9 | 321 | |
8d987ec6 K |
322 | private static generateSchemaTags (tags) { |
323 | const schema = { | |
e032aec9 | 324 | '@context': 'http://schema.org', |
8d987ec6 K |
325 | '@type': tags.schemaType, |
326 | 'name': tags.title, | |
327 | 'description': tags.description, | |
328 | 'image': tags.image.url, | |
329 | 'url': tags.url | |
330 | } | |
331 | ||
332 | if (tags.list) { | |
333 | schema['numberOfItems'] = tags.list.numberOfItems | |
334 | schema['thumbnailUrl'] = tags.image.url | |
335 | } | |
336 | ||
337 | if (tags.embed) { | |
338 | schema['embedUrl'] = tags.embed.url | |
339 | schema['uploadDate'] = tags.embed.createdAt | |
340 | schema['duration'] = tags.embed.duration | |
341 | schema['iterationCount'] = tags.embed.views | |
342 | schema['thumbnailUrl'] = tags.image.url | |
343 | schema['contentUrl'] = tags.url | |
344 | } | |
345 | ||
346 | return schema | |
347 | } | |
348 | ||
349 | private static addTags (htmlStringPage: string, tagsValues: any) { | |
350 | const openGraphMetaTags = this.generateOpenGraphMetaTags(tagsValues) | |
351 | const standardMetaTags = this.generateStandardMetaTags(tagsValues) | |
352 | const twitterCardMetaTags = this.generateTwitterCardMetaTags(tagsValues) | |
353 | const schemaTags = this.generateSchemaTags(tagsValues) | |
354 | ||
355 | const { url, title, embed } = tagsValues | |
356 | ||
357 | const oembedLinkTags = [] | |
358 | ||
359 | if (embed) { | |
360 | oembedLinkTags.push({ | |
361 | type: 'application/json+oembed', | |
362 | href: WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(url), | |
363 | title | |
364 | }) | |
e032aec9 C |
365 | } |
366 | ||
367 | let tagsString = '' | |
368 | ||
369 | // Opengraph | |
370 | Object.keys(openGraphMetaTags).forEach(tagName => { | |
a1587156 | 371 | const tagValue = openGraphMetaTags[tagName] |
e032aec9 C |
372 | |
373 | tagsString += `<meta property="${tagName}" content="${tagValue}" />` | |
374 | }) | |
375 | ||
8d987ec6 K |
376 | // Standard |
377 | Object.keys(standardMetaTags).forEach(tagName => { | |
378 | const tagValue = standardMetaTags[tagName] | |
379 | ||
380 | tagsString += `<meta property="${tagName}" content="${tagValue}" />` | |
381 | }) | |
382 | ||
383 | // Twitter card | |
384 | Object.keys(twitterCardMetaTags).forEach(tagName => { | |
385 | const tagValue = twitterCardMetaTags[tagName] | |
386 | ||
387 | tagsString += `<meta property="${tagName}" content="${tagValue}" />` | |
388 | }) | |
389 | ||
e032aec9 C |
390 | // OEmbed |
391 | for (const oembedLinkTag of oembedLinkTags) { | |
392 | tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />` | |
393 | } | |
394 | ||
395 | // Schema.org | |
8d987ec6 K |
396 | if (schemaTags) { |
397 | tagsString += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>` | |
398 | } | |
92bf2f62 | 399 | |
8d987ec6 K |
400 | // SEO, use origin URL |
401 | tagsString += `<link rel="canonical" href="${url}" />` | |
92bf2f62 | 402 | |
8d987ec6 | 403 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.META_TAGS, tagsString) |
e032aec9 C |
404 | } |
405 | } |