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