1 import { parallel } from 'async'
2 import express = require('express')
3 import fs = require('fs')
4 import { join } from 'path'
5 import expressValidator = require('express-validator')
6 // TODO: use .validator when express-validator typing will have validator field
7 const validator = expressValidator['validator']
9 const db = require('../initializers/database')
15 } from '../initializers'
17 const clientsRouter = express.Router()
19 // TODO: move to constants
20 const opengraphComment = '<!-- opengraph tags -->'
21 const distPath = join(__dirname, '..', '..', 'client/dist')
22 const embedPath = join(distPath, 'standalone/videos/embed.html')
23 const indexPath = join(distPath, 'index.html')
25 // Special route that add OpenGraph tags
26 // Do not use a template engine for a so little thing
27 clientsRouter.use('/videos/watch/:id', generateWatchHtmlPage)
29 clientsRouter.use('/videos/embed', function (req, res, next) {
30 res.sendFile(embedPath)
33 // Static HTML/CSS/JS client files
34 clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
36 // 404 for static files not found
37 clientsRouter.use('/client/*', function (req, res, next) {
41 // ---------------------------------------------------------------------------
47 // ---------------------------------------------------------------------------
49 function addOpenGraphTags (htmlStringPage, video) {
50 let basePreviewUrlHttp
52 if (video.isOwned()) {
53 basePreviewUrlHttp = CONFIG.WEBSERVER.URL
55 basePreviewUrlHttp = REMOTE_SCHEME.HTTP + '://' + video.Author.Pod.host
58 // We fetch the remote preview (bigger than the thumbnail)
59 // This should not overhead the remote server since social websites put in a cache the OpenGraph tags
60 // We can't use the thumbnail because these social websites want bigger images (> 200x200 for Facebook for example)
61 const previewUrl = basePreviewUrlHttp + STATIC_PATHS.PREVIEWS + video.getPreviewName()
62 const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.id
66 'og:title': video.name,
67 'og:image': previewUrl,
69 'og:description': video.description,
72 'description': video.description,
75 'twitter:card': 'summary_large_image',
76 'twitter:site': '@Chocobozzz',
77 'twitter:title': video.name,
78 'twitter:description': video.description,
79 'twitter:image': previewUrl
83 Object.keys(metaTags).forEach(function (tagName) {
84 const tagValue = metaTags[tagName]
86 tagsString += '<meta property="' + tagName + '" content="' + tagValue + '" />'
89 return htmlStringPage.replace(opengraphComment, tagsString)
92 function generateWatchHtmlPage (req, res, next) {
93 const videoId = req.params.id
95 // Let Angular application handle errors
96 if (!validator.isUUID(videoId, 4)) return res.sendFile(indexPath)
99 file: function (callback) {
100 fs.readFile(indexPath, callback)
103 video: function (callback) {
104 db.Video.loadAndPopulateAuthorAndPodAndTags(videoId, callback)
106 }, function (err, result: any) {
107 if (err) return next(err)
109 const html = result.file.toString()
110 const video = result.video
112 // Let Angular application handle errors
113 if (!video) return res.sendFile(indexPath)
115 const htmlStringPageWithTags = addOpenGraphTags(html, video)
116 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)