1 import { parallel } from 'async'
2 import * as express from 'express'
3 import * as fs from 'fs'
4 import { join } from 'path'
5 import * as validator from 'validator'
7 import { database as db } from '../initializers/database'
13 } from '../initializers'
14 import { root } from '../helpers'
16 const clientsRouter = express.Router()
18 // TODO: move to constants
19 const opengraphComment = '<!-- opengraph tags -->'
20 const distPath = join(root(), 'client', 'dist')
21 const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
22 const indexPath = join(distPath, 'index.html')
24 // Special route that add OpenGraph tags
25 // Do not use a template engine for a so little thing
26 clientsRouter.use('/videos/watch/:id', generateWatchHtmlPage)
28 clientsRouter.use('/videos/embed', function (req, res, next) {
29 res.sendFile(embedPath)
32 // Static HTML/CSS/JS client files
33 clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
35 // 404 for static files not found
36 clientsRouter.use('/client/*', function (req, res, next) {
40 // ---------------------------------------------------------------------------
46 // ---------------------------------------------------------------------------
48 function addOpenGraphTags (htmlStringPage, video) {
49 let basePreviewUrlHttp
51 if (video.isOwned()) {
52 basePreviewUrlHttp = CONFIG.WEBSERVER.URL
54 basePreviewUrlHttp = REMOTE_SCHEME.HTTP + '://' + video.Author.Pod.host
57 // We fetch the remote preview (bigger than the thumbnail)
58 // This should not overhead the remote server since social websites put in a cache the OpenGraph tags
59 // We can't use the thumbnail because these social websites want bigger images (> 200x200 for Facebook for example)
60 const previewUrl = basePreviewUrlHttp + STATIC_PATHS.PREVIEWS + video.getPreviewName()
61 const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.id
65 'og:title': video.name,
66 'og:image': previewUrl,
68 'og:description': video.description,
71 'description': video.description,
74 'twitter:card': 'summary_large_image',
75 'twitter:site': '@Chocobozzz',
76 'twitter:title': video.name,
77 'twitter:description': video.description,
78 'twitter:image': previewUrl
82 Object.keys(metaTags).forEach(function (tagName) {
83 const tagValue = metaTags[tagName]
85 tagsString += '<meta property="' + tagName + '" content="' + tagValue + '" />'
88 return htmlStringPage.replace(opengraphComment, tagsString)
91 function generateWatchHtmlPage (req, res, next) {
92 const videoId = req.params.id
94 // Let Angular application handle errors
95 if (!validator.isUUID(videoId, 4)) return res.sendFile(indexPath)
98 file: function (callback) {
99 fs.readFile(indexPath, callback)
102 video: function (callback) {
103 db.Video.loadAndPopulateAuthorAndPodAndTags(videoId, callback)
105 }, function (err, result: any) {
106 if (err) return next(err)
108 const html = result.file.toString()
109 const video = result.video
111 // Let Angular application handle errors
112 if (!video) return res.sendFile(indexPath)
114 const htmlStringPageWithTags = addOpenGraphTags(html, video)
115 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)