1 import * as express from 'express'
2 import { join } from 'path'
3 import * as validator from 'validator'
4 import * as Promise from 'bluebird'
6 import { database as db } from '../initializers/database'
12 } from '../initializers'
13 import { root, readFileBufferPromise } from '../helpers'
14 import { VideoInstance } from '../models'
16 const clientsRouter = express.Router()
18 const distPath = join(root(), 'client', 'dist')
19 const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
20 const indexPath = join(distPath, 'index.html')
22 // Special route that add OpenGraph tags
23 // Do not use a template engine for a so little thing
24 clientsRouter.use('/videos/watch/:id', generateWatchHtmlPage)
26 clientsRouter.use('/videos/embed', (req: express.Request, res: express.Response, next: express.NextFunction) => {
27 res.sendFile(embedPath)
30 // Static HTML/CSS/JS client files
31 clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
33 // 404 for static files not found
34 clientsRouter.use('/client/*', (req: express.Request, res: express.Response, next: express.NextFunction) => {
38 // ---------------------------------------------------------------------------
44 // ---------------------------------------------------------------------------
46 function addOpenGraphTags (htmlStringPage: string, video: VideoInstance) {
47 const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName()
48 const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.id
52 'og:title': video.name,
53 'og:image': previewUrl,
55 'og:description': video.description,
58 'description': video.description,
61 'twitter:card': 'summary_large_image',
62 'twitter:site': '@Chocobozzz',
63 'twitter:title': video.name,
64 'twitter:description': video.description,
65 'twitter:image': previewUrl
69 Object.keys(metaTags).forEach(tagName => {
70 const tagValue = metaTags[tagName]
72 tagsString += '<meta property="' + tagName + '" content="' + tagValue + '" />'
75 return htmlStringPage.replace(OPENGRAPH_COMMENT, tagsString)
78 function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
79 const videoId = '' + req.params.id
80 let videoPromise: Promise<VideoInstance>
82 // Let Angular application handle errors
83 if (validator.isUUID(videoId, 4)) {
84 videoPromise = db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(videoId)
85 } else if (validator.isInt(videoId)) {
86 videoPromise = db.Video.loadAndPopulateAuthorAndPodAndTags(+videoId)
88 return res.sendFile(indexPath)
92 readFileBufferPromise(indexPath),
95 .then(([ file, video ]) => {
97 video = video as VideoInstance
99 const html = file.toString()
101 // Let Angular application handle errors
102 if (!video) return res.sendFile(indexPath)
104 const htmlStringPageWithTags = addOpenGraphTags(html, video)
105 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
107 .catch(err => next(err))