]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
b49f22d8 C |
2 | import { readFile } from 'fs-extra' |
3 | import { join } from 'path' | |
4 | import validator from 'validator' | |
55cb8bc7 | 5 | import { toCompleteUUID } from '@server/helpers/custom-validators/misc' |
d0800f76 | 6 | import { ActorImageModel } from '@server/models/actor/actor-image' |
f8360396 | 7 | import { root } from '@shared/core-utils' |
aea0b0e7 | 8 | import { escapeHTML } from '@shared/core-utils/renderer' |
f304a158 | 9 | import { sha256 } from '@shared/extra-utils' |
aea0b0e7 | 10 | import { HTMLServerConfig } from '@shared/models' |
bd45d503 | 11 | import { buildFileLocale, getDefaultLocale, is18nLocale, POSSIBLE_LOCALES } from '../../shared/core-utils/i18n/i18n' |
c0e8b12e | 12 | import { HttpStatusCode } from '../../shared/models/http/http-error-codes' |
b49f22d8 | 13 | import { VideoPlaylistPrivacy, VideoPrivacy } from '../../shared/models/videos' |
06aad801 | 14 | import { isTestInstance } from '../helpers/core-utils' |
b49f22d8 | 15 | import { logger } from '../helpers/logger' |
c68e2b2d | 16 | import { mdToOneLinePlainText } from '../helpers/markdown' |
b49f22d8 | 17 | import { CONFIG } from '../initializers/config' |
8d987ec6 | 18 | import { |
b49f22d8 | 19 | ACCEPT_HEADERS, |
8d987ec6 K |
20 | CUSTOM_HTML_TAG_COMMENTS, |
21 | EMBED_SIZE, | |
f2eb23cd | 22 | FILES_CONTENT_HASH, |
b49f22d8 C |
23 | PLUGIN_GLOBAL_CSS_PATH, |
24 | WEBSERVER | |
8d987ec6 | 25 | } from '../initializers/constants' |
92bf2f62 | 26 | import { AccountModel } from '../models/account/account' |
e5dbd508 | 27 | import { getActivityStreamDuration } from '../models/video/formatter/video-format-utils' |
b49f22d8 | 28 | import { VideoModel } from '../models/video/video' |
92bf2f62 | 29 | import { VideoChannelModel } from '../models/video/video-channel' |
b49f22d8 | 30 | import { VideoPlaylistModel } from '../models/video/video-playlist' |
8d987ec6 | 31 | import { MAccountActor, MChannelActor } from '../types/models' |
d0800f76 | 32 | import { getBiggestActorImage } from './actor-image' |
2539932e | 33 | import { ServerConfigManager } from './server-config-manager' |
e032aec9 | 34 | |
6fad8e51 C |
35 | type Tags = { |
36 | ogType: string | |
b96777c3 | 37 | twitterCard: 'player' | 'summary' | 'summary_large_image' |
6fad8e51 C |
38 | schemaType: string |
39 | ||
40 | list?: { | |
41 | numberOfItems: number | |
42 | } | |
43 | ||
55cb8bc7 C |
44 | escapedSiteName: string |
45 | escapedTitle: string | |
46 | escapedDescription: string | |
47 | ||
6fad8e51 | 48 | url: string |
106fa224 | 49 | originUrl: string |
6fad8e51 | 50 | |
352819ef C |
51 | disallowIndexation?: boolean |
52 | ||
6fad8e51 C |
53 | embed?: { |
54 | url: string | |
55 | createdAt: string | |
56 | duration?: string | |
57 | views?: number | |
58 | } | |
59 | ||
60 | image: { | |
61 | url: string | |
62 | width?: number | |
63 | height?: number | |
64 | } | |
65 | } | |
66 | ||
f2eb23cd | 67 | class ClientHtml { |
e032aec9 | 68 | |
a1587156 | 69 | private static htmlCache: { [path: string]: string } = {} |
e032aec9 C |
70 | |
71 | static invalidCache () { | |
3e753302 C |
72 | logger.info('Cleaning HTML cache.') |
73 | ||
e032aec9 C |
74 | ClientHtml.htmlCache = {} |
75 | } | |
76 | ||
9aac4423 | 77 | static async getDefaultHTMLPage (req: express.Request, res: express.Response, paramLang?: string) { |
7738273b RK |
78 | const html = paramLang |
79 | ? await ClientHtml.getIndexHTML(req, res, paramLang) | |
80 | : await ClientHtml.getIndexHTML(req, res) | |
e032aec9 | 81 | |
9aac4423 C |
82 | let customHtml = ClientHtml.addTitleTag(html) |
83 | customHtml = ClientHtml.addDescriptionTag(customHtml) | |
e032aec9 | 84 | |
9aac4423 | 85 | return customHtml |
e032aec9 C |
86 | } |
87 | ||
d4a8e7a6 C |
88 | static async getWatchHTMLPage (videoIdArg: string, req: express.Request, res: express.Response) { |
89 | const videoId = toCompleteUUID(videoIdArg) | |
90 | ||
e032aec9 | 91 | // Let Angular application handle errors |
92bf2f62 | 92 | if (!validator.isInt(videoId) && !validator.isUUID(videoId, 4)) { |
2d53be02 | 93 | res.status(HttpStatusCode.NOT_FOUND_404) |
e032aec9 C |
94 | return ClientHtml.getIndexHTML(req, res) |
95 | } | |
96 | ||
97 | const [ html, video ] = await Promise.all([ | |
98 | ClientHtml.getIndexHTML(req, res), | |
d636ab58 | 99 | VideoModel.loadWithBlacklist(videoId) |
e032aec9 C |
100 | ]) |
101 | ||
102 | // Let Angular application handle errors | |
22a73cb8 | 103 | if (!video || video.privacy === VideoPrivacy.PRIVATE || video.privacy === VideoPrivacy.INTERNAL || video.VideoBlacklist) { |
2d53be02 | 104 | res.status(HttpStatusCode.NOT_FOUND_404) |
c08579e1 | 105 | return html |
e032aec9 | 106 | } |
c68e2b2d | 107 | const description = mdToOneLinePlainText(video.description) |
e032aec9 | 108 | |
55cb8bc7 C |
109 | let customHtml = ClientHtml.addTitleTag(html, video.name) |
110 | customHtml = ClientHtml.addDescriptionTag(customHtml, description) | |
8d987ec6 K |
111 | |
112 | const url = WEBSERVER.URL + video.getWatchStaticPath() | |
106fa224 | 113 | const originUrl = video.url |
55cb8bc7 C |
114 | const title = video.name |
115 | const siteName = CONFIG.INSTANCE.NAME | |
8d987ec6 K |
116 | |
117 | const image = { | |
118 | url: WEBSERVER.URL + video.getPreviewStaticPath() | |
119 | } | |
120 | ||
121 | const embed = { | |
122 | url: WEBSERVER.URL + video.getEmbedStaticPath(), | |
123 | createdAt: video.createdAt.toISOString(), | |
124 | duration: getActivityStreamDuration(video.duration), | |
125 | views: video.views | |
126 | } | |
127 | ||
128 | const ogType = 'video' | |
129 | const twitterCard = CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary_large_image' | |
130 | const schemaType = 'VideoObject' | |
131 | ||
106fa224 | 132 | customHtml = ClientHtml.addTags(customHtml, { |
a59f210f C |
133 | url, |
134 | originUrl, | |
55cb8bc7 C |
135 | escapedSiteName: escapeHTML(siteName), |
136 | escapedTitle: escapeHTML(title), | |
137 | escapedDescription: escapeHTML(description), | |
a59f210f C |
138 | image, |
139 | embed, | |
140 | ogType, | |
141 | twitterCard, | |
142 | schemaType | |
106fa224 | 143 | }) |
8d987ec6 K |
144 | |
145 | return customHtml | |
146 | } | |
147 | ||
d4a8e7a6 C |
148 | static async getWatchPlaylistHTMLPage (videoPlaylistIdArg: string, req: express.Request, res: express.Response) { |
149 | const videoPlaylistId = toCompleteUUID(videoPlaylistIdArg) | |
150 | ||
8d987ec6 K |
151 | // Let Angular application handle errors |
152 | if (!validator.isInt(videoPlaylistId) && !validator.isUUID(videoPlaylistId, 4)) { | |
2d53be02 | 153 | res.status(HttpStatusCode.NOT_FOUND_404) |
8d987ec6 K |
154 | return ClientHtml.getIndexHTML(req, res) |
155 | } | |
156 | ||
157 | const [ html, videoPlaylist ] = await Promise.all([ | |
158 | ClientHtml.getIndexHTML(req, res), | |
159 | VideoPlaylistModel.loadWithAccountAndChannel(videoPlaylistId, null) | |
160 | ]) | |
161 | ||
162 | // Let Angular application handle errors | |
163 | if (!videoPlaylist || videoPlaylist.privacy === VideoPlaylistPrivacy.PRIVATE) { | |
2d53be02 | 164 | res.status(HttpStatusCode.NOT_FOUND_404) |
8d987ec6 K |
165 | return html |
166 | } | |
167 | ||
c68e2b2d | 168 | const description = mdToOneLinePlainText(videoPlaylist.description) |
55cb8bc7 C |
169 | |
170 | let customHtml = ClientHtml.addTitleTag(html, videoPlaylist.name) | |
171 | customHtml = ClientHtml.addDescriptionTag(customHtml, description) | |
8d987ec6 | 172 | |
a892c54a | 173 | const url = WEBSERVER.URL + videoPlaylist.getWatchStaticPath() |
106fa224 | 174 | const originUrl = videoPlaylist.url |
55cb8bc7 C |
175 | const title = videoPlaylist.name |
176 | const siteName = CONFIG.INSTANCE.NAME | |
8d987ec6 K |
177 | |
178 | const image = { | |
179 | url: videoPlaylist.getThumbnailUrl() | |
180 | } | |
181 | ||
6fad8e51 C |
182 | const embed = { |
183 | url: WEBSERVER.URL + videoPlaylist.getEmbedStaticPath(), | |
184 | createdAt: videoPlaylist.createdAt.toISOString() | |
185 | } | |
186 | ||
8d987ec6 | 187 | const list = { |
6fad8e51 | 188 | numberOfItems: videoPlaylist.get('videosLength') as number |
8d987ec6 K |
189 | } |
190 | ||
191 | const ogType = 'video' | |
6fad8e51 | 192 | const twitterCard = CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary' |
8d987ec6 K |
193 | const schemaType = 'ItemList' |
194 | ||
106fa224 | 195 | customHtml = ClientHtml.addTags(customHtml, { |
a59f210f C |
196 | url, |
197 | originUrl, | |
55cb8bc7 C |
198 | escapedSiteName: escapeHTML(siteName), |
199 | escapedTitle: escapeHTML(title), | |
200 | escapedDescription: escapeHTML(description), | |
a59f210f | 201 | embed, |
a59f210f C |
202 | image, |
203 | list, | |
204 | ogType, | |
205 | twitterCard, | |
206 | schemaType | |
106fa224 | 207 | }) |
92bf2f62 C |
208 | |
209 | return customHtml | |
210 | } | |
211 | ||
212 | static async getAccountHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) { | |
9a911038 K |
213 | const accountModelPromise = AccountModel.loadByNameWithHost(nameWithHost) |
214 | return this.getAccountOrChannelHTMLPage(() => accountModelPromise, req, res) | |
92bf2f62 C |
215 | } |
216 | ||
217 | static async getVideoChannelHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) { | |
9a911038 K |
218 | const videoChannelModelPromise = VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithHost) |
219 | return this.getAccountOrChannelHTMLPage(() => videoChannelModelPromise, req, res) | |
220 | } | |
221 | ||
222 | static async getActorHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) { | |
012580d9 C |
223 | const [ account, channel ] = await Promise.all([ |
224 | AccountModel.loadByNameWithHost(nameWithHost), | |
225 | VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithHost) | |
226 | ]) | |
9a911038 | 227 | |
012580d9 | 228 | return this.getAccountOrChannelHTMLPage(() => Promise.resolve(account || channel), req, res) |
92bf2f62 C |
229 | } |
230 | ||
cf649c2e C |
231 | static async getEmbedHTML () { |
232 | const path = ClientHtml.getEmbedPath() | |
cf649c2e | 233 | |
b9da21bd | 234 | if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path] |
cf649c2e C |
235 | |
236 | const buffer = await readFile(path) | |
2539932e | 237 | const serverConfig = await ServerConfigManager.Instance.getHTMLServerConfig() |
cf649c2e C |
238 | |
239 | let html = buffer.toString() | |
240 | html = await ClientHtml.addAsyncPluginCSS(html) | |
2564d97e | 241 | html = ClientHtml.addCustomCSS(html) |
915e2bbb | 242 | html = ClientHtml.addTitleTag(html) |
aea0b0e7 C |
243 | html = ClientHtml.addDescriptionTag(html) |
244 | html = ClientHtml.addServerConfig(html, serverConfig) | |
cf649c2e C |
245 | |
246 | ClientHtml.htmlCache[path] = html | |
247 | ||
248 | return html | |
249 | } | |
250 | ||
92bf2f62 | 251 | private static async getAccountOrChannelHTMLPage ( |
b49f22d8 | 252 | loader: () => Promise<MAccountActor | MChannelActor>, |
92bf2f62 C |
253 | req: express.Request, |
254 | res: express.Response | |
255 | ) { | |
256 | const [ html, entity ] = await Promise.all([ | |
257 | ClientHtml.getIndexHTML(req, res), | |
258 | loader() | |
259 | ]) | |
260 | ||
261 | // Let Angular application handle errors | |
262 | if (!entity) { | |
2d53be02 | 263 | res.status(HttpStatusCode.NOT_FOUND_404) |
92bf2f62 C |
264 | return ClientHtml.getIndexHTML(req, res) |
265 | } | |
266 | ||
c68e2b2d | 267 | const description = mdToOneLinePlainText(entity.description) |
55cb8bc7 C |
268 | |
269 | let customHtml = ClientHtml.addTitleTag(html, entity.getDisplayName()) | |
270 | customHtml = ClientHtml.addDescriptionTag(customHtml, description) | |
8d987ec6 | 271 | |
a59f210f | 272 | const url = entity.getLocalUrl() |
106fa224 | 273 | const originUrl = entity.Actor.url |
55cb8bc7 C |
274 | const siteName = CONFIG.INSTANCE.NAME |
275 | const title = entity.getDisplayName() | |
8d987ec6 | 276 | |
d0800f76 | 277 | const avatar = getBiggestActorImage(entity.Actor.Avatars) |
8d987ec6 | 278 | const image = { |
d0800f76 | 279 | url: ActorImageModel.getImageUrl(avatar), |
280 | width: avatar?.width, | |
281 | height: avatar?.height | |
8d987ec6 K |
282 | } |
283 | ||
284 | const ogType = 'website' | |
285 | const twitterCard = 'summary' | |
286 | const schemaType = 'ProfilePage' | |
287 | ||
a59f210f C |
288 | customHtml = ClientHtml.addTags(customHtml, { |
289 | url, | |
290 | originUrl, | |
55cb8bc7 C |
291 | escapedTitle: escapeHTML(title), |
292 | escapedSiteName: escapeHTML(siteName), | |
293 | escapedDescription: escapeHTML(description), | |
a59f210f C |
294 | image, |
295 | ogType, | |
296 | twitterCard, | |
352819ef C |
297 | schemaType, |
298 | disallowIndexation: !entity.Actor.isOwned() | |
a59f210f | 299 | }) |
9aac4423 C |
300 | |
301 | return customHtml | |
302 | } | |
303 | ||
304 | private static async getIndexHTML (req: express.Request, res: express.Response, paramLang?: string) { | |
305 | const path = ClientHtml.getIndexPath(req, res, paramLang) | |
b9da21bd | 306 | if (!isTestInstance() && ClientHtml.htmlCache[path]) return ClientHtml.htmlCache[path] |
9aac4423 C |
307 | |
308 | const buffer = await readFile(path) | |
2539932e | 309 | const serverConfig = await ServerConfigManager.Instance.getHTMLServerConfig() |
9aac4423 C |
310 | |
311 | let html = buffer.toString() | |
312 | ||
caf2aaf4 K |
313 | html = ClientHtml.addManifestContentHash(html) |
314 | html = ClientHtml.addFaviconContentHash(html) | |
315 | html = ClientHtml.addLogoContentHash(html) | |
9aac4423 | 316 | html = ClientHtml.addCustomCSS(html) |
aea0b0e7 | 317 | html = ClientHtml.addServerConfig(html, serverConfig) |
a8b666e9 | 318 | html = await ClientHtml.addAsyncPluginCSS(html) |
9aac4423 | 319 | |
a1587156 | 320 | ClientHtml.htmlCache[path] = html |
9aac4423 C |
321 | |
322 | return html | |
e032aec9 C |
323 | } |
324 | ||
7738273b | 325 | private static getIndexPath (req: express.Request, res: express.Response, paramLang: string) { |
e032aec9 C |
326 | let lang: string |
327 | ||
328 | // Check param lang validity | |
329 | if (paramLang && is18nLocale(paramLang)) { | |
330 | lang = paramLang | |
331 | ||
332 | // Save locale in cookies | |
333 | res.cookie('clientLanguage', lang, { | |
6dd9de95 | 334 | secure: WEBSERVER.SCHEME === 'https', |
bc90883f | 335 | sameSite: 'none', |
e032aec9 C |
336 | maxAge: 1000 * 3600 * 24 * 90 // 3 months |
337 | }) | |
338 | ||
339 | } else if (req.cookies.clientLanguage && is18nLocale(req.cookies.clientLanguage)) { | |
340 | lang = req.cookies.clientLanguage | |
341 | } else { | |
342 | lang = req.acceptsLanguages(POSSIBLE_LOCALES) || getDefaultLocale() | |
343 | } | |
344 | ||
450de91e C |
345 | logger.debug( |
346 | 'Serving %s HTML language', buildFileLocale(lang), | |
347 | { cookie: req.cookies?.clientLanguage, paramLang, acceptLanguage: req.headers['accept-language'] } | |
348 | ) | |
349 | ||
f8360396 | 350 | return join(root(), 'client', 'dist', buildFileLocale(lang), 'index.html') |
e032aec9 C |
351 | } |
352 | ||
cf649c2e | 353 | private static getEmbedPath () { |
f8360396 | 354 | return join(root(), 'client', 'dist', 'standalone', 'videos', 'embed.html') |
cf649c2e C |
355 | } |
356 | ||
caf2aaf4 K |
357 | private static addManifestContentHash (htmlStringPage: string) { |
358 | return htmlStringPage.replace('[manifestContentHash]', FILES_CONTENT_HASH.MANIFEST) | |
359 | } | |
360 | ||
1fdf8edb | 361 | private static addFaviconContentHash (htmlStringPage: string) { |
caf2aaf4 K |
362 | return htmlStringPage.replace('[faviconContentHash]', FILES_CONTENT_HASH.FAVICON) |
363 | } | |
364 | ||
1fdf8edb | 365 | private static addLogoContentHash (htmlStringPage: string) { |
caf2aaf4 K |
366 | return htmlStringPage.replace('[logoContentHash]', FILES_CONTENT_HASH.LOGO) |
367 | } | |
368 | ||
9aac4423 C |
369 | private static addTitleTag (htmlStringPage: string, title?: string) { |
370 | let text = title || CONFIG.INSTANCE.NAME | |
371 | if (title) text += ` - ${CONFIG.INSTANCE.NAME}` | |
372 | ||
55cb8bc7 | 373 | const titleTag = `<title>${escapeHTML(text)}</title>` |
e032aec9 C |
374 | |
375 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.TITLE, titleTag) | |
376 | } | |
377 | ||
9aac4423 C |
378 | private static addDescriptionTag (htmlStringPage: string, description?: string) { |
379 | const content = description || CONFIG.INSTANCE.SHORT_DESCRIPTION | |
55cb8bc7 | 380 | const descriptionTag = `<meta name="description" content="${escapeHTML(content)}" />` |
e032aec9 C |
381 | |
382 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.DESCRIPTION, descriptionTag) | |
383 | } | |
384 | ||
385 | private static addCustomCSS (htmlStringPage: string) { | |
ffb321be | 386 | const styleTag = `<style class="custom-css-style">${CONFIG.INSTANCE.CUSTOMIZATIONS.CSS}</style>` |
e032aec9 C |
387 | |
388 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.CUSTOM_CSS, styleTag) | |
389 | } | |
390 | ||
aea0b0e7 | 391 | private static addServerConfig (htmlStringPage: string, serverConfig: HTMLServerConfig) { |
b4c945f3 C |
392 | // Stringify the JSON object, and then stringify the string object so we can inject it into the HTML |
393 | const serverConfigString = JSON.stringify(JSON.stringify(serverConfig)) | |
394 | const configScriptTag = `<script type="application/javascript">window.PeerTubeServerConfig = ${serverConfigString}</script>` | |
aea0b0e7 C |
395 | |
396 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.SERVER_CONFIG, configScriptTag) | |
397 | } | |
398 | ||
a8b666e9 C |
399 | private static async addAsyncPluginCSS (htmlStringPage: string) { |
400 | const globalCSSContent = await readFile(PLUGIN_GLOBAL_CSS_PATH) | |
3e753302 | 401 | if (globalCSSContent.byteLength === 0) return htmlStringPage |
a8b666e9 C |
402 | |
403 | const fileHash = sha256(globalCSSContent) | |
404 | const linkTag = `<link rel="stylesheet" href="/plugins/global.css?hash=${fileHash}" />` | |
ffb321be C |
405 | |
406 | return htmlStringPage.replace('</head>', linkTag + '</head>') | |
407 | } | |
408 | ||
6fad8e51 | 409 | private static generateOpenGraphMetaTags (tags: Tags) { |
8d987ec6 K |
410 | const metaTags = { |
411 | 'og:type': tags.ogType, | |
55cb8bc7 C |
412 | 'og:site_name': tags.escapedSiteName, |
413 | 'og:title': tags.escapedTitle, | |
8d987ec6 K |
414 | 'og:image': tags.image.url |
415 | } | |
416 | ||
417 | if (tags.image.width && tags.image.height) { | |
418 | metaTags['og:image:width'] = tags.image.width | |
419 | metaTags['og:image:height'] = tags.image.height | |
420 | } | |
e032aec9 | 421 | |
8d987ec6 | 422 | metaTags['og:url'] = tags.url |
55cb8bc7 | 423 | metaTags['og:description'] = tags.escapedDescription |
e032aec9 | 424 | |
8d987ec6 K |
425 | if (tags.embed) { |
426 | metaTags['og:video:url'] = tags.embed.url | |
427 | metaTags['og:video:secure_url'] = tags.embed.url | |
428 | metaTags['og:video:type'] = 'text/html' | |
429 | metaTags['og:video:width'] = EMBED_SIZE.width | |
430 | metaTags['og:video:height'] = EMBED_SIZE.height | |
431 | } | |
e032aec9 | 432 | |
8d987ec6 K |
433 | return metaTags |
434 | } | |
e032aec9 | 435 | |
6fad8e51 | 436 | private static generateStandardMetaTags (tags: Tags) { |
8d987ec6 | 437 | return { |
55cb8bc7 C |
438 | name: tags.escapedTitle, |
439 | description: tags.escapedDescription, | |
8d987ec6 K |
440 | image: tags.image.url |
441 | } | |
442 | } | |
e032aec9 | 443 | |
6fad8e51 | 444 | private static generateTwitterCardMetaTags (tags: Tags) { |
8d987ec6 K |
445 | const metaTags = { |
446 | 'twitter:card': tags.twitterCard, | |
e032aec9 | 447 | 'twitter:site': CONFIG.SERVICES.TWITTER.USERNAME, |
55cb8bc7 C |
448 | 'twitter:title': tags.escapedTitle, |
449 | 'twitter:description': tags.escapedDescription, | |
8d987ec6 | 450 | 'twitter:image': tags.image.url |
e032aec9 C |
451 | } |
452 | ||
8d987ec6 K |
453 | if (tags.image.width && tags.image.height) { |
454 | metaTags['twitter:image:width'] = tags.image.width | |
455 | metaTags['twitter:image:height'] = tags.image.height | |
456 | } | |
457 | ||
b96777c3 C |
458 | if (tags.twitterCard === 'player') { |
459 | metaTags['twitter:player'] = tags.embed.url | |
460 | metaTags['twitter:player:width'] = EMBED_SIZE.width | |
461 | metaTags['twitter:player:height'] = EMBED_SIZE.height | |
462 | } | |
463 | ||
8d987ec6 K |
464 | return metaTags |
465 | } | |
e032aec9 | 466 | |
6fad8e51 | 467 | private static generateSchemaTags (tags: Tags) { |
8d987ec6 | 468 | const schema = { |
e032aec9 | 469 | '@context': 'http://schema.org', |
8d987ec6 | 470 | '@type': tags.schemaType, |
55cb8bc7 C |
471 | 'name': tags.escapedTitle, |
472 | 'description': tags.escapedDescription, | |
8d987ec6 K |
473 | 'image': tags.image.url, |
474 | 'url': tags.url | |
475 | } | |
476 | ||
477 | if (tags.list) { | |
478 | schema['numberOfItems'] = tags.list.numberOfItems | |
479 | schema['thumbnailUrl'] = tags.image.url | |
480 | } | |
481 | ||
482 | if (tags.embed) { | |
483 | schema['embedUrl'] = tags.embed.url | |
484 | schema['uploadDate'] = tags.embed.createdAt | |
6fad8e51 C |
485 | |
486 | if (tags.embed.duration) schema['duration'] = tags.embed.duration | |
487 | if (tags.embed.views) schema['iterationCount'] = tags.embed.views | |
488 | ||
8d987ec6 K |
489 | schema['thumbnailUrl'] = tags.image.url |
490 | schema['contentUrl'] = tags.url | |
491 | } | |
492 | ||
493 | return schema | |
494 | } | |
495 | ||
6fad8e51 | 496 | private static addTags (htmlStringPage: string, tagsValues: Tags) { |
8d987ec6 K |
497 | const openGraphMetaTags = this.generateOpenGraphMetaTags(tagsValues) |
498 | const standardMetaTags = this.generateStandardMetaTags(tagsValues) | |
499 | const twitterCardMetaTags = this.generateTwitterCardMetaTags(tagsValues) | |
500 | const schemaTags = this.generateSchemaTags(tagsValues) | |
501 | ||
55cb8bc7 | 502 | const { url, escapedTitle, embed, originUrl, disallowIndexation } = tagsValues |
8d987ec6 | 503 | |
55cb8bc7 | 504 | const oembedLinkTags: { type: string, href: string, escapedTitle: string }[] = [] |
8d987ec6 K |
505 | |
506 | if (embed) { | |
507 | oembedLinkTags.push({ | |
508 | type: 'application/json+oembed', | |
509 | href: WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(url), | |
55cb8bc7 | 510 | escapedTitle |
8d987ec6 | 511 | }) |
e032aec9 C |
512 | } |
513 | ||
55cb8bc7 | 514 | let tagsStr = '' |
e032aec9 C |
515 | |
516 | // Opengraph | |
517 | Object.keys(openGraphMetaTags).forEach(tagName => { | |
a1587156 | 518 | const tagValue = openGraphMetaTags[tagName] |
e032aec9 | 519 | |
55cb8bc7 | 520 | tagsStr += `<meta property="${tagName}" content="${tagValue}" />` |
e032aec9 C |
521 | }) |
522 | ||
8d987ec6 K |
523 | // Standard |
524 | Object.keys(standardMetaTags).forEach(tagName => { | |
525 | const tagValue = standardMetaTags[tagName] | |
526 | ||
55cb8bc7 | 527 | tagsStr += `<meta property="${tagName}" content="${tagValue}" />` |
8d987ec6 K |
528 | }) |
529 | ||
530 | // Twitter card | |
531 | Object.keys(twitterCardMetaTags).forEach(tagName => { | |
532 | const tagValue = twitterCardMetaTags[tagName] | |
533 | ||
55cb8bc7 | 534 | tagsStr += `<meta property="${tagName}" content="${tagValue}" />` |
8d987ec6 K |
535 | }) |
536 | ||
e032aec9 C |
537 | // OEmbed |
538 | for (const oembedLinkTag of oembedLinkTags) { | |
55cb8bc7 | 539 | tagsStr += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.escapedTitle}" />` |
e032aec9 C |
540 | } |
541 | ||
542 | // Schema.org | |
8d987ec6 | 543 | if (schemaTags) { |
55cb8bc7 | 544 | tagsStr += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>` |
8d987ec6 | 545 | } |
92bf2f62 | 546 | |
8d987ec6 | 547 | // SEO, use origin URL |
55cb8bc7 | 548 | tagsStr += `<link rel="canonical" href="${originUrl}" />` |
92bf2f62 | 549 | |
352819ef | 550 | if (disallowIndexation) { |
55cb8bc7 | 551 | tagsStr += `<meta name="robots" content="noindex" />` |
352819ef C |
552 | } |
553 | ||
55cb8bc7 | 554 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.META_TAGS, tagsStr) |
e032aec9 C |
555 | } |
556 | } | |
f2eb23cd | 557 | |
5fc44b57 | 558 | function sendHTML (html: string, res: express.Response, localizedHTML: boolean = false) { |
f2eb23cd RK |
559 | res.set('Content-Type', 'text/html; charset=UTF-8') |
560 | ||
5fc44b57 | 561 | if (localizedHTML) { |
562 | res.set('Vary', 'Accept-Language') | |
563 | } | |
564 | ||
f2eb23cd RK |
565 | return res.send(html) |
566 | } | |
567 | ||
568 | async function serveIndexHTML (req: express.Request, res: express.Response) { | |
8bd67ef6 | 569 | if (req.accepts(ACCEPT_HEADERS) === 'html' || !req.headers.accept) { |
f2eb23cd RK |
570 | try { |
571 | await generateHTMLPage(req, res, req.params.language) | |
572 | return | |
573 | } catch (err) { | |
574 | logger.error('Cannot generate HTML page.', err) | |
76148b27 | 575 | return res.status(HttpStatusCode.INTERNAL_SERVER_ERROR_500).end() |
f2eb23cd RK |
576 | } |
577 | } | |
578 | ||
76148b27 | 579 | return res.status(HttpStatusCode.NOT_ACCEPTABLE_406).end() |
f2eb23cd RK |
580 | } |
581 | ||
582 | // --------------------------------------------------------------------------- | |
583 | ||
584 | export { | |
585 | ClientHtml, | |
586 | sendHTML, | |
587 | serveIndexHTML | |
588 | } | |
589 | ||
590 | async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) { | |
591 | const html = await ClientHtml.getDefaultHTMLPage(req, res, paramLang) | |
592 | ||
5fc44b57 | 593 | return sendHTML(html, res, true) |
f2eb23cd | 594 | } |