]>
Commit | Line | Data |
---|---|---|
e032aec9 | 1 | import * as express from 'express' |
e032aec9 | 2 | import { buildFileLocale, getDefaultLocale, is18nLocale, POSSIBLE_LOCALES } from '../../shared/models/i18n/i18n' |
74dc3bca | 3 | import { CUSTOM_HTML_TAG_COMMENTS, EMBED_SIZE, WEBSERVER } from '../initializers/constants' |
e032aec9 | 4 | import { join } from 'path' |
62689b94 | 5 | import { escapeHTML } from '../helpers/core-utils' |
e032aec9 C |
6 | import { VideoModel } from '../models/video/video' |
7 | import * as validator from 'validator' | |
8 | import { VideoPrivacy } from '../../shared/models/videos' | |
62689b94 | 9 | import { readFile } from 'fs-extra' |
098eb377 | 10 | import { getActivityStreamDuration } from '../models/video/video-format-utils' |
92bf2f62 C |
11 | import { AccountModel } from '../models/account/account' |
12 | import { VideoChannelModel } from '../models/video/video-channel' | |
13 | import * as Bluebird from 'bluebird' | |
6dd9de95 | 14 | import { CONFIG } from '../initializers/config' |
e032aec9 C |
15 | |
16 | export class ClientHtml { | |
17 | ||
92bf2f62 | 18 | private static htmlCache: { [ path: string ]: string } = {} |
e032aec9 C |
19 | |
20 | static invalidCache () { | |
21 | ClientHtml.htmlCache = {} | |
22 | } | |
23 | ||
9aac4423 C |
24 | static async getDefaultHTMLPage (req: express.Request, res: express.Response, paramLang?: string) { |
25 | const html = await ClientHtml.getIndexHTML(req, res, paramLang) | |
e032aec9 | 26 | |
9aac4423 C |
27 | let customHtml = ClientHtml.addTitleTag(html) |
28 | customHtml = ClientHtml.addDescriptionTag(customHtml) | |
e032aec9 | 29 | |
9aac4423 | 30 | return customHtml |
e032aec9 C |
31 | } |
32 | ||
33 | static async getWatchHTMLPage (videoId: string, req: express.Request, res: express.Response) { | |
e032aec9 | 34 | // Let Angular application handle errors |
92bf2f62 | 35 | if (!validator.isInt(videoId) && !validator.isUUID(videoId, 4)) { |
e032aec9 C |
36 | return ClientHtml.getIndexHTML(req, res) |
37 | } | |
38 | ||
39 | const [ html, video ] = await Promise.all([ | |
40 | ClientHtml.getIndexHTML(req, res), | |
92bf2f62 | 41 | VideoModel.loadAndPopulateAccountAndServerAndTags(videoId) |
e032aec9 C |
42 | ]) |
43 | ||
44 | // Let Angular application handle errors | |
45 | if (!video || video.privacy === VideoPrivacy.PRIVATE) { | |
46 | return ClientHtml.getIndexHTML(req, res) | |
47 | } | |
48 | ||
9aac4423 C |
49 | let customHtml = ClientHtml.addTitleTag(html, escapeHTML(video.name)) |
50 | customHtml = ClientHtml.addDescriptionTag(customHtml, escapeHTML(video.description)) | |
92bf2f62 C |
51 | customHtml = ClientHtml.addVideoOpenGraphAndOEmbedTags(customHtml, video) |
52 | ||
53 | return customHtml | |
54 | } | |
55 | ||
56 | static async getAccountHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) { | |
57 | return this.getAccountOrChannelHTMLPage(() => AccountModel.loadByNameWithHost(nameWithHost), req, res) | |
58 | } | |
59 | ||
60 | static async getVideoChannelHTMLPage (nameWithHost: string, req: express.Request, res: express.Response) { | |
61 | return this.getAccountOrChannelHTMLPage(() => VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithHost), req, res) | |
62 | } | |
63 | ||
64 | private static async getAccountOrChannelHTMLPage ( | |
65 | loader: () => Bluebird<AccountModel | VideoChannelModel>, | |
66 | req: express.Request, | |
67 | res: express.Response | |
68 | ) { | |
69 | const [ html, entity ] = await Promise.all([ | |
70 | ClientHtml.getIndexHTML(req, res), | |
71 | loader() | |
72 | ]) | |
73 | ||
74 | // Let Angular application handle errors | |
75 | if (!entity) { | |
76 | return ClientHtml.getIndexHTML(req, res) | |
77 | } | |
78 | ||
79 | let customHtml = ClientHtml.addTitleTag(html, escapeHTML(entity.getDisplayName())) | |
80 | customHtml = ClientHtml.addDescriptionTag(customHtml, escapeHTML(entity.description)) | |
81 | customHtml = ClientHtml.addAccountOrChannelMetaTags(customHtml, entity) | |
9aac4423 C |
82 | |
83 | return customHtml | |
84 | } | |
85 | ||
86 | private static async getIndexHTML (req: express.Request, res: express.Response, paramLang?: string) { | |
87 | const path = ClientHtml.getIndexPath(req, res, paramLang) | |
92bf2f62 | 88 | if (ClientHtml.htmlCache[ path ]) return ClientHtml.htmlCache[ path ] |
9aac4423 C |
89 | |
90 | const buffer = await readFile(path) | |
91 | ||
92 | let html = buffer.toString() | |
93 | ||
94 | html = ClientHtml.addCustomCSS(html) | |
95 | ||
92bf2f62 | 96 | ClientHtml.htmlCache[ path ] = html |
9aac4423 C |
97 | |
98 | return html | |
e032aec9 C |
99 | } |
100 | ||
101 | private static getIndexPath (req: express.Request, res: express.Response, paramLang?: string) { | |
102 | let lang: string | |
103 | ||
104 | // Check param lang validity | |
105 | if (paramLang && is18nLocale(paramLang)) { | |
106 | lang = paramLang | |
107 | ||
108 | // Save locale in cookies | |
109 | res.cookie('clientLanguage', lang, { | |
6dd9de95 | 110 | secure: WEBSERVER.SCHEME === 'https', |
e032aec9 C |
111 | sameSite: true, |
112 | maxAge: 1000 * 3600 * 24 * 90 // 3 months | |
113 | }) | |
114 | ||
115 | } else if (req.cookies.clientLanguage && is18nLocale(req.cookies.clientLanguage)) { | |
116 | lang = req.cookies.clientLanguage | |
117 | } else { | |
118 | lang = req.acceptsLanguages(POSSIBLE_LOCALES) || getDefaultLocale() | |
119 | } | |
120 | ||
121 | return join(__dirname, '../../../client/dist/' + buildFileLocale(lang) + '/index.html') | |
122 | } | |
123 | ||
9aac4423 C |
124 | private static addTitleTag (htmlStringPage: string, title?: string) { |
125 | let text = title || CONFIG.INSTANCE.NAME | |
126 | if (title) text += ` - ${CONFIG.INSTANCE.NAME}` | |
127 | ||
128 | const titleTag = `<title>${text}</title>` | |
e032aec9 C |
129 | |
130 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.TITLE, titleTag) | |
131 | } | |
132 | ||
9aac4423 C |
133 | private static addDescriptionTag (htmlStringPage: string, description?: string) { |
134 | const content = description || CONFIG.INSTANCE.SHORT_DESCRIPTION | |
135 | const descriptionTag = `<meta name="description" content="${content}" />` | |
e032aec9 C |
136 | |
137 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.DESCRIPTION, descriptionTag) | |
138 | } | |
139 | ||
140 | private static addCustomCSS (htmlStringPage: string) { | |
141 | const styleTag = '<style class="custom-css-style">' + CONFIG.INSTANCE.CUSTOMIZATIONS.CSS + '</style>' | |
142 | ||
143 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.CUSTOM_CSS, styleTag) | |
144 | } | |
145 | ||
92bf2f62 | 146 | private static addVideoOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoModel) { |
6dd9de95 C |
147 | const previewUrl = WEBSERVER.URL + video.getPreviewStaticPath() |
148 | const videoUrl = WEBSERVER.URL + video.getWatchStaticPath() | |
e032aec9 C |
149 | |
150 | const videoNameEscaped = escapeHTML(video.name) | |
151 | const videoDescriptionEscaped = escapeHTML(video.description) | |
6dd9de95 | 152 | const embedUrl = WEBSERVER.URL + video.getEmbedStaticPath() |
e032aec9 C |
153 | |
154 | const openGraphMetaTags = { | |
155 | 'og:type': 'video', | |
156 | 'og:title': videoNameEscaped, | |
157 | 'og:image': previewUrl, | |
158 | 'og:url': videoUrl, | |
159 | 'og:description': videoDescriptionEscaped, | |
160 | ||
161 | 'og:video:url': embedUrl, | |
162 | 'og:video:secure_url': embedUrl, | |
fb651cf2 | 163 | 'og:video:type': 'text/html', |
e032aec9 C |
164 | 'og:video:width': EMBED_SIZE.width, |
165 | 'og:video:height': EMBED_SIZE.height, | |
166 | ||
167 | 'name': videoNameEscaped, | |
168 | 'description': videoDescriptionEscaped, | |
169 | 'image': previewUrl, | |
170 | ||
171 | 'twitter:card': CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary_large_image', | |
172 | 'twitter:site': CONFIG.SERVICES.TWITTER.USERNAME, | |
173 | 'twitter:title': videoNameEscaped, | |
174 | 'twitter:description': videoDescriptionEscaped, | |
175 | 'twitter:image': previewUrl, | |
176 | 'twitter:player': embedUrl, | |
177 | 'twitter:player:width': EMBED_SIZE.width, | |
178 | 'twitter:player:height': EMBED_SIZE.height | |
179 | } | |
180 | ||
181 | const oembedLinkTags = [ | |
182 | { | |
183 | type: 'application/json+oembed', | |
6dd9de95 | 184 | href: WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(videoUrl), |
e032aec9 C |
185 | title: videoNameEscaped |
186 | } | |
187 | ] | |
188 | ||
189 | const schemaTags = { | |
190 | '@context': 'http://schema.org', | |
191 | '@type': 'VideoObject', | |
192 | name: videoNameEscaped, | |
193 | description: videoDescriptionEscaped, | |
194 | thumbnailUrl: previewUrl, | |
195 | uploadDate: video.createdAt.toISOString(), | |
098eb377 | 196 | duration: getActivityStreamDuration(video.duration), |
e032aec9 C |
197 | contentUrl: videoUrl, |
198 | embedUrl: embedUrl, | |
199 | interactionCount: video.views | |
200 | } | |
201 | ||
202 | let tagsString = '' | |
203 | ||
204 | // Opengraph | |
205 | Object.keys(openGraphMetaTags).forEach(tagName => { | |
92bf2f62 | 206 | const tagValue = openGraphMetaTags[ tagName ] |
e032aec9 C |
207 | |
208 | tagsString += `<meta property="${tagName}" content="${tagValue}" />` | |
209 | }) | |
210 | ||
211 | // OEmbed | |
212 | for (const oembedLinkTag of oembedLinkTags) { | |
213 | tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />` | |
214 | } | |
215 | ||
216 | // Schema.org | |
217 | tagsString += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>` | |
218 | ||
c04eb647 C |
219 | // SEO, use origin video url so Google does not index remote videos |
220 | tagsString += `<link rel="canonical" href="${video.url}" />` | |
e032aec9 | 221 | |
92bf2f62 C |
222 | return this.addOpenGraphAndOEmbedTags(htmlStringPage, tagsString) |
223 | } | |
224 | ||
225 | private static addAccountOrChannelMetaTags (htmlStringPage: string, entity: AccountModel | VideoChannelModel) { | |
226 | // SEO, use origin account or channel URL | |
227 | const metaTags = `<link rel="canonical" href="${entity.Actor.url}" />` | |
228 | ||
229 | return this.addOpenGraphAndOEmbedTags(htmlStringPage, metaTags) | |
230 | } | |
231 | ||
232 | private static addOpenGraphAndOEmbedTags (htmlStringPage: string, metaTags: string) { | |
233 | return htmlStringPage.replace(CUSTOM_HTML_TAG_COMMENTS.META_TAGS, metaTags) | |
e032aec9 C |
234 | } |
235 | } |