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