]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/client.js
Fix opengraph url tag
[github/Chocobozzz/PeerTube.git] / server / controllers / client.js
CommitLineData
830bcd0f
C
1'use strict'
2
3const parallel = require('async/parallel')
4const express = require('express')
5const fs = require('fs')
6const mongoose = require('mongoose')
7const path = require('path')
73ce7f96 8const validator = require('express-validator').validator
830bcd0f
C
9
10const constants = require('../initializers/constants')
830bcd0f
C
11
12const Video = mongoose.model('Video')
13const router = express.Router()
14
15const opengraphComment = '<!-- opengraph tags -->'
16const embedPath = path.join(__dirname, '../../client/dist/standalone/videos/embed.html')
17const 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
73ce7f96 21router.use('/videos/watch/:id', generateWatchHtmlPage)
830bcd0f
C
22
23router.use('/videos/embed', function (req, res, next) {
24 res.sendFile(embedPath)
25})
26
27// ---------------------------------------------------------------------------
28
29module.exports = router
30
31// ---------------------------------------------------------------------------
32
33function addOpenGraphTags (htmlStringPage, video) {
40e3f5e1 34 let basePreviewUrlHttp
41b5da1d
C
35
36 if (video.isOwned()) {
40e3f5e1 37 basePreviewUrlHttp = constants.CONFIG.WEBSERVER.URL
41b5da1d 38 } else {
40e3f5e1 39 basePreviewUrlHttp = constants.REMOTE_SCHEME.HTTP + '://' + video.podHost
41b5da1d
C
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)
40e3f5e1
C
45 const previewUrl = basePreviewUrlHttp + constants.STATIC_PATHS.PREVIEWS + video.getPreviewName()
46 const videoUrl = constants.CONFIG.WEBSERVER.URL + '/videos/watch/' + video._id
830bcd0f
C
47
48 const metaTags = {
49 'og:type': 'video',
50 'og:title': video.name,
41b5da1d 51 'og:image': previewUrl,
830bcd0f
C
52 'og:url': videoUrl,
53 'og:description': video.description,
54
55 'name': video.name,
56 'description': video.description,
41b5da1d 57 'image': previewUrl,
830bcd0f
C
58
59 'twitter:card': 'summary_large_image',
60 'twitter:site': '@Chocobozzz',
61 'twitter:title': video.name,
62 'twitter:description': video.description,
41b5da1d 63 'twitter:image': previewUrl
830bcd0f
C
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
76function generateWatchHtmlPage (req, res, next) {
73ce7f96
C
77 const videoId = req.params.id
78
79 // Let Angular application handle errors
80 if (!validator.isMongoId(videoId)) return res.sendFile(indexPath)
81
830bcd0f
C
82 parallel({
83 file: function (callback) {
84 fs.readFile(indexPath, callback)
85 },
86
87 video: function (callback) {
73ce7f96 88 Video.load(videoId, callback)
830bcd0f
C
89 }
90 }, function (err, results) {
91 if (err) return next(err)
92
93 const html = results.file.toString()
2550fab3 94 const video = results.video
830bcd0f 95
73ce7f96
C
96 // Let Angular application handle errors
97 if (!video) return res.sendFile(indexPath)
98
830bcd0f
C
99 const htmlStringPageWithTags = addOpenGraphTags(html, video)
100 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
101 })
102}