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