572db6133f99e6ce68f38e91913789559fafa2a5
[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 distPath = path.join(__dirname, '../../client/dist')
17 const embedPath = path.join(distPath, 'standalone/videos/embed.html')
18 const indexPath = path.join(distPath, 'index.html')
19
20 // Special route that add OpenGraph tags
21 // Do not use a template engine for a so little thing
22 router.use('/videos/watch/:id', generateWatchHtmlPage)
23
24 router.use('/videos/embed', function (req, res, next) {
25   res.sendFile(embedPath)
26 })
27
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
36 // ---------------------------------------------------------------------------
37
38 module.exports = router
39
40 // ---------------------------------------------------------------------------
41
42 function addOpenGraphTags (htmlStringPage, video) {
43   let basePreviewUrlHttp
44
45   if (video.isOwned()) {
46     basePreviewUrlHttp = constants.CONFIG.WEBSERVER.URL
47   } else {
48     basePreviewUrlHttp = constants.REMOTE_SCHEME.HTTP + '://' + video.podHost
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)
54   const previewUrl = basePreviewUrlHttp + constants.STATIC_PATHS.PREVIEWS + video.getPreviewName()
55   const videoUrl = constants.CONFIG.WEBSERVER.URL + '/videos/watch/' + video._id
56
57   const metaTags = {
58     'og:type': 'video',
59     'og:title': video.name,
60     'og:image': previewUrl,
61     'og:url': videoUrl,
62     'og:description': video.description,
63
64     'name': video.name,
65     'description': video.description,
66     'image': previewUrl,
67
68     'twitter:card': 'summary_large_image',
69     'twitter:site': '@Chocobozzz',
70     'twitter:title': video.name,
71     'twitter:description': video.description,
72     'twitter:image': previewUrl
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) {
86   const videoId = req.params.id
87
88   // Let Angular application handle errors
89   if (!validator.isMongoId(videoId)) return res.sendFile(indexPath)
90
91   parallel({
92     file: function (callback) {
93       fs.readFile(indexPath, callback)
94     },
95
96     video: function (callback) {
97       Video.load(videoId, callback)
98     }
99   }, function (err, results) {
100     if (err) return next(err)
101
102     const html = results.file.toString()
103     const video = results.video
104
105     // Let Angular application handle errors
106     if (!video) return res.sendFile(indexPath)
107
108     const htmlStringPageWithTags = addOpenGraphTags(html, video)
109     res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
110   })
111 }