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