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