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