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