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