]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/client.js
Fix opengraph url tag
[github/Chocobozzz/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 let basePreviewUrlHttp
35
36 if (video.isOwned()) {
37 basePreviewUrlHttp = constants.CONFIG.WEBSERVER.URL
38 } else {
39 basePreviewUrlHttp = constants.REMOTE_SCHEME.HTTP + '://' + video.podHost
40 }
41
42 // We fetch the remote preview (bigger than the thumbnail)
43 // This should not overhead the remote server since social websites put in a cache the OpenGraph tags
44 // We can't use the thumbnail because these social websites want bigger images (> 200x200 for Facebook for example)
45 const previewUrl = basePreviewUrlHttp + constants.STATIC_PATHS.PREVIEWS + video.getPreviewName()
46 const videoUrl = constants.CONFIG.WEBSERVER.URL + '/videos/watch/' + video._id
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 }