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