]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/client.js
Server: generate magnet uri on the fly
[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')
8
9const constants = require('../initializers/constants')
10const middlewares = require('../middlewares')
11const validators = middlewares.validators
12const validatorsVideos = validators.videos
13
14const Video = mongoose.model('Video')
15const router = express.Router()
16
17const opengraphComment = '<!-- opengraph tags -->'
18const embedPath = path.join(__dirname, '../../client/dist/standalone/videos/embed.html')
19const indexPath = path.join(__dirname, '../../client/dist/index.html')
20
21// Special route that add OpenGraph tags
22// Do not use a template engine for a so little thing
23router.use('/videos/watch/:id', validatorsVideos.videosGet, generateWatchHtmlPage)
24
25router.use('/videos/embed', function (req, res, next) {
26 res.sendFile(embedPath)
27})
28
29// ---------------------------------------------------------------------------
30
31module.exports = router
32
33// ---------------------------------------------------------------------------
34
35function addOpenGraphTags (htmlStringPage, video) {
36 const thumbnailUrl = constants.CONFIG.WEBSERVER.URL + video.thumbnailPath
37 const videoUrl = constants.CONFIG.WEBSERVER.URL + '/videos/watch/'
38
39 const metaTags = {
40 'og:type': 'video',
41 'og:title': video.name,
42 'og:image': thumbnailUrl,
43 'og:url': videoUrl,
44 'og:description': video.description,
45
46 'name': video.name,
47 'description': video.description,
48 'image': thumbnailUrl,
49
50 'twitter:card': 'summary_large_image',
51 'twitter:site': '@Chocobozzz',
52 'twitter:title': video.name,
53 'twitter:description': video.description,
54 'twitter:image': thumbnailUrl
55 }
56
57 let tagsString = ''
58 Object.keys(metaTags).forEach(function (tagName) {
59 const tagValue = metaTags[tagName]
60
61 tagsString += '<meta property="' + tagName + '" content="' + tagValue + '" />'
62 })
63
64 return htmlStringPage.replace(opengraphComment, tagsString)
65}
66
67function generateWatchHtmlPage (req, res, next) {
68 parallel({
69 file: function (callback) {
70 fs.readFile(indexPath, callback)
71 },
72
73 video: function (callback) {
74 Video.load(req.params.id, callback)
75 }
76 }, function (err, results) {
77 if (err) return next(err)
78
79 const html = results.file.toString()
80 const video = results.video.toFormatedJSON()
81
82 const htmlStringPageWithTags = addOpenGraphTags(html, video)
83 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
84 })
85}