Move to promises
[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 } from '../initializers'
13 import { root, readFileBufferPromise } from '../helpers'
14 import { VideoInstance } from '../models'
15
16 const clientsRouter = express.Router()
17
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')
23
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)
27
28 clientsRouter.use('/videos/embed', function (req: express.Request, res: express.Response, next: express.NextFunction) {
29   res.sendFile(embedPath)
30 })
31
32 // Static HTML/CSS/JS client files
33 clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
34
35 // 404 for static files not found
36 clientsRouter.use('/client/*', function (req: express.Request, res: express.Response, next: express.NextFunction) {
37   res.sendStatus(404)
38 })
39
40 // ---------------------------------------------------------------------------
41
42 export {
43   clientsRouter
44 }
45
46 // ---------------------------------------------------------------------------
47
48 function addOpenGraphTags (htmlStringPage: string, video: VideoInstance) {
49   let basePreviewUrlHttp
50
51   if (video.isOwned()) {
52     basePreviewUrlHttp = CONFIG.WEBSERVER.URL
53   } else {
54     basePreviewUrlHttp = REMOTE_SCHEME.HTTP + '://' + video.Author.Pod.host
55   }
56
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
62
63   const metaTags = {
64     'og:type': 'video',
65     'og:title': video.name,
66     'og:image': previewUrl,
67     'og:url': videoUrl,
68     'og:description': video.description,
69
70     'name': video.name,
71     'description': video.description,
72     'image': previewUrl,
73
74     'twitter:card': 'summary_large_image',
75     'twitter:site': '@Chocobozzz',
76     'twitter:title': video.name,
77     'twitter:description': video.description,
78     'twitter:image': previewUrl
79   }
80
81   let tagsString = ''
82   Object.keys(metaTags).forEach(function (tagName) {
83     const tagValue = metaTags[tagName]
84
85     tagsString += '<meta property="' + tagName + '" content="' + tagValue + '" />'
86   })
87
88   return htmlStringPage.replace(opengraphComment, tagsString)
89 }
90
91 function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
92   const videoId = '' + req.params.id
93
94   // Let Angular application handle errors
95   if (!validator.isUUID(videoId, 4)) return res.sendFile(indexPath)
96
97   Promise.all([
98     readFileBufferPromise(indexPath),
99     db.Video.loadAndPopulateAuthorAndPodAndTags(videoId)
100   ])
101   .then(([ file, video ]) => {
102     file = file as Buffer
103     video = video as VideoInstance
104
105     const html = file.toString()
106
107     // Let Angular application handle errors
108     if (!video) return res.sendFile(indexPath)
109
110     const htmlStringPageWithTags = addOpenGraphTags(html, video)
111     res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
112   })
113   .catch(err => next(err))
114 }