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