]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/client.js
Server: do not break remote videos processing on error
[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')
830bcd0f 6const path = require('path')
73ce7f96 7const validator = require('express-validator').validator
830bcd0f
C
8
9const constants = require('../initializers/constants')
feb4bdfd 10const db = require('../initializers/database')
830bcd0f 11
830bcd0f
C
12const router = express.Router()
13
14const opengraphComment = '<!-- opengraph tags -->'
79530164
C
15const distPath = path.join(__dirname, '../../client/dist')
16const embedPath = path.join(distPath, 'standalone/videos/embed.html')
17const indexPath = path.join(distPath, 'index.html')
830bcd0f
C
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
79530164
C
27// Static HTML/CSS/JS client files
28router.use('/client', express.static(distPath, { maxAge: constants.STATIC_MAX_AGE }))
29
30// 404 for static files not found
31router.use('/client/*', function (req, res, next) {
32 res.sendStatus(404)
33})
34
830bcd0f
C
35// ---------------------------------------------------------------------------
36
37module.exports = router
38
39// ---------------------------------------------------------------------------
40
41function addOpenGraphTags (htmlStringPage, video) {
40e3f5e1 42 let basePreviewUrlHttp
41b5da1d
C
43
44 if (video.isOwned()) {
40e3f5e1 45 basePreviewUrlHttp = constants.CONFIG.WEBSERVER.URL
41b5da1d 46 } else {
feb4bdfd 47 basePreviewUrlHttp = constants.REMOTE_SCHEME.HTTP + '://' + video.Author.Pod.host
41b5da1d
C
48 }
49
50 // We fetch the remote preview (bigger than the thumbnail)
51 // This should not overhead the remote server since social websites put in a cache the OpenGraph tags
52 // We can't use the thumbnail because these social websites want bigger images (> 200x200 for Facebook for example)
40e3f5e1 53 const previewUrl = basePreviewUrlHttp + constants.STATIC_PATHS.PREVIEWS + video.getPreviewName()
feb4bdfd 54 const videoUrl = constants.CONFIG.WEBSERVER.URL + '/videos/watch/' + video.id
830bcd0f
C
55
56 const metaTags = {
57 'og:type': 'video',
58 'og:title': video.name,
41b5da1d 59 'og:image': previewUrl,
830bcd0f
C
60 'og:url': videoUrl,
61 'og:description': video.description,
62
63 'name': video.name,
64 'description': video.description,
41b5da1d 65 'image': previewUrl,
830bcd0f
C
66
67 'twitter:card': 'summary_large_image',
68 'twitter:site': '@Chocobozzz',
69 'twitter:title': video.name,
70 'twitter:description': video.description,
41b5da1d 71 'twitter:image': previewUrl
830bcd0f
C
72 }
73
74 let tagsString = ''
75 Object.keys(metaTags).forEach(function (tagName) {
76 const tagValue = metaTags[tagName]
77
78 tagsString += '<meta property="' + tagName + '" content="' + tagValue + '" />'
79 })
80
81 return htmlStringPage.replace(opengraphComment, tagsString)
82}
83
84function generateWatchHtmlPage (req, res, next) {
73ce7f96
C
85 const videoId = req.params.id
86
87 // Let Angular application handle errors
feb4bdfd 88 if (!validator.isUUID(videoId, 4)) return res.sendFile(indexPath)
73ce7f96 89
830bcd0f
C
90 parallel({
91 file: function (callback) {
92 fs.readFile(indexPath, callback)
93 },
94
95 video: function (callback) {
7920c273 96 db.Video.loadAndPopulateAuthorAndPodAndTags(videoId, callback)
830bcd0f
C
97 }
98 }, function (err, results) {
99 if (err) return next(err)
100
101 const html = results.file.toString()
2550fab3 102 const video = results.video
830bcd0f 103
73ce7f96
C
104 // Let Angular application handle errors
105 if (!video) return res.sendFile(indexPath)
106
830bcd0f
C
107 const htmlStringPageWithTags = addOpenGraphTags(html, video)
108 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
109 })
110}