]>
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')
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 distPath
= path
.join(__dirname
, '../../client/dist')
17 const embedPath
= path
.join(distPath
, 'standalone/videos/embed.html')
18 const indexPath
= path
.join(distPath
, 'index.html')
20 // Special route that add OpenGraph tags
21 // Do not use a template engine for a so little thing
22 router
.use('/videos/watch/:id', generateWatchHtmlPage
)
24 router
.use('/videos/embed', function (req
, res
, next
) {
25 res
.sendFile(embedPath
)
28 // Static HTML/CSS/JS client files
29 router
.use('/client', express
.static(distPath
, { maxAge: constants
.STATIC_MAX_AGE
}))
31 // 404 for static files not found
32 router
.use('/client/*', function (req
, res
, next
) {
36 // ---------------------------------------------------------------------------
38 module
.exports
= router
40 // ---------------------------------------------------------------------------
42 function addOpenGraphTags (htmlStringPage
, video
) {
43 let basePreviewUrlHttp
45 if (video
.isOwned()) {
46 basePreviewUrlHttp
= constants
.CONFIG
.WEBSERVER
.URL
48 basePreviewUrlHttp
= constants
.REMOTE_SCHEME
.HTTP
+ '://' + video
.podHost
51 // We fetch the remote preview (bigger than the thumbnail)
52 // This should not overhead the remote server since social websites put in a cache the OpenGraph tags
53 // We can't use the thumbnail because these social websites want bigger images (> 200x200 for Facebook for example)
54 const previewUrl
= basePreviewUrlHttp
+ constants
.STATIC_PATHS
.PREVIEWS
+ video
.getPreviewName()
55 const videoUrl
= constants
.CONFIG
.WEBSERVER
.URL
+ '/videos/watch/' + video
._id
59 'og:title': video
.name
,
60 'og:image': previewUrl
,
62 'og:description': video
.description
,
65 'description': video
.description
,
68 'twitter:card': 'summary_large_image',
69 'twitter:site': '@Chocobozzz',
70 'twitter:title': video
.name
,
71 'twitter:description': video
.description
,
72 'twitter:image': previewUrl
76 Object
.keys(metaTags
).forEach(function (tagName
) {
77 const tagValue
= metaTags
[tagName
]
79 tagsString
+= '<meta property="' + tagName
+ '" content="' + tagValue
+ '" />'
82 return htmlStringPage
.replace(opengraphComment
, tagsString
)
85 function generateWatchHtmlPage (req
, res
, next
) {
86 const videoId
= req
.params
.id
88 // Let Angular application handle errors
89 if (!validator
.isMongoId(videoId
)) return res
.sendFile(indexPath
)
92 file: function (callback
) {
93 fs
.readFile(indexPath
, callback
)
96 video: function (callback
) {
97 Video
.load(videoId
, callback
)
99 }, function (err
, results
) {
100 if (err
) return next(err
)
102 const html
= results
.file
.toString()
103 const video
= results
.video
105 // Let Angular application handle errors
106 if (!video
) return res
.sendFile(indexPath
)
108 const htmlStringPageWithTags
= addOpenGraphTags(html
, video
)
109 res
.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags
)