]>
Commit | Line | Data |
---|---|---|
da854ddd | 1 | import * as Bluebird from 'bluebird' |
4d4e5cd4 | 2 | import * as express from 'express' |
d00e2393 | 3 | import * as helmet from 'helmet' |
65fcc311 | 4 | import { join } from 'path' |
4d4e5cd4 | 5 | import * as validator from 'validator' |
da854ddd | 6 | import { escapeHTML, readFileBufferPromise, root } from '../helpers/core-utils' |
74b7c6d4 | 7 | import { ACCEPT_HEADERS, CONFIG, EMBED_SIZE, OPENGRAPH_AND_OEMBED_COMMENT, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers' |
eb080476 | 8 | import { asyncMiddleware } from '../middlewares' |
3fd3ab2d | 9 | import { VideoModel } from '../models/video/video' |
a00a8f09 | 10 | import { VideoPrivacy } from '../../shared/models/videos' |
8afc19a6 C |
11 | import { |
12 | buildFileLocale, | |
13 | getCompleteLocale, | |
14 | getDefaultLocale, | |
15 | is18nLocale, | |
16 | LOCALE_FILES, | |
17 | POSSIBLE_LOCALES | |
18 | } from '../../shared/models/i18n/i18n' | |
830bcd0f | 19 | |
65fcc311 | 20 | const clientsRouter = express.Router() |
830bcd0f | 21 | |
e02643f3 | 22 | const distPath = join(root(), 'client', 'dist') |
1f30a185 | 23 | const assetsImagesPath = join(root(), 'client', 'dist', 'assets', 'images') |
e02643f3 | 24 | const embedPath = join(distPath, 'standalone', 'videos', 'embed.html') |
99941732 | 25 | const testEmbedPath = join(distPath, 'standalone', 'videos', 'test-embed.html') |
830bcd0f | 26 | |
d8755eed | 27 | // Special route that add OpenGraph and oEmbed tags |
830bcd0f | 28 | // Do not use a template engine for a so little thing |
eb080476 C |
29 | clientsRouter.use('/videos/watch/:id', |
30 | asyncMiddleware(generateWatchHtmlPage) | |
31 | ) | |
830bcd0f | 32 | |
8afc19a6 | 33 | clientsRouter.use('' + |
d00e2393 RK |
34 | '/videos/embed', |
35 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
36 | res.removeHeader('X-Frame-Options') | |
37 | res.sendFile(embedPath) | |
38 | } | |
39 | ) | |
99941732 WL |
40 | clientsRouter.use('' + |
41 | '/videos/test-embed', (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
42 | res.sendFile(testEmbedPath) | |
43 | }) | |
830bcd0f | 44 | |
79530164 | 45 | // Static HTML/CSS/JS client files |
78967fca C |
46 | |
47 | const staticClientFiles = [ | |
48 | 'manifest.json', | |
49 | 'ngsw-worker.js', | |
50 | 'ngsw.json' | |
51 | ] | |
52 | for (const staticClientFile of staticClientFiles) { | |
53 | const path = join(root(), 'client', 'dist', staticClientFile) | |
54 | clientsRouter.use('/' + staticClientFile, express.static(path, { maxAge: STATIC_MAX_AGE })) | |
55 | } | |
56 | ||
65fcc311 | 57 | clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE })) |
6bafac54 | 58 | clientsRouter.use('/client/assets/images', express.static(assetsImagesPath, { maxAge: STATIC_MAX_AGE })) |
79530164 | 59 | |
e945b184 | 60 | clientsRouter.use('/client/locales/:locale/:file.json', function (req, res) { |
7ce44a74 C |
61 | const locale = req.params.locale |
62 | const file = req.params.file | |
63 | ||
74b7c6d4 C |
64 | if (is18nLocale(locale) && LOCALE_FILES.indexOf(file) !== -1) { |
65 | const completeLocale = getCompleteLocale(locale) | |
66 | const completeFileLocale = buildFileLocale(completeLocale) | |
67 | return res.sendFile(join(__dirname, `../../../client/dist/locale/${file}_${completeFileLocale}.json`)) | |
e945b184 C |
68 | } |
69 | ||
70 | return res.sendStatus(404) | |
71 | }) | |
72 | ||
79530164 | 73 | // 404 for static files not found |
075f16ca | 74 | clientsRouter.use('/client/*', (req: express.Request, res: express.Response, next: express.NextFunction) => { |
79530164 C |
75 | res.sendStatus(404) |
76 | }) | |
77 | ||
989e526a C |
78 | // Always serve index client page (the client is a single page application, let it handle routing) |
79 | // Try to provide the right language index.html | |
80 | clientsRouter.use('/(:language)?', function (req, res) { | |
81 | if (req.accepts(ACCEPT_HEADERS) === 'html') { | |
8afc19a6 | 82 | return res.sendFile(getIndexPath(req, res, req.params.language)) |
989e526a C |
83 | } |
84 | ||
85 | return res.status(404).end() | |
86 | }) | |
87 | ||
830bcd0f C |
88 | // --------------------------------------------------------------------------- |
89 | ||
65fcc311 C |
90 | export { |
91 | clientsRouter | |
92 | } | |
830bcd0f C |
93 | |
94 | // --------------------------------------------------------------------------- | |
95 | ||
8afc19a6 | 96 | function getIndexPath (req: express.Request, res: express.Response, paramLang?: string) { |
989e526a C |
97 | let lang: string |
98 | ||
99 | // Check param lang validity | |
100 | if (paramLang && is18nLocale(paramLang)) { | |
101 | lang = paramLang | |
8afc19a6 C |
102 | |
103 | // Save locale in cookies | |
104 | res.cookie('clientLanguage', lang, { | |
105 | secure: CONFIG.WEBSERVER.SCHEME === 'https', | |
106 | sameSite: true, | |
107 | maxAge: 1000 * 3600 * 24 * 90 // 3 months | |
108 | }) | |
109 | ||
110 | } else if (req.cookies.clientLanguage && is18nLocale(req.cookies.clientLanguage)) { | |
111 | lang = req.cookies.clientLanguage | |
989e526a | 112 | } else { |
8afc19a6 | 113 | lang = req.acceptsLanguages(POSSIBLE_LOCALES) || getDefaultLocale() |
989e526a C |
114 | } |
115 | ||
74b7c6d4 | 116 | return join(__dirname, '../../../client/dist/' + buildFileLocale(lang) + '/index.html') |
989e526a C |
117 | } |
118 | ||
3fd3ab2d | 119 | function addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoModel) { |
d38309c3 | 120 | const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName() |
d8755eed | 121 | const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid |
830bcd0f | 122 | |
a434c465 C |
123 | const videoNameEscaped = escapeHTML(video.name) |
124 | const videoDescriptionEscaped = escapeHTML(video.description) | |
40e87e9e | 125 | const embedUrl = CONFIG.WEBSERVER.URL + video.getEmbedStaticPath() |
49347a0a | 126 | |
d8755eed | 127 | const openGraphMetaTags = { |
830bcd0f | 128 | 'og:type': 'video', |
a434c465 | 129 | 'og:title': videoNameEscaped, |
41b5da1d | 130 | 'og:image': previewUrl, |
830bcd0f | 131 | 'og:url': videoUrl, |
a434c465 | 132 | 'og:description': videoDescriptionEscaped, |
830bcd0f | 133 | |
7ff7802a C |
134 | 'og:video:url': embedUrl, |
135 | 'og:video:secure_url': embedUrl, | |
136 | 'og:video:type': 'text/html', | |
137 | 'og:video:width': EMBED_SIZE.width, | |
138 | 'og:video:height': EMBED_SIZE.height, | |
139 | ||
a434c465 C |
140 | 'name': videoNameEscaped, |
141 | 'description': videoDescriptionEscaped, | |
41b5da1d | 142 | 'image': previewUrl, |
830bcd0f | 143 | |
8be1afa1 C |
144 | 'twitter:card': CONFIG.SERVICES.TWITTER.WHITELISTED ? 'player' : 'summary_large_image', |
145 | 'twitter:site': CONFIG.SERVICES.TWITTER.USERNAME, | |
a434c465 C |
146 | 'twitter:title': videoNameEscaped, |
147 | 'twitter:description': videoDescriptionEscaped, | |
7ff7802a C |
148 | 'twitter:image': previewUrl, |
149 | 'twitter:player': embedUrl, | |
150 | 'twitter:player:width': EMBED_SIZE.width, | |
151 | 'twitter:player:height': EMBED_SIZE.height | |
830bcd0f C |
152 | } |
153 | ||
d8755eed C |
154 | const oembedLinkTags = [ |
155 | { | |
156 | type: 'application/json+oembed', | |
157 | href: CONFIG.WEBSERVER.URL + '/services/oembed?url=' + encodeURIComponent(videoUrl), | |
a434c465 | 158 | title: videoNameEscaped |
d8755eed C |
159 | } |
160 | ] | |
161 | ||
093237cf | 162 | const schemaTags = { |
c7b1b92b C |
163 | '@context': 'http://schema.org', |
164 | '@type': 'VideoObject', | |
093237cf C |
165 | name: videoNameEscaped, |
166 | description: videoDescriptionEscaped, | |
acbffe9c C |
167 | thumbnailUrl: previewUrl, |
168 | uploadDate: video.createdAt.toISOString(), | |
093237cf | 169 | duration: video.getActivityStreamDuration(), |
acbffe9c C |
170 | contentUrl: videoUrl, |
171 | embedUrl: embedUrl, | |
172 | interactionCount: video.views | |
093237cf C |
173 | } |
174 | ||
830bcd0f | 175 | let tagsString = '' |
c7b1b92b C |
176 | |
177 | // Opengraph | |
d8755eed C |
178 | Object.keys(openGraphMetaTags).forEach(tagName => { |
179 | const tagValue = openGraphMetaTags[tagName] | |
830bcd0f | 180 | |
d8755eed | 181 | tagsString += `<meta property="${tagName}" content="${tagValue}" />` |
830bcd0f C |
182 | }) |
183 | ||
c7b1b92b | 184 | // OEmbed |
d8755eed C |
185 | for (const oembedLinkTag of oembedLinkTags) { |
186 | tagsString += `<link rel="alternate" type="${oembedLinkTag.type}" href="${oembedLinkTag.href}" title="${oembedLinkTag.title}" />` | |
187 | } | |
188 | ||
c7b1b92b C |
189 | // Schema.org |
190 | tagsString += `<script type="application/ld+json">${JSON.stringify(schemaTags)}</script>` | |
093237cf | 191 | |
acbffe9c C |
192 | // SEO |
193 | tagsString += `<link rel="canonical" href="${videoUrl}" />` | |
194 | ||
d8755eed | 195 | return htmlStringPage.replace(OPENGRAPH_AND_OEMBED_COMMENT, tagsString) |
830bcd0f C |
196 | } |
197 | ||
eb080476 | 198 | async function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) { |
69818c93 | 199 | const videoId = '' + req.params.id |
3fd3ab2d | 200 | let videoPromise: Bluebird<VideoModel> |
73ce7f96 C |
201 | |
202 | // Let Angular application handle errors | |
0a6658fd | 203 | if (validator.isUUID(videoId, 4)) { |
3fd3ab2d | 204 | videoPromise = VideoModel.loadByUUIDAndPopulateAccountAndServerAndTags(videoId) |
0a6658fd | 205 | } else if (validator.isInt(videoId)) { |
3fd3ab2d | 206 | videoPromise = VideoModel.loadAndPopulateAccountAndServerAndTags(+videoId) |
0a6658fd | 207 | } else { |
8afc19a6 | 208 | return res.sendFile(getIndexPath(req, res)) |
0a6658fd | 209 | } |
73ce7f96 | 210 | |
eb080476 | 211 | let [ file, video ] = await Promise.all([ |
8afc19a6 | 212 | readFileBufferPromise(getIndexPath(req, res)), |
0a6658fd | 213 | videoPromise |
6fcd19ba | 214 | ]) |
830bcd0f | 215 | |
eb080476 | 216 | const html = file.toString() |
73ce7f96 | 217 | |
eb080476 | 218 | // Let Angular application handle errors |
8afc19a6 | 219 | if (!video || video.privacy === VideoPrivacy.PRIVATE) return res.sendFile(getIndexPath(req, res)) |
eb080476 C |
220 | |
221 | const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video) | |
222 | res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags) | |
830bcd0f | 223 | } |