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