aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/client.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-10-25 11:55:06 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-10-26 09:11:38 +0200
commiteb08047657e739bcd9e592d76307befa3998482b (patch)
treefc309f51ece792fd4307c4af510710a853e1d6b2 /server/controllers/client.ts
parent5f04dd2f743961e0a06c29531cc3ccc9e4928d56 (diff)
downloadPeerTube-eb08047657e739bcd9e592d76307befa3998482b.tar.gz
PeerTube-eb08047657e739bcd9e592d76307befa3998482b.tar.zst
PeerTube-eb08047657e739bcd9e592d76307befa3998482b.zip
Use async/await in controllers
Diffstat (limited to 'server/controllers/client.ts')
-rw-r--r--server/controllers/client.ts31
1 files changed, 16 insertions, 15 deletions
diff --git a/server/controllers/client.ts b/server/controllers/client.ts
index 6a2ac4aab..1391993a7 100644
--- a/server/controllers/client.ts
+++ b/server/controllers/client.ts
@@ -1,7 +1,7 @@
1import * as express from 'express' 1import * as express from 'express'
2import { join } from 'path' 2import { join } from 'path'
3import * as validator from 'validator' 3import * as validator from 'validator'
4import * as Promise from 'bluebird' 4import * as Bluebird from 'bluebird'
5 5
6import { database as db } from '../initializers/database' 6import { database as db } from '../initializers/database'
7import { 7import {
@@ -11,6 +11,7 @@ import {
11 OPENGRAPH_AND_OEMBED_COMMENT 11 OPENGRAPH_AND_OEMBED_COMMENT
12} from '../initializers' 12} from '../initializers'
13import { root, readFileBufferPromise, escapeHTML } from '../helpers' 13import { root, readFileBufferPromise, escapeHTML } from '../helpers'
14import { asyncMiddleware } from '../middlewares'
14import { VideoInstance } from '../models' 15import { VideoInstance } from '../models'
15 16
16const clientsRouter = express.Router() 17const clientsRouter = express.Router()
@@ -21,7 +22,9 @@ const indexPath = join(distPath, 'index.html')
21 22
22// Special route that add OpenGraph and oEmbed tags 23// Special route that add OpenGraph and oEmbed tags
23// Do not use a template engine for a so little thing 24// Do not use a template engine for a so little thing
24clientsRouter.use('/videos/watch/:id', generateWatchHtmlPage) 25clientsRouter.use('/videos/watch/:id',
26 asyncMiddleware(generateWatchHtmlPage)
27)
25 28
26clientsRouter.use('/videos/embed', (req: express.Request, res: express.Response, next: express.NextFunction) => { 29clientsRouter.use('/videos/embed', (req: express.Request, res: express.Response, next: express.NextFunction) => {
27 res.sendFile(embedPath) 30 res.sendFile(embedPath)
@@ -90,9 +93,9 @@ function addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoInstance
90 return htmlStringPage.replace(OPENGRAPH_AND_OEMBED_COMMENT, tagsString) 93 return htmlStringPage.replace(OPENGRAPH_AND_OEMBED_COMMENT, tagsString)
91} 94}
92 95
93function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) { 96async function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) {
94 const videoId = '' + req.params.id 97 const videoId = '' + req.params.id
95 let videoPromise: Promise<VideoInstance> 98 let videoPromise: Bluebird<VideoInstance>
96 99
97 // Let Angular application handle errors 100 // Let Angular application handle errors
98 if (validator.isUUID(videoId, 4)) { 101 if (validator.isUUID(videoId, 4)) {
@@ -103,21 +106,19 @@ function generateWatchHtmlPage (req: express.Request, res: express.Response, nex
103 return res.sendFile(indexPath) 106 return res.sendFile(indexPath)
104 } 107 }
105 108
106 Promise.all([ 109 let [ file, video ] = await Promise.all([
107 readFileBufferPromise(indexPath), 110 readFileBufferPromise(indexPath),
108 videoPromise 111 videoPromise
109 ]) 112 ])
110 .then(([ file, video ]) => {
111 file = file as Buffer
112 video = video as VideoInstance
113 113
114 const html = file.toString() 114 file = file as Buffer
115 video = video as VideoInstance
115 116
116 // Let Angular application handle errors 117 const html = file.toString()
117 if (!video) return res.sendFile(indexPath)
118 118
119 const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video) 119 // Let Angular application handle errors
120 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags) 120 if (!video) return res.sendFile(indexPath)
121 }) 121
122 .catch(err => next(err)) 122 const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video)
123 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)
123} 124}