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