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