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