]>
Commit | Line | Data |
---|---|---|
65fcc311 | 1 | import { parallel } from 'async' |
4d4e5cd4 C |
2 | import * as express from 'express' |
3 | import * as fs from 'fs' | |
65fcc311 | 4 | import { join } from 'path' |
4d4e5cd4 | 5 | import * as validator from 'validator' |
830bcd0f | 6 | |
e02643f3 | 7 | import { database as db } from '../initializers/database' |
65fcc311 C |
8 | import { |
9 | CONFIG, | |
10 | REMOTE_SCHEME, | |
11 | STATIC_PATHS, | |
12 | STATIC_MAX_AGE | |
13 | } from '../initializers' | |
e02643f3 | 14 | import { root } from '../helpers' |
830bcd0f | 15 | |
65fcc311 | 16 | const clientsRouter = express.Router() |
830bcd0f | 17 | |
65fcc311 | 18 | // TODO: move to constants |
830bcd0f | 19 | const opengraphComment = '<!-- opengraph tags -->' |
e02643f3 C |
20 | const distPath = join(root(), 'client', 'dist') |
21 | const embedPath = join(distPath, 'standalone', 'videos', 'embed.html') | |
65fcc311 | 22 | const indexPath = join(distPath, 'index.html') |
830bcd0f C |
23 | |
24 | // Special route that add OpenGraph tags | |
25 | // Do not use a template engine for a so little thing | |
65fcc311 | 26 | clientsRouter.use('/videos/watch/:id', generateWatchHtmlPage) |
830bcd0f | 27 | |
65fcc311 | 28 | clientsRouter.use('/videos/embed', function (req, res, next) { |
830bcd0f C |
29 | res.sendFile(embedPath) |
30 | }) | |
31 | ||
79530164 | 32 | // Static HTML/CSS/JS client files |
65fcc311 | 33 | clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE })) |
79530164 C |
34 | |
35 | // 404 for static files not found | |
65fcc311 | 36 | clientsRouter.use('/client/*', function (req, res, next) { |
79530164 C |
37 | res.sendStatus(404) |
38 | }) | |
39 | ||
830bcd0f C |
40 | // --------------------------------------------------------------------------- |
41 | ||
65fcc311 C |
42 | export { |
43 | clientsRouter | |
44 | } | |
830bcd0f C |
45 | |
46 | // --------------------------------------------------------------------------- | |
47 | ||
48 | function addOpenGraphTags (htmlStringPage, video) { | |
40e3f5e1 | 49 | let basePreviewUrlHttp |
41b5da1d C |
50 | |
51 | if (video.isOwned()) { | |
65fcc311 | 52 | basePreviewUrlHttp = CONFIG.WEBSERVER.URL |
41b5da1d | 53 | } else { |
65fcc311 | 54 | basePreviewUrlHttp = REMOTE_SCHEME.HTTP + '://' + video.Author.Pod.host |
41b5da1d C |
55 | } |
56 | ||
57 | // We fetch the remote preview (bigger than the thumbnail) | |
58 | // This should not overhead the remote server since social websites put in a cache the OpenGraph tags | |
59 | // We can't use the thumbnail because these social websites want bigger images (> 200x200 for Facebook for example) | |
65fcc311 C |
60 | const previewUrl = basePreviewUrlHttp + STATIC_PATHS.PREVIEWS + video.getPreviewName() |
61 | const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.id | |
830bcd0f C |
62 | |
63 | const metaTags = { | |
64 | 'og:type': 'video', | |
65 | 'og:title': video.name, | |
41b5da1d | 66 | 'og:image': previewUrl, |
830bcd0f C |
67 | 'og:url': videoUrl, |
68 | 'og:description': video.description, | |
69 | ||
70 | 'name': video.name, | |
71 | 'description': video.description, | |
41b5da1d | 72 | 'image': previewUrl, |
830bcd0f C |
73 | |
74 | 'twitter:card': 'summary_large_image', | |
75 | 'twitter:site': '@Chocobozzz', | |
76 | 'twitter:title': video.name, | |
77 | 'twitter:description': video.description, | |
41b5da1d | 78 | 'twitter:image': previewUrl |
830bcd0f C |
79 | } |
80 | ||
81 | let tagsString = '' | |
82 | Object.keys(metaTags).forEach(function (tagName) { | |
83 | const tagValue = metaTags[tagName] | |
84 | ||
85 | tagsString += '<meta property="' + tagName + '" content="' + tagValue + '" />' | |
86 | }) | |
87 | ||
88 | return htmlStringPage.replace(opengraphComment, tagsString) | |
89 | } | |
90 | ||
91 | function generateWatchHtmlPage (req, res, next) { | |
73ce7f96 C |
92 | const videoId = req.params.id |
93 | ||
94 | // Let Angular application handle errors | |
feb4bdfd | 95 | if (!validator.isUUID(videoId, 4)) return res.sendFile(indexPath) |
73ce7f96 | 96 | |
830bcd0f C |
97 | parallel({ |
98 | file: function (callback) { | |
99 | fs.readFile(indexPath, callback) | |
100 | }, | |
101 | ||
102 | video: function (callback) { | |
7920c273 | 103 | db.Video.loadAndPopulateAuthorAndPodAndTags(videoId, callback) |
830bcd0f | 104 | } |
65fcc311 | 105 | }, function (err, result: any) { |
830bcd0f C |
106 | if (err) return next(err) |
107 | ||
65fcc311 C |
108 | const html = result.file.toString() |
109 | const video = result.video | |
830bcd0f | 110 | |
73ce7f96 C |
111 | // Let Angular application handle errors |
112 | if (!video) return res.sendFile(indexPath) | |
113 | ||
830bcd0f C |
114 | const htmlStringPageWithTags = addOpenGraphTags(html, video) |
115 | res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags) | |
116 | }) | |
117 | } |