3 const parallel = require('async/parallel')
4 const express = require('express')
5 const fs = require('fs')
6 const path = require('path')
7 const validator = require('express-validator').validator
9 const constants = require('../initializers/constants')
10 const db = require('../initializers/database')
12 const router = express.Router()
14 const opengraphComment = '<!-- opengraph tags -->'
15 const distPath = path.join(__dirname, '..', '..', 'client/dist')
16 const embedPath = path.join(distPath, 'standalone/videos/embed.html')
17 const indexPath = path.join(distPath, '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 // Static HTML/CSS/JS client files
28 router.use('/client', express.static(distPath, { maxAge: constants.STATIC_MAX_AGE }))
30 // 404 for static files not found
31 router.use('/client/*', function (req, res, next) {
35 // ---------------------------------------------------------------------------
37 module.exports = router
39 // ---------------------------------------------------------------------------
41 function addOpenGraphTags (htmlStringPage, video) {
42 let basePreviewUrlHttp
44 if (video.isOwned()) {
45 basePreviewUrlHttp = constants.CONFIG.WEBSERVER.URL
47 basePreviewUrlHttp = constants.REMOTE_SCHEME.HTTP + '://' + video.Author.Pod.host
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)
53 const previewUrl = basePreviewUrlHttp + constants.STATIC_PATHS.PREVIEWS + video.getPreviewName()
54 const videoUrl = constants.CONFIG.WEBSERVER.URL + '/videos/watch/' + video.id
58 'og:title': video.name,
59 'og:image': previewUrl,
61 'og:description': video.description,
64 'description': video.description,
67 'twitter:card': 'summary_large_image',
68 'twitter:site': '@Chocobozzz',
69 'twitter:title': video.name,
70 'twitter:description': video.description,
71 'twitter:image': previewUrl
75 Object.keys(metaTags).forEach(function (tagName) {
76 const tagValue = metaTags[tagName]
78 tagsString += '<meta property="' + tagName + '" content="' + tagValue + '" />'
81 return htmlStringPage.replace(opengraphComment, tagsString)
84 function generateWatchHtmlPage (req, res, next) {
85 const videoId = req.params.id
87 // Let Angular application handle errors
88 if (!validator.isUUID(videoId, 4)) return res.sendFile(indexPath)
91 file: function (callback) {
92 fs.readFile(indexPath, callback)
95 video: function (callback) {
96 db.Video.loadAndPopulateAuthorAndPodAndTags(videoId, callback)
98 }, function (err, results) {
99 if (err) return next(err)
101 const html = results.file.toString()
102 const video = results.video
104 // Let Angular application handle errors
105 if (!video) return res.sendFile(indexPath)
107 const htmlStringPageWithTags = addOpenGraphTags(html, video)
108 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)