]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/client.ts
Fix total videos stats
[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'
9import { CONFIG } from '@server/initializers/config'
10
11const clientsRouter = express.Router()
12
13const distPath = join(root(), 'client', 'dist')
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', asyncMiddleware(generateWatchHtmlPage))
20clientsRouter.use('/accounts/:nameWithHost', asyncMiddleware(generateAccountHtmlPage))
21clientsRouter.use('/video-channels/:nameWithHost', asyncMiddleware(generateVideoChannelHtmlPage))
22
23const embedCSPMiddleware = CONFIG.CSP.ENABLED
24 ? embedCSP
25 : (req: express.Request, res: express.Response, next: express.NextFunction) => next()
26
27clientsRouter.use(
28 '/videos/embed',
29 embedCSPMiddleware,
30 (req: express.Request, res: express.Response) => {
31 res.removeHeader('X-Frame-Options')
32 res.sendFile(embedPath)
33 }
34)
35clientsRouter.use(
36 '/videos/test-embed',
37 (req: express.Request, res: express.Response) => res.sendFile(testEmbedPath)
38)
39
40// Static HTML/CSS/JS client files
41
42const staticClientFiles = [
43 'manifest.webmanifest',
44 'ngsw-worker.js',
45 'ngsw.json'
46]
47for (const staticClientFile of staticClientFiles) {
48 const path = join(root(), 'client', 'dist', staticClientFile)
49
50 clientsRouter.get('/' + staticClientFile, (req: express.Request, res: express.Response) => {
51 res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER })
52 })
53}
54
55clientsRouter.use('/client/locales/:locale/:file.json', serveServerTranslations)
56clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE.CLIENT }))
57
58// 404 for static files not found
59clientsRouter.use('/client/*', (req: express.Request, res: express.Response) => {
60 res.sendStatus(404)
61})
62
63// Always serve index client page (the client is a single page application, let it handle routing)
64// Try to provide the right language index.html
65clientsRouter.use('/(:language)?', asyncMiddleware(serveIndexHTML))
66
67// ---------------------------------------------------------------------------
68
69export {
70 clientsRouter
71}
72
73// ---------------------------------------------------------------------------
74
75function serveServerTranslations (req: express.Request, res: express.Response) {
76 const locale = req.params.locale
77 const file = req.params.file
78
79 if (is18nLocale(locale) && LOCALE_FILES.includes(file)) {
80 const completeLocale = getCompleteLocale(locale)
81 const completeFileLocale = buildFileLocale(completeLocale)
82
83 const path = join(__dirname, `../../../client/dist/locale/${file}.${completeFileLocale}.json`)
84 return res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER })
85 }
86
87 return res.sendStatus(404)
88}
89
90async function serveIndexHTML (req: express.Request, res: express.Response) {
91 if (req.accepts(ACCEPT_HEADERS) === 'html') {
92 try {
93 await generateHTMLPage(req, res, req.params.language)
94 return
95 } catch (err) {
96 logger.error('Cannot generate HTML page.', err)
97 }
98 }
99
100 return res.status(404).end()
101}
102
103async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
104 const html = await ClientHtml.getDefaultHTMLPage(req, res, paramLang)
105
106 return sendHTML(html, res)
107}
108
109async function generateWatchHtmlPage (req: express.Request, res: express.Response) {
110 const html = await ClientHtml.getWatchHTMLPage(req.params.id + '', req, res)
111
112 return sendHTML(html, res)
113}
114
115async function generateAccountHtmlPage (req: express.Request, res: express.Response) {
116 const html = await ClientHtml.getAccountHTMLPage(req.params.nameWithHost, req, res)
117
118 return sendHTML(html, res)
119}
120
121async function generateVideoChannelHtmlPage (req: express.Request, res: express.Response) {
122 const html = await ClientHtml.getVideoChannelHTMLPage(req.params.nameWithHost, req, res)
123
124 return sendHTML(html, res)
125}
126
127function sendHTML (html: string, res: express.Response) {
128 res.set('Content-Type', 'text/html; charset=UTF-8')
129
130 return res.send(html)
131}