1324e62cd607c6eae0d344533e65bd3d29b62289
[oweals/peertube.git] / server / controllers / client.ts
1 import * as express from 'express'
2 import { join } from 'path'
3 import * as validator from 'validator'
4 import * as Promise from 'bluebird'
5
6 import { database as db } from '../initializers/database'
7 import {
8   CONFIG,
9   REMOTE_SCHEME,
10   STATIC_PATHS,
11   STATIC_MAX_AGE,
12   OPENGRAPH_COMMENT
13 } from '../initializers'
14 import { root, readFileBufferPromise } from '../helpers'
15 import { VideoInstance } from '../models'
16
17 const clientsRouter = express.Router()
18
19 const distPath = join(root(), 'client', 'dist')
20 const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
21 const indexPath = join(distPath, 'index.html')
22
23 // Special route that add OpenGraph tags
24 // Do not use a template engine for a so little thing
25 clientsRouter.use('/videos/watch/:id', generateWatchHtmlPage)
26
27 clientsRouter.use('/videos/embed', (req: express.Request, res: express.Response, next: express.NextFunction) => {
28   res.sendFile(embedPath)
29 })
30
31 // Static HTML/CSS/JS client files
32 clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
33
34 // 404 for static files not found
35 clientsRouter.use('/client/*', (req: express.Request, res: express.Response, next: express.NextFunction) => {
36   res.sendStatus(404)
37 })
38
39 // ---------------------------------------------------------------------------
40
41 export {
42   clientsRouter
43 }
44
45 // ---------------------------------------------------------------------------
46
47 function addOpenGraphTags (htmlStringPage: string, video: VideoInstance) {
48   const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName()
49   const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.id
50
51   const metaTags = {
52     'og:type': 'video',
53     'og:title': video.name,
54     'og:image': previewUrl,
55     'og:url': videoUrl,
56     'og:description': video.description,
57
58     'name': video.name,
59     'description': video.description,
60     'image': previewUrl,
61
62     'twitter:card': 'summary_large_image',
63     'twitter:site': '@Chocobozzz',
64     'twitter:title': video.name,
65     'twitter:description': video.description,
66     'twitter:image': previewUrl
67   }
68
69   let tagsString = ''
70   Object.keys(metaTags).forEach(tagName => {
71     const tagValue = metaTags[tagName]
72
73     tagsString += '<meta property="' + tagName + '" content="' + tagValue + '" />'
74   })
75
76   return htmlStringPage.replace(OPENGRAPH_COMMENT, tagsString)
77 }
78
79 function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
80   const videoId = '' + req.params.id
81   let videoPromise: Promise<VideoInstance>
82
83   // Let Angular application handle errors
84   if (validator.isUUID(videoId, 4)) {
85     videoPromise = db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(videoId)
86   } else if (validator.isInt(videoId)) {
87     videoPromise = db.Video.loadAndPopulateAuthorAndPodAndTags(+videoId)
88   } else {
89     return res.sendFile(indexPath)
90   }
91
92   Promise.all([
93     readFileBufferPromise(indexPath),
94     videoPromise
95   ])
96   .then(([ file, video ]) => {
97     file = file as Buffer
98     video = video as VideoInstance
99
100     const html = file.toString()
101
102     // Let Angular application handle errors
103     if (!video) return res.sendFile(indexPath)
104
105     const htmlStringPageWithTags = addOpenGraphTags(html, video)
106     res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
107   })
108   .catch(err => next(err))
109 }