]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import { buildFileLocale, getDefaultLocale, is18nLocale, POSSIBLE_LOCALES } from '../../shared/core-utils/i18n/i18n' | |
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' | |
11 | import { join } from 'path' | |
12 | import { escapeHTML, sha256 } from '../helpers/core-utils' | |
13 | import { VideoModel } from '../models/video/video' | |
14 | import { VideoPlaylistModel } from '../models/video/video-playlist' | |
15 | import validator from 'validator' | |
16 | import { VideoPrivacy, VideoPlaylistPrivacy } from '../../shared/models/videos' | |
17 | import { readFile } from 'fs-extra' | |
18 | import { getActivityStreamDuration } from '../models/video/video-format-utils' | |
19 | import { AccountModel } from '../models/account/account' | |
20 | import { VideoChannelModel } from '../models/video/video-channel' | |
21 | import * as Bluebird from 'bluebird' | |
22 | import { CONFIG } from '../initializers/config' | |
23 | import { logger } from '../helpers/logger' | |
24 | import { MAccountActor, MChannelActor } from '../types/models' | |
25 | ||
26 | type Tags = { | |
27 | ogType: string | |
28 | twitterCard: 'player' | 'summary' | 'summary_large_image' | |
29 | schemaType: string | |
30 | ||
31 | list?: { | |
32 | numberOfItems: number | |
33 | } | |
34 | ||
35 | siteName: string | |
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 | ||
54 | export class ClientHtml { | |
55 | ||
56 | private static htmlCache: { [path: string]: string } = {} | |
57 | ||
58 | static invalidCache () { | |
59 | logger.info('Cleaning HTML cache.') | |
60 | ||
61 | ClientHtml.htmlCache = {} | |
62 | } | |
63 | ||
64 | static async getDefaultHTMLPage (req: express.Request, res: express.Response, paramLang?: string) { | |
65 | const html = paramLang | |
66 | ? await ClientHtml.getIndexHTML(req, res, paramLang) | |
67 | : await ClientHtml.getIndexHTML(req, res) | |
68 | ||
69 | let customHtml = ClientHtml.addTitleTag(html) | |
70 | customHtml = ClientHtml.addDescriptionTag(customHtml) | |
71 | ||
72 | return customHtml | |
73 | } | |
74 | ||
75 | static async getWatchHTMLPage (videoId: string, req: express.Request, res: express.Response) { | |
76 | // Let Angular application handle errors | |
77 | if (!validator.isInt(videoId) && !validator.isUUID(videoId, 4)) { | |
78 | res.status(404) | |
79 | return ClientHtml.getIndexHTML(req, res) | |
80 | } | |
81 | ||
82 | const [ html, video ] = await Promise.all([ | |
83 | ClientHtml.getIndexHTML(req, res), | |
84 | VideoModel.loadWithBlacklist(videoId) | |
85 | ]) | |
86 | ||
87 | // Let Angular application handle errors | |
88 | if (!video || video.privacy === VideoPrivacy.PRIVATE || video.privacy === VideoPrivacy.INTERNAL || video.VideoBlacklist) { | |
89 | res.status(404) | |
90 | return html | |
91 | } | |
92 | ||
93 | let customHtml = ClientHtml.addTitleTag(html, escapeHTML(video.name)) | |
94 | customHtml = ClientHtml.addDescriptionTag(customHtml, escapeHTML(video.description)) | |
95 | ||
96 | const url = WEBSERVER.URL + video.getWatchStaticPath() | |
97 | const title = escapeHTML(video.name) | |
98 | const siteName = escapeHTML(CONFIG.INSTANCE.NAME) | |
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 | ||
116 | customHtml = ClientHtml.addTags(customHtml, { url, siteName, title, description, image, embed, ogType, twitterCard, schemaType }) | |
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) | |
144 | const siteName = escapeHTML(CONFIG.INSTANCE.NAME) | |
145 | const description = escapeHTML(videoPlaylist.description) | |
146 | ||
147 | const image = { | |
148 | url: videoPlaylist.getThumbnailUrl() | |
149 | } | |
150 | ||
151 | const embed = { | |
152 | url: WEBSERVER.URL + videoPlaylist.getEmbedStaticPath(), | |
153 | createdAt: videoPlaylist.createdAt.toISOString() | |
154 | } | |
155 | ||
156 | const list = { | |
157 | numberOfItems: videoPlaylist.get('videosLength') as number | |
158 | } | |
159 | ||
160 | const ogType = 'video' | |
161 | const twitterCard = CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary' | |
162 | const schemaType = 'ItemList' | |
163 | ||
164 | customHtml = ClientHtml.addTags(customHtml, { url, siteName, embed, title, description, image, list, ogType, twitterCard, schemaType }) | |
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 | ||
177 | static async getEmbedHTML () { | |
178 | const path = ClientHtml.getEmbedPath() | |
179 | ||
180 | if (ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path] | |
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 | ||
192 | private static async getAccountOrChannelHTMLPage ( | |
193 | loader: () => Bluebird<MAccountActor | MChannelActor>, | |
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) { | |
204 | res.status(404) | |
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)) | |
210 | ||
211 | const url = entity.Actor.url | |
212 | const siteName = escapeHTML(CONFIG.INSTANCE.NAME) | |
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 | ||
226 | customHtml = ClientHtml.addTags(customHtml, { url, title, siteName, description, image, ogType, twitterCard, schemaType }) | |
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) | |
233 | if (ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path] | |
234 | ||
235 | const buffer = await readFile(path) | |
236 | ||
237 | let html = buffer.toString() | |
238 | ||
239 | if (paramLang) html = ClientHtml.addHtmlLang(html, paramLang) | |
240 | html = ClientHtml.addManifestContentHash(html) | |
241 | html = ClientHtml.addFaviconContentHash(html) | |
242 | html = ClientHtml.addLogoContentHash(html) | |
243 | html = ClientHtml.addCustomCSS(html) | |
244 | html = await ClientHtml.addAsyncPluginCSS(html) | |
245 | ||
246 | ClientHtml.htmlCache[path] = html | |
247 | ||
248 | return html | |
249 | } | |
250 | ||
251 | private static getIndexPath (req: express.Request, res: express.Response, paramLang: string) { | |
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, { | |
260 | secure: WEBSERVER.SCHEME === 'https', | |
261 | sameSite: 'none', | |
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 | ||
274 | private static getEmbedPath () { | |
275 | return join(__dirname, '../../../client/dist/standalone/videos/embed.html') | |
276 | } | |
277 | ||
278 | private static addHtmlLang (htmlStringPage: string, paramLang: string) { | |
279 | return htmlStringPage.replace('<html>', `<html lang="${paramLang}">`) | |
280 | } | |
281 | ||
282 | private static addManifestContentHash (htmlStringPage: string) { | |
283 | return htmlStringPage.replace('[manifestContentHash]', FILES_CONTENT_HASH.MANIFEST) | |
284 | } | |
285 | ||
286 | private static addFaviconContentHash (htmlStringPage: string) { | |
287 | return htmlStringPage.replace('[faviconContentHash]', FILES_CONTENT_HASH.FAVICON) | |
288 | } | |
289 | ||
290 | private static addLogoContentHash (htmlStringPage: string) { | |
291 | return htmlStringPage.replace('[logoContentHash]', FILES_CONTENT_HASH.LOGO) | |
292 | } | |
293 | ||
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>` | |
299 | ||
300 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.TITLE, titleTag) | |
301 | } | |
302 | ||
303 | private static addDescriptionTag (htmlStringPage: string, description?: string) { | |
304 | const content = description || CONFIG.INSTANCE.SHORT_DESCRIPTION | |
305 | const descriptionTag = `<meta name="description" content="${content}" />` | |
306 | ||
307 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.DESCRIPTION, descriptionTag) | |
308 | } | |
309 | ||
310 | private static addCustomCSS (htmlStringPage: string) { | |
311 | const styleTag = `<style class="custom-css-style">${CONFIG.INSTANCE.CUSTOMIZATIONS.CSS}</style>` | |
312 | ||
313 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.CUSTOM_CSS, styleTag) | |
314 | } | |
315 | ||
316 | private static async addAsyncPluginCSS (htmlStringPage: string) { | |
317 | const globalCSSContent = await readFile(PLUGIN_GLOBAL_CSS_PATH) | |
318 | if (globalCSSContent.byteLength === 0) return htmlStringPage | |
319 | ||
320 | const fileHash = sha256(globalCSSContent) | |
321 | const linkTag = `<link rel="stylesheet" href="/plugins/global.css?hash=${fileHash}" />` | |
322 | ||
323 | return htmlStringPage.replace('</head>', linkTag + '</head>') | |
324 | } | |
325 | ||
326 | private static generateOpenGraphMetaTags (tags: Tags) { | |
327 | const metaTags = { | |
328 | 'og:type': tags.ogType, | |
329 | 'og:site_name': tags.siteName, | |
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 | } | |
338 | ||
339 | metaTags['og:url'] = tags.url | |
340 | metaTags['og:description'] = tags.description | |
341 | ||
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 | } | |
349 | ||
350 | return metaTags | |
351 | } | |
352 | ||
353 | private static generateStandardMetaTags (tags: Tags) { | |
354 | return { | |
355 | name: tags.title, | |
356 | description: tags.description, | |
357 | image: tags.image.url | |
358 | } | |
359 | } | |
360 | ||
361 | private static generateTwitterCardMetaTags (tags: Tags) { | |
362 | const metaTags = { | |
363 | 'twitter:card': tags.twitterCard, | |
364 | 'twitter:site': CONFIG.SERVICES.TWITTER.USERNAME, | |
365 | 'twitter:title': tags.title, | |
366 | 'twitter:description': tags.description, | |
367 | 'twitter:image': tags.image.url | |
368 | } | |
369 | ||
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 | ||
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 | ||
381 | return metaTags | |
382 | } | |
383 | ||
384 | private static generateSchemaTags (tags: Tags) { | |
385 | const schema = { | |
386 | '@context': 'http://schema.org', | |
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 | |
402 | ||
403 | if (tags.embed.duration) schema['duration'] = tags.embed.duration | |
404 | if (tags.embed.views) schema['iterationCount'] = tags.embed.views | |
405 | ||
406 | schema['thumbnailUrl'] = tags.image.url | |
407 | schema['contentUrl'] = tags.url | |
408 | } | |
409 | ||
410 | return schema | |
411 | } | |
412 | ||
413 | private static addTags (htmlStringPage: string, tagsValues: Tags) { | |
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 | ||
421 | const oembedLinkTags: { type: string, href: string, title: string }[] = [] | |
422 | ||
423 | if (embed) { | |
424 | oembedLinkTags.push({ | |
425 | type: 'application/json+oembed', | |
426 | href: WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(url), | |
427 | title | |
428 | }) | |
429 | } | |
430 | ||
431 | let tagsString = '' | |
432 | ||
433 | // Opengraph | |
434 | Object.keys(openGraphMetaTags).forEach(tagName => { | |
435 | const tagValue = openGraphMetaTags[tagName] | |
436 | ||
437 | tagsString += `<meta property="${tagName}" content="${tagValue}" />` | |
438 | }) | |
439 | ||
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 | ||
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 | |
460 | if (schemaTags) { | |
461 | tagsString += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>` | |
462 | } | |
463 | ||
464 | // SEO, use origin URL | |
465 | tagsString += `<link rel="canonical" href="${url}" />` | |
466 | ||
467 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.META_TAGS, tagsString) | |
468 | } | |
469 | } |