]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/controllers/client.ts
Add plugin translation system
[github/Chocobozzz/PeerTube.git] / server / controllers / client.ts
1 import * as express from 'express'
2 import { join } from 'path'
3 import { root } from '../helpers/core-utils'
4 import { ACCEPT_HEADERS, STATIC_MAX_AGE } from '../initializers/constants'
5 import { asyncMiddleware, embedCSP } from '../middlewares'
6 import { buildFileLocale, getCompleteLocale, is18nLocale, LOCALE_FILES } from '../../shared/models/i18n/i18n'
7 import { ClientHtml } from '../lib/client-html'
8 import { logger } from '../helpers/logger'
9
10 const clientsRouter = express.Router()
11
12 const distPath = join(root(), 'client', 'dist')
13 const assetsImagesPath = join(root(), 'client', 'dist', 'assets', 'images')
14 const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
15 const testEmbedPath = join(distPath, 'standalone', 'videos', 'test-embed.html')
16
17 // Special route that add OpenGraph and oEmbed tags
18 // Do not use a template engine for a so little thing
19 clientsRouter.use('/videos/watch/:id', asyncMiddleware(generateWatchHtmlPage))
20 clientsRouter.use('/accounts/:nameWithHost', asyncMiddleware(generateAccountHtmlPage))
21 clientsRouter.use('/video-channels/:nameWithHost', asyncMiddleware(generateVideoChannelHtmlPage))
22
23 clientsRouter.use(
24 '/videos/embed',
25 embedCSP,
26 (req: express.Request, res: express.Response) => {
27 res.removeHeader('X-Frame-Options')
28 res.sendFile(embedPath)
29 }
30 )
31 clientsRouter.use(
32 '/videos/test-embed',
33 (req: express.Request, res: express.Response) => res.sendFile(testEmbedPath)
34 )
35
36 // Static HTML/CSS/JS client files
37
38 const staticClientFiles = [
39 'manifest.webmanifest',
40 'ngsw-worker.js',
41 'ngsw.json'
42 ]
43 for (const staticClientFile of staticClientFiles) {
44 const path = join(root(), 'client', 'dist', staticClientFile)
45 clientsRouter.use('/' + staticClientFile, express.static(path, { maxAge: STATIC_MAX_AGE }))
46 }
47
48 clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
49 clientsRouter.use('/client/assets/images', express.static(assetsImagesPath, { maxAge: STATIC_MAX_AGE }))
50
51 clientsRouter.use('/client/locales/:locale/:file.json', serveServerTranslations)
52
53 // 404 for static files not found
54 clientsRouter.use('/client/*', (req: express.Request, res: express.Response) => {
55 res.sendStatus(404)
56 })
57
58 // Always serve index client page (the client is a single page application, let it handle routing)
59 // Try to provide the right language index.html
60 clientsRouter.use('/(:language)?', asyncMiddleware(serveIndexHTML))
61
62 // ---------------------------------------------------------------------------
63
64 export {
65 clientsRouter
66 }
67
68 // ---------------------------------------------------------------------------
69
70 async function serveServerTranslations (req: express.Request, res: express.Response) {
71 const locale = req.params.locale
72 const file = req.params.file
73
74 if (is18nLocale(locale) && LOCALE_FILES.indexOf(file) !== -1) {
75 const completeLocale = getCompleteLocale(locale)
76 const completeFileLocale = buildFileLocale(completeLocale)
77 return res.sendFile(join(__dirname, `../../../client/dist/locale/${file}_${completeFileLocale}.json`))
78 }
79
80 return res.sendStatus(404)
81 }
82
83 async function serveIndexHTML (req: express.Request, res: express.Response) {
84 if (req.accepts(ACCEPT_HEADERS) === 'html') {
85 try {
86 await generateHTMLPage(req, res, req.params.language)
87 return
88 } catch (err) {
89 logger.error('Cannot generate HTML page.', err)
90 }
91 }
92
93 return res.status(404).end()
94 }
95
96 async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
97 const html = await ClientHtml.getDefaultHTMLPage(req, res, paramLang)
98
99 return sendHTML(html, res)
100 }
101
102 async function generateWatchHtmlPage (req: express.Request, res: express.Response) {
103 const html = await ClientHtml.getWatchHTMLPage(req.params.id + '', req, res)
104
105 return sendHTML(html, res)
106 }
107
108 async function generateAccountHtmlPage (req: express.Request, res: express.Response) {
109 const html = await ClientHtml.getAccountHTMLPage(req.params.nameWithHost, req, res)
110
111 return sendHTML(html, res)
112 }
113
114 async function generateVideoChannelHtmlPage (req: express.Request, res: express.Response) {
115 const html = await ClientHtml.getVideoChannelHTMLPage(req.params.nameWithHost, req, res)
116
117 return sendHTML(html, res)
118 }
119
120 function sendHTML (html: string, res: express.Response) {
121 res.set('Content-Type', 'text/html; charset=UTF-8')
122
123 return res.send(html)
124 }