]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/client.ts
Fix lint
[github/Chocobozzz/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_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 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 addOpenGraphTags (htmlStringPage: string, video: VideoInstance) {
47 const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName()
48 const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.id
49
50 const metaTags = {
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 let tagsString = ''
69 Object.keys(metaTags).forEach(tagName => {
70 const tagValue = metaTags[tagName]
71
72 tagsString += '<meta property="' + tagName + '" content="' + tagValue + '" />'
73 })
74
75 return htmlStringPage.replace(OPENGRAPH_COMMENT, tagsString)
76 }
77
78 function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
79 const videoId = '' + req.params.id
80 let videoPromise: Promise<VideoInstance>
81
82 // Let Angular application handle errors
83 if (validator.isUUID(videoId, 4)) {
84 videoPromise = db.Video.loadByUUIDAndPopulateAuthorAndPodAndTags(videoId)
85 } else if (validator.isInt(videoId)) {
86 videoPromise = db.Video.loadAndPopulateAuthorAndPodAndTags(+videoId)
87 } else {
88 return res.sendFile(indexPath)
89 }
90
91 Promise.all([
92 readFileBufferPromise(indexPath),
93 videoPromise
94 ])
95 .then(([ file, video ]) => {
96 file = file as Buffer
97 video = video as VideoInstance
98
99 const html = file.toString()
100
101 // Let Angular application handle errors
102 if (!video) return res.sendFile(indexPath)
103
104 const htmlStringPageWithTags = addOpenGraphTags(html, video)
105 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
106 })
107 .catch(err => next(err))
108 }