]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/client.ts
more systemd service hardening (#1488)
[github/Chocobozzz/PeerTube.git] / server / controllers / client.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
65fcc311 2import { join } from 'path'
e032aec9
C
3import { root } from '../helpers/core-utils'
4import { ACCEPT_HEADERS, STATIC_MAX_AGE } from '../initializers'
5e755fff 5import { asyncMiddleware, embedCSP } from '../middlewares'
e032aec9
C
6import { buildFileLocale, getCompleteLocale, is18nLocale, LOCALE_FILES } from '../../shared/models/i18n/i18n'
7import { ClientHtml } from '../lib/client-html'
57c36b27 8import { logger } from '../helpers/logger'
830bcd0f 9
65fcc311 10const clientsRouter = express.Router()
830bcd0f 11
e02643f3 12const distPath = join(root(), 'client', 'dist')
1f30a185 13const assetsImagesPath = join(root(), 'client', 'dist', 'assets', 'images')
e02643f3 14const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
99941732 15const testEmbedPath = join(distPath, 'standalone', 'videos', 'test-embed.html')
830bcd0f 16
d8755eed 17// Special route that add OpenGraph and oEmbed tags
830bcd0f 18// Do not use a template engine for a so little thing
eb080476
C
19clientsRouter.use('/videos/watch/:id',
20 asyncMiddleware(generateWatchHtmlPage)
21)
830bcd0f 22
8afc19a6 23clientsRouter.use('' +
d00e2393 24 '/videos/embed',
5e755fff 25 embedCSP,
d00e2393
RK
26 (req: express.Request, res: express.Response, next: express.NextFunction) => {
27 res.removeHeader('X-Frame-Options')
28 res.sendFile(embedPath)
29 }
30)
99941732
WL
31clientsRouter.use('' +
32 '/videos/test-embed', (req: express.Request, res: express.Response, next: express.NextFunction) => {
33 res.sendFile(testEmbedPath)
34})
830bcd0f 35
79530164 36// Static HTML/CSS/JS client files
78967fca
C
37
38const staticClientFiles = [
fcc7c060 39 'manifest.webmanifest',
78967fca
C
40 'ngsw-worker.js',
41 'ngsw.json'
42]
43for (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
65fcc311 48clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
6bafac54 49clientsRouter.use('/client/assets/images', express.static(assetsImagesPath, { maxAge: STATIC_MAX_AGE }))
79530164 50
e945b184 51clientsRouter.use('/client/locales/:locale/:file.json', function (req, res) {
7ce44a74
C
52 const locale = req.params.locale
53 const file = req.params.file
54
74b7c6d4
C
55 if (is18nLocale(locale) && LOCALE_FILES.indexOf(file) !== -1) {
56 const completeLocale = getCompleteLocale(locale)
57 const completeFileLocale = buildFileLocale(completeLocale)
58 return res.sendFile(join(__dirname, `../../../client/dist/locale/${file}_${completeFileLocale}.json`))
e945b184
C
59 }
60
61 return res.sendStatus(404)
62})
63
79530164 64// 404 for static files not found
075f16ca 65clientsRouter.use('/client/*', (req: express.Request, res: express.Response, next: express.NextFunction) => {
79530164
C
66 res.sendStatus(404)
67})
68
989e526a
C
69// Always serve index client page (the client is a single page application, let it handle routing)
70// Try to provide the right language index.html
57c36b27 71clientsRouter.use('/(:language)?', async function (req, res) {
989e526a 72 if (req.accepts(ACCEPT_HEADERS) === 'html') {
57c36b27
C
73 try {
74 await generateHTMLPage(req, res, req.params.language)
75 return
76 } catch (err) {
77 logger.error('Cannot generate HTML page.', err)
78 }
989e526a
C
79 }
80
81 return res.status(404).end()
82})
83
830bcd0f
C
84// ---------------------------------------------------------------------------
85
65fcc311
C
86export {
87 clientsRouter
88}
830bcd0f
C
89
90// ---------------------------------------------------------------------------
91
e032aec9 92async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
d73c9888 93 const html = await ClientHtml.getIndexHTML(req, res, paramLang)
989e526a 94
e032aec9 95 return sendHTML(html, res)
989e526a
C
96}
97
e032aec9
C
98async function generateWatchHtmlPage (req: express.Request, res: express.Response) {
99 const html = await ClientHtml.getWatchHTMLPage(req.params.id + '', req, res)
830bcd0f 100
e032aec9 101 return sendHTML(html, res)
830bcd0f
C
102}
103
e032aec9
C
104function sendHTML (html: string, res: express.Response) {
105 res.set('Content-Type', 'text/html; charset=UTF-8')
eb080476 106
e032aec9 107 return res.send(html)
830bcd0f 108}