]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/client.js
e3251d7e97be9cd5a124afb96ae10a5eed54bf4a
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
10 const constants
= require('../initializers/constants')
12 const Video
= mongoose
.model('Video')
13 const router
= express
.Router()
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')
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
)
23 router
.use('/videos/embed', function (req
, res
, next
) {
24 res
.sendFile(embedPath
)
27 // ---------------------------------------------------------------------------
29 module
.exports
= router
31 // ---------------------------------------------------------------------------
33 function addOpenGraphTags (htmlStringPage
, video
) {
34 const videoUrl
= constants
.CONFIG
.WEBSERVER
.URL
+ '/videos/watch/'
37 if (video
.isOwned()) {
38 baseUrlHttp
= constants
.CONFIG
.WEBSERVER
.URL
40 baseUrlHttp
= constants
.REMOTE_SCHEME
.HTTP
+ '://' + video
.podHost
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()
50 'og:title': video
.name
,
51 'og:image': previewUrl
,
53 'og:description': video
.description
,
56 'description': video
.description
,
59 'twitter:card': 'summary_large_image',
60 'twitter:site': '@Chocobozzz',
61 'twitter:title': video
.name
,
62 'twitter:description': video
.description
,
63 'twitter:image': previewUrl
67 Object
.keys(metaTags
).forEach(function (tagName
) {
68 const tagValue
= metaTags
[tagName
]
70 tagsString
+= '<meta property="' + tagName
+ '" content="' + tagValue
+ '" />'
73 return htmlStringPage
.replace(opengraphComment
, tagsString
)
76 function generateWatchHtmlPage (req
, res
, next
) {
77 const videoId
= req
.params
.id
79 // Let Angular application handle errors
80 if (!validator
.isMongoId(videoId
)) return res
.sendFile(indexPath
)
83 file: function (callback
) {
84 fs
.readFile(indexPath
, callback
)
87 video: function (callback
) {
88 Video
.load(videoId
, callback
)
90 }, function (err
, results
) {
91 if (err
) return next(err
)
93 const html
= results
.file
.toString()
94 const video
= results
.video
96 // Let Angular application handle errors
97 if (!video
) return res
.sendFile(indexPath
)
99 const htmlStringPageWithTags
= addOpenGraphTags(html
, video
)
100 res
.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags
)