Async signature and various fixes
[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', function (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/*', function (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   let basePreviewUrlHttp
49
50   if (video.isOwned()) {
51     basePreviewUrlHttp = CONFIG.WEBSERVER.URL
52   } else {
53     basePreviewUrlHttp = REMOTE_SCHEME.HTTP + '://' + video.Author.Pod.host
54   }
55
56   // We fetch the remote preview (bigger than the thumbnail)
57   // This should not overhead the remote server since social websites put in a cache the OpenGraph tags
58   // We can't use the thumbnail because these social websites want bigger images (> 200x200 for Facebook for example)
59   const previewUrl = basePreviewUrlHttp + STATIC_PATHS.PREVIEWS + video.getPreviewName()
60   const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.id
61
62   const metaTags = {
63     'og:type': 'video',
64     'og:title': video.name,
65     'og:image': previewUrl,
66     'og:url': videoUrl,
67     'og:description': video.description,
68
69     'name': video.name,
70     'description': video.description,
71     'image': previewUrl,
72
73     'twitter:card': 'summary_large_image',
74     'twitter:site': '@Chocobozzz',
75     'twitter:title': video.name,
76     'twitter:description': video.description,
77     'twitter:image': previewUrl
78   }
79
80   let tagsString = ''
81   Object.keys(metaTags).forEach(function (tagName) {
82     const tagValue = metaTags[tagName]
83
84     tagsString += '<meta property="' + tagName + '" content="' + tagValue + '" />'
85   })
86
87   return htmlStringPage.replace(OPENGRAPH_COMMENT, tagsString)
88 }
89
90 function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
91   const videoId = '' + req.params.id
92
93   // Let Angular application handle errors
94   if (!validator.isUUID(videoId, 4)) return res.sendFile(indexPath)
95
96   Promise.all([
97     readFileBufferPromise(indexPath),
98     db.Video.loadAndPopulateAuthorAndPodAndTags(videoId)
99   ])
100   .then(([ file, video ]) => {
101     file = file as Buffer
102     video = video as VideoInstance
103
104     const html = file.toString()
105
106     // Let Angular application handle errors
107     if (!video) return res.sendFile(indexPath)
108
109     const htmlStringPageWithTags = addOpenGraphTags(html, video)
110     res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
111   })
112   .catch(err => next(err))
113 }