]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/client.js
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')
9 const constants
= require('../initializers/constants')
10 const middlewares
= require('../middlewares')
11 const validators
= middlewares
.validators
12 const validatorsVideos
= validators
.videos
14 const Video
= mongoose
.model('Video')
15 const router
= express
.Router()
17 const opengraphComment
= '<!-- opengraph tags -->'
18 const embedPath
= path
.join(__dirname
, '../../client/dist/standalone/videos/embed.html')
19 const indexPath
= path
.join(__dirname
, '../../client/dist/index.html')
21 // Special route that add OpenGraph tags
22 // Do not use a template engine for a so little thing
23 router
.use('/videos/watch/:id', validatorsVideos
.videosGet
, generateWatchHtmlPage
)
25 router
.use('/videos/embed', function (req
, res
, next
) {
26 res
.sendFile(embedPath
)
29 // ---------------------------------------------------------------------------
31 module
.exports
= router
33 // ---------------------------------------------------------------------------
35 function addOpenGraphTags (htmlStringPage
, video
) {
36 const videoUrl
= constants
.CONFIG
.WEBSERVER
.URL
+ '/videos/watch/'
39 if (video
.isOwned()) {
40 baseUrlHttp
= constants
.CONFIG
.WEBSERVER
.URL
42 baseUrlHttp
= constants
.REMOTE_SCHEME
.HTTP
+ '://' + video
.podUrl
45 // We fetch the remote preview (bigger than the thumbnail)
46 // This should not overhead the remote server since social websites put in a cache the OpenGraph tags
47 // We can't use the thumbnail because these social websites want bigger images (> 200x200 for Facebook for example)
48 const previewUrl
= baseUrlHttp
+ constants
.STATIC_PATHS
.PREVIEWS
+ video
.getPreviewName()
52 'og:title': video
.name
,
53 'og:image': previewUrl
,
55 'og:description': video
.description
,
58 'description': video
.description
,
61 'twitter:card': 'summary_large_image',
62 'twitter:site': '@Chocobozzz',
63 'twitter:title': video
.name
,
64 'twitter:description': video
.description
,
65 'twitter:image': previewUrl
69 Object
.keys(metaTags
).forEach(function (tagName
) {
70 const tagValue
= metaTags
[tagName
]
72 tagsString
+= '<meta property="' + tagName
+ '" content="' + tagValue
+ '" />'
75 return htmlStringPage
.replace(opengraphComment
, tagsString
)
78 function generateWatchHtmlPage (req
, res
, next
) {
80 file: function (callback
) {
81 fs
.readFile(indexPath
, callback
)
84 video: function (callback
) {
85 Video
.load(req
.params
.id
, callback
)
87 }, function (err
, results
) {
88 if (err
) return next(err
)
90 const html
= results
.file
.toString()
91 const video
= results
.video
.toFormatedJSON()
93 const htmlStringPageWithTags
= addOpenGraphTags(html
, video
)
94 res
.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags
)