diff options
Diffstat (limited to 'server/controllers/client.ts')
-rw-r--r-- | server/controllers/client.ts | 31 |
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 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { join } from 'path' | 2 | import { join } from 'path' |
3 | import * as validator from 'validator' | 3 | import * as validator from 'validator' |
4 | import * as Promise from 'bluebird' | 4 | import * as Bluebird from 'bluebird' |
5 | 5 | ||
6 | import { database as db } from '../initializers/database' | 6 | import { database as db } from '../initializers/database' |
7 | import { | 7 | import { |
@@ -11,6 +11,7 @@ import { | |||
11 | OPENGRAPH_AND_OEMBED_COMMENT | 11 | OPENGRAPH_AND_OEMBED_COMMENT |
12 | } from '../initializers' | 12 | } from '../initializers' |
13 | import { root, readFileBufferPromise, escapeHTML } from '../helpers' | 13 | import { root, readFileBufferPromise, escapeHTML } from '../helpers' |
14 | import { asyncMiddleware } from '../middlewares' | ||
14 | import { VideoInstance } from '../models' | 15 | import { VideoInstance } from '../models' |
15 | 16 | ||
16 | const clientsRouter = express.Router() | 17 | const 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 |
24 | clientsRouter.use('/videos/watch/:id', generateWatchHtmlPage) | 25 | clientsRouter.use('/videos/watch/:id', |
26 | asyncMiddleware(generateWatchHtmlPage) | ||
27 | ) | ||
25 | 28 | ||
26 | clientsRouter.use('/videos/embed', (req: express.Request, res: express.Response, next: express.NextFunction) => { | 29 | clientsRouter.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 | ||
93 | function generateWatchHtmlPage (req: express.Request, res: express.Response, next: express.NextFunction) { | 96 | async 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 | } |