e3251d7e97be9cd5a124afb96ae10a5eed54bf4a
[oweals/peertube.git] / server / controllers / client.js
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 const validator = require('express-validator').validator
9
10 const constants = require('../initializers/constants')
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
21 router.use('/videos/watch/:id', generateWatchHtmlPage)
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) {
34   const videoUrl = constants.CONFIG.WEBSERVER.URL + '/videos/watch/'
35   let baseUrlHttp
36
37   if (video.isOwned()) {
38     baseUrlHttp = constants.CONFIG.WEBSERVER.URL
39   } else {
40     baseUrlHttp = constants.REMOTE_SCHEME.HTTP + '://' + video.podHost
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()
47
48   const metaTags = {
49     'og:type': 'video',
50     'og:title': video.name,
51     'og:image': previewUrl,
52     'og:url': videoUrl,
53     'og:description': video.description,
54
55     'name': video.name,
56     'description': video.description,
57     'image': previewUrl,
58
59     'twitter:card': 'summary_large_image',
60     'twitter:site': '@Chocobozzz',
61     'twitter:title': video.name,
62     'twitter:description': video.description,
63     'twitter:image': previewUrl
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) {
77   const videoId = req.params.id
78
79   // Let Angular application handle errors
80   if (!validator.isMongoId(videoId)) return res.sendFile(indexPath)
81
82   parallel({
83     file: function (callback) {
84       fs.readFile(indexPath, callback)
85     },
86
87     video: function (callback) {
88       Video.load(videoId, callback)
89     }
90   }, function (err, results) {
91     if (err) return next(err)
92
93     const html = results.file.toString()
94     const video = results.video
95
96     // Let Angular application handle errors
97     if (!video) return res.sendFile(indexPath)
98
99     const htmlStringPageWithTags = addOpenGraphTags(html, video)
100     res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
101   })
102 }