aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/controllers/client.ts37
1 files changed, 26 insertions, 11 deletions
diff --git a/server/controllers/client.ts b/server/controllers/client.ts
index 385757fa6..dfffe5487 100644
--- a/server/controllers/client.ts
+++ b/server/controllers/client.ts
@@ -7,8 +7,14 @@ import { ACCEPT_HEADERS, CONFIG, EMBED_SIZE, OPENGRAPH_AND_OEMBED_COMMENT, STATI
7import { asyncMiddleware } from '../middlewares' 7import { asyncMiddleware } from '../middlewares'
8import { VideoModel } from '../models/video/video' 8import { VideoModel } from '../models/video/video'
9import { VideoPrivacy } from '../../shared/models/videos' 9import { VideoPrivacy } from '../../shared/models/videos'
10import { buildFileLocale, getCompleteLocale, getDefaultLocale, is18nLocale } from '../../shared/models' 10import {
11import { LOCALE_FILES } from '../../shared/models/i18n/i18n' 11 buildFileLocale,
12 getCompleteLocale,
13 getDefaultLocale,
14 is18nLocale,
15 LOCALE_FILES,
16 POSSIBLE_LOCALES
17} from '../../shared/models/i18n/i18n'
12 18
13const clientsRouter = express.Router() 19const clientsRouter = express.Router()
14 20
@@ -22,7 +28,8 @@ clientsRouter.use('/videos/watch/:id',
22 asyncMiddleware(generateWatchHtmlPage) 28 asyncMiddleware(generateWatchHtmlPage)
23) 29)
24 30
25clientsRouter.use('/videos/embed', (req: express.Request, res: express.Response, next: express.NextFunction) => { 31clientsRouter.use('' +
32 '/videos/embed', (req: express.Request, res: express.Response, next: express.NextFunction) => {
26 res.sendFile(embedPath) 33 res.sendFile(embedPath)
27}) 34})
28 35
@@ -63,7 +70,7 @@ clientsRouter.use('/client/*', (req: express.Request, res: express.Response, nex
63// Try to provide the right language index.html 70// Try to provide the right language index.html
64clientsRouter.use('/(:language)?', function (req, res) { 71clientsRouter.use('/(:language)?', function (req, res) {
65 if (req.accepts(ACCEPT_HEADERS) === 'html') { 72 if (req.accepts(ACCEPT_HEADERS) === 'html') {
66 return res.sendFile(getIndexPath(req, req.params.language)) 73 return res.sendFile(getIndexPath(req, res, req.params.language))
67 } 74 }
68 75
69 return res.status(404).end() 76 return res.status(404).end()
@@ -77,16 +84,24 @@ export {
77 84
78// --------------------------------------------------------------------------- 85// ---------------------------------------------------------------------------
79 86
80function getIndexPath (req: express.Request, paramLang?: string) { 87function getIndexPath (req: express.Request, res: express.Response, paramLang?: string) {
81 let lang: string 88 let lang: string
82 89
83 // Check param lang validity 90 // Check param lang validity
84 if (paramLang && is18nLocale(paramLang)) { 91 if (paramLang && is18nLocale(paramLang)) {
85 lang = paramLang 92 lang = paramLang
93
94 // Save locale in cookies
95 res.cookie('clientLanguage', lang, {
96 secure: CONFIG.WEBSERVER.SCHEME === 'https',
97 sameSite: true,
98 maxAge: 1000 * 3600 * 24 * 90 // 3 months
99 })
100
101 } else if (req.cookies.clientLanguage && is18nLocale(req.cookies.clientLanguage)) {
102 lang = req.cookies.clientLanguage
86 } else { 103 } else {
87 // lang = req.acceptsLanguages(POSSIBLE_LOCALES) || getDefaultLocale() 104 lang = req.acceptsLanguages(POSSIBLE_LOCALES) || getDefaultLocale()
88 // Disable auto language for now
89 lang = getDefaultLocale()
90 } 105 }
91 106
92 return join(__dirname, '../../../client/dist/' + buildFileLocale(lang) + '/index.html') 107 return join(__dirname, '../../../client/dist/' + buildFileLocale(lang) + '/index.html')
@@ -181,18 +196,18 @@ async function generateWatchHtmlPage (req: express.Request, res: express.Respons
181 } else if (validator.isInt(videoId)) { 196 } else if (validator.isInt(videoId)) {
182 videoPromise = VideoModel.loadAndPopulateAccountAndServerAndTags(+videoId) 197 videoPromise = VideoModel.loadAndPopulateAccountAndServerAndTags(+videoId)
183 } else { 198 } else {
184 return res.sendFile(getIndexPath(req)) 199 return res.sendFile(getIndexPath(req, res))
185 } 200 }
186 201
187 let [ file, video ] = await Promise.all([ 202 let [ file, video ] = await Promise.all([
188 readFileBufferPromise(getIndexPath(req)), 203 readFileBufferPromise(getIndexPath(req, res)),
189 videoPromise 204 videoPromise
190 ]) 205 ])
191 206
192 const html = file.toString() 207 const html = file.toString()
193 208
194 // Let Angular application handle errors 209 // Let Angular application handle errors
195 if (!video || video.privacy === VideoPrivacy.PRIVATE) return res.sendFile(getIndexPath(req)) 210 if (!video || video.privacy === VideoPrivacy.PRIVATE) return res.sendFile(getIndexPath(req, res))
196 211
197 const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video) 212 const htmlStringPageWithTags = addOpenGraphAndOEmbedTags(html, video)
198 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags) 213 res.set('Content-Type', 'text/html; charset=UTF-8').send(htmlStringPageWithTags)