aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/controllers/client.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/controllers/client.ts')
-rw-r--r--server/controllers/client.ts40
1 files changed, 35 insertions, 5 deletions
diff --git a/server/controllers/client.ts b/server/controllers/client.ts
index aff00fe6e..a29b51c51 100644
--- a/server/controllers/client.ts
+++ b/server/controllers/client.ts
@@ -3,17 +3,24 @@ import * as express from 'express'
3import { join } from 'path' 3import { join } from 'path'
4import * as validator from 'validator' 4import * as validator from 'validator'
5import { escapeHTML, readFileBufferPromise, root } from '../helpers/core-utils' 5import { escapeHTML, readFileBufferPromise, root } from '../helpers/core-utils'
6import { CONFIG, EMBED_SIZE, OPENGRAPH_AND_OEMBED_COMMENT, STATIC_MAX_AGE, STATIC_PATHS } from '../initializers' 6import {
7 ACCEPT_HEADERS,
8 CONFIG,
9 EMBED_SIZE,
10 OPENGRAPH_AND_OEMBED_COMMENT,
11 STATIC_MAX_AGE,
12 STATIC_PATHS
13} from '../initializers'
7import { asyncMiddleware } from '../middlewares' 14import { asyncMiddleware } from '../middlewares'
8import { VideoModel } from '../models/video/video' 15import { VideoModel } from '../models/video/video'
9import { VideoPrivacy } from '../../shared/models/videos' 16import { VideoPrivacy } from '../../shared/models/videos'
17import { I18N_LOCALES, is18nLocale, getDefaultLocale } from '../../shared/models'
10 18
11const clientsRouter = express.Router() 19const clientsRouter = express.Router()
12 20
13const distPath = join(root(), 'client', 'dist') 21const distPath = join(root(), 'client', 'dist')
14const assetsImagesPath = join(root(), 'client', 'dist', 'assets', 'images') 22const assetsImagesPath = join(root(), 'client', 'dist', 'assets', 'images')
15const embedPath = join(distPath, 'standalone', 'videos', 'embed.html') 23const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
16const indexPath = join(distPath, 'index.html')
17 24
18// Special route that add OpenGraph and oEmbed tags 25// Special route that add OpenGraph and oEmbed tags
19// Do not use a template engine for a so little thing 26// Do not use a template engine for a so little thing
@@ -45,6 +52,16 @@ clientsRouter.use('/client/*', (req: express.Request, res: express.Response, nex
45 res.sendStatus(404) 52 res.sendStatus(404)
46}) 53})
47 54
55// Always serve index client page (the client is a single page application, let it handle routing)
56// Try to provide the right language index.html
57clientsRouter.use('/(:language)?', function (req, res) {
58 if (req.accepts(ACCEPT_HEADERS) === 'html') {
59 return res.sendFile(getIndexPath(req, req.params.language))
60 }
61
62 return res.status(404).end()
63})
64
48// --------------------------------------------------------------------------- 65// ---------------------------------------------------------------------------
49 66
50export { 67export {
@@ -53,6 +70,19 @@ export {
53 70
54// --------------------------------------------------------------------------- 71// ---------------------------------------------------------------------------
55 72
73function getIndexPath (req: express.Request, paramLang?: string) {
74 let lang: string
75
76 // Check param lang validity
77 if (paramLang && is18nLocale(paramLang)) {
78 lang = paramLang
79 } else {
80 lang = req.acceptsLanguages(Object.keys(I18N_LOCALES)) || getDefaultLocale()
81 }
82
83 return join(__dirname, '../../../client/dist/' + lang + '/index.html')
84}
85
56function addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoModel) { 86function addOpenGraphAndOEmbedTags (htmlStringPage: string, video: VideoModel) {
57 const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName() 87 const previewUrl = CONFIG.WEBSERVER.URL + STATIC_PATHS.PREVIEWS + video.getPreviewName()
58 const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid 88 const videoUrl = CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
@@ -142,18 +172,18 @@ async function generateWatchHtmlPage (req: express.Request, res: express.Respons
142 } else if (validator.isInt(videoId)) { 172 } else if (validator.isInt(videoId)) {
143 videoPromise = VideoModel.loadAndPopulateAccountAndServerAndTags(+videoId) 173 videoPromise = VideoModel.loadAndPopulateAccountAndServerAndTags(+videoId)
144 } else { 174 } else {
145 return res.sendFile(indexPath) 175 return res.sendFile(getIndexPath(req))
146 } 176 }
147 177
148 let [ file, video ] = await Promise.all([ 178 let [ file, video ] = await Promise.all([
149 readFileBufferPromise(indexPath), 179 readFileBufferPromise(getIndexPath(req)),
150 videoPromise 180 videoPromise
151 ]) 181 ])
152 182
153 const html = file.toString() 183 const html = file.toString()
154 184
155 // Let Angular application handle errors 185 // Let Angular application handle errors
156 if (!video || video.privacy === VideoPrivacy.PRIVATE) return res.sendFile(indexPath) 186 if (!video || video.privacy === VideoPrivacy.PRIVATE) return res.sendFile(getIndexPath(req))
157 187
158 const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video) 188 const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video)
159 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags) 189 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)