]>
Commit | Line | Data |
---|---|---|
830bcd0f C |
1 | 'use strict' |
2 | ||
3 | const parallel = require('async/parallel') | |
4 | const express = require('express') | |
5 | const fs = require('fs') | |
6 | const mongoose = require('mongoose') | |
7 | const path = require('path') | |
73ce7f96 | 8 | const validator = require('express-validator').validator |
830bcd0f C |
9 | |
10 | const constants = require('../initializers/constants') | |
830bcd0f C |
11 | |
12 | const Video = mongoose.model('Video') | |
13 | const router = express.Router() | |
14 | ||
15 | const opengraphComment = '<!-- opengraph tags -->' | |
16 | const embedPath = path.join(__dirname, '../../client/dist/standalone/videos/embed.html') | |
17 | const indexPath = path.join(__dirname, '../../client/dist/index.html') | |
18 | ||
19 | // Special route that add OpenGraph tags | |
20 | // Do not use a template engine for a so little thing | |
73ce7f96 | 21 | router.use('/videos/watch/:id', generateWatchHtmlPage) |
830bcd0f C |
22 | |
23 | router.use('/videos/embed', function (req, res, next) { | |
24 | res.sendFile(embedPath) | |
25 | }) | |
26 | ||
27 | // --------------------------------------------------------------------------- | |
28 | ||
29 | module.exports = router | |
30 | ||
31 | // --------------------------------------------------------------------------- | |
32 | ||
33 | function addOpenGraphTags (htmlStringPage, video) { | |
830bcd0f | 34 | const videoUrl = constants.CONFIG.WEBSERVER.URL + '/videos/watch/' |
41b5da1d C |
35 | let baseUrlHttp |
36 | ||
37 | if (video.isOwned()) { | |
38 | baseUrlHttp = constants.CONFIG.WEBSERVER.URL | |
39 | } else { | |
2550fab3 | 40 | baseUrlHttp = constants.REMOTE_SCHEME.HTTP + '://' + video.podHost |
41b5da1d C |
41 | } |
42 | ||
43 | // We fetch the remote preview (bigger than the thumbnail) | |
44 | // This should not overhead the remote server since social websites put in a cache the OpenGraph tags | |
45 | // We can't use the thumbnail because these social websites want bigger images (> 200x200 for Facebook for example) | |
46 | const previewUrl = baseUrlHttp + constants.STATIC_PATHS.PREVIEWS + video.getPreviewName() | |
830bcd0f C |
47 | |
48 | const metaTags = { | |
49 | 'og:type': 'video', | |
50 | 'og:title': video.name, | |
41b5da1d | 51 | 'og:image': previewUrl, |
830bcd0f C |
52 | 'og:url': videoUrl, |
53 | 'og:description': video.description, | |
54 | ||
55 | 'name': video.name, | |
56 | 'description': video.description, | |
41b5da1d | 57 | 'image': previewUrl, |
830bcd0f C |
58 | |
59 | 'twitter:card': 'summary_large_image', | |
60 | 'twitter:site': '@Chocobozzz', | |
61 | 'twitter:title': video.name, | |
62 | 'twitter:description': video.description, | |
41b5da1d | 63 | 'twitter:image': previewUrl |
830bcd0f C |
64 | } |
65 | ||
66 | let tagsString = '' | |
67 | Object.keys(metaTags).forEach(function (tagName) { | |
68 | const tagValue = metaTags[tagName] | |
69 | ||
70 | tagsString += '<meta property="' + tagName + '" content="' + tagValue + '" />' | |
71 | }) | |
72 | ||
73 | return htmlStringPage.replace(opengraphComment, tagsString) | |
74 | } | |
75 | ||
76 | function generateWatchHtmlPage (req, res, next) { | |
73ce7f96 C |
77 | const videoId = req.params.id |
78 | ||
79 | // Let Angular application handle errors | |
80 | if (!validator.isMongoId(videoId)) return res.sendFile(indexPath) | |
81 | ||
830bcd0f C |
82 | parallel({ |
83 | file: function (callback) { | |
84 | fs.readFile(indexPath, callback) | |
85 | }, | |
86 | ||
87 | video: function (callback) { | |
73ce7f96 | 88 | Video.load(videoId, callback) |
830bcd0f C |
89 | } |
90 | }, function (err, results) { | |
91 | if (err) return next(err) | |
92 | ||
93 | const html = results.file.toString() | |
2550fab3 | 94 | const video = results.video |
830bcd0f | 95 | |
73ce7f96 C |
96 | // Let Angular application handle errors |
97 | if (!video) return res.sendFile(indexPath) | |
98 | ||
830bcd0f C |
99 | const htmlStringPageWithTags = addOpenGraphTags(html, video) |
100 | res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags) | |
101 | }) | |
102 | } |