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