]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/client.ts
Fix crash regarding video stream issue
[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'
5import { asyncMiddleware } 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 assetsImagesPath = join(root(), 'client', 'dist', 'assets', 'images')
14const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
15const 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
19clientsRouter.use('/videos/watch/:id',
20 asyncMiddleware(generateWatchHtmlPage)
21)
22
23clientsRouter.use('' +
24 '/videos/embed',
25 (req: express.Request, res: express.Response, next: express.NextFunction) => {
26 res.removeHeader('X-Frame-Options')
27 res.sendFile(embedPath)
28 }
29)
30clientsRouter.use('' +
31 '/videos/test-embed', (req: express.Request, res: express.Response, next: express.NextFunction) => {
32 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 clientsRouter.use('/' + staticClientFile, express.static(path, { maxAge: STATIC_MAX_AGE }))
45}
46
47clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE }))
48clientsRouter.use('/client/assets/images', express.static(assetsImagesPath, { maxAge: STATIC_MAX_AGE }))
49
50clientsRouter.use('/client/locales/:locale/:file.json', function (req, res) {
51 const locale = req.params.locale
52 const file = req.params.file
53
54 if (is18nLocale(locale) && LOCALE_FILES.indexOf(file) !== -1) {
55 const completeLocale = getCompleteLocale(locale)
56 const completeFileLocale = buildFileLocale(completeLocale)
57 return res.sendFile(join(__dirname, `../../../client/dist/locale/${file}_${completeFileLocale}.json`))
58 }
59
60 return res.sendStatus(404)
61})
62
63// 404 for static files not found
64clientsRouter.use('/client/*', (req: express.Request, res: express.Response, next: express.NextFunction) => {
65 res.sendStatus(404)
66})
67
68// Always serve index client page (the client is a single page application, let it handle routing)
69// Try to provide the right language index.html
70clientsRouter.use('/(:language)?', async function (req, res) {
71 if (req.accepts(ACCEPT_HEADERS) === 'html') {
72 try {
73 await generateHTMLPage(req, res, req.params.language)
74 return
75 } catch (err) {
76 logger.error('Cannot generate HTML page.', err)
77 }
78 }
79
80 return res.status(404).end()
81})
82
83// ---------------------------------------------------------------------------
84
85export {
86 clientsRouter
87}
88
89// ---------------------------------------------------------------------------
90
91async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
92 const html = await ClientHtml.getIndexHTML(req, res, paramLang)
93
94 return sendHTML(html, res)
95}
96
97async function generateWatchHtmlPage (req: express.Request, res: express.Response) {
98 const html = await ClientHtml.getWatchHTMLPage(req.params.id + '', req, res)
99
100 return sendHTML(html, res)
101}
102
103function sendHTML (html: string, res: express.Response) {
104 res.set('Content-Type', 'text/html; charset=UTF-8')
105
106 return res.send(html)
107}