]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/client.ts
Translated using Weblate (Spanish)
[github/Chocobozzz/PeerTube.git] / server / controllers / client.ts
... / ...
CommitLineData
1import * as express from 'express'
2import { constants, promises as fs } from 'fs'
3import { readFile } from 'fs-extra'
4import { join } from 'path'
5import { CONFIG } from '@server/initializers/config'
6import { HttpStatusCode } from '@shared/core-utils'
7import { buildFileLocale, getCompleteLocale, is18nLocale, LOCALE_FILES } from '@shared/core-utils/i18n'
8import { root } from '../helpers/core-utils'
9import { STATIC_MAX_AGE } from '../initializers/constants'
10import { ClientHtml, sendHTML, serveIndexHTML } from '../lib/client-html'
11import { asyncMiddleware, embedCSP } from '../middlewares'
12
13const clientsRouter = express.Router()
14
15const distPath = join(root(), 'client', 'dist')
16const testEmbedPath = join(distPath, 'standalone', 'videos', 'test-embed.html')
17
18// Special route that add OpenGraph and oEmbed tags
19// Do not use a template engine for a so little thing
20clientsRouter.use('/videos/watch/playlist/:id', asyncMiddleware(generateWatchPlaylistHtmlPage))
21clientsRouter.use('/videos/watch/:id', asyncMiddleware(generateWatchHtmlPage))
22clientsRouter.use('/accounts/:nameWithHost', asyncMiddleware(generateAccountHtmlPage))
23clientsRouter.use('/video-channels/:nameWithHost', asyncMiddleware(generateVideoChannelHtmlPage))
24
25const embedMiddlewares = [
26 CONFIG.CSP.ENABLED
27 ? embedCSP
28 : (req: express.Request, res: express.Response, next: express.NextFunction) => next(),
29
30 (req: express.Request, res: express.Response, next: express.NextFunction) => {
31 res.removeHeader('X-Frame-Options')
32
33 // Don't cache HTML file since it's an index to the immutable JS/CSS files
34 res.setHeader('Cache-Control', 'public, max-age=0')
35
36 next()
37 },
38
39 asyncMiddleware(generateEmbedHtmlPage)
40]
41
42clientsRouter.use('/videos/embed', ...embedMiddlewares)
43clientsRouter.use('/video-playlists/embed', ...embedMiddlewares)
44
45const testEmbedController = (req: express.Request, res: express.Response) => res.sendFile(testEmbedPath)
46
47clientsRouter.use('/videos/test-embed', testEmbedController)
48clientsRouter.use('/video-playlists/test-embed', testEmbedController)
49
50// Dynamic PWA manifest
51clientsRouter.get('/manifest.webmanifest', asyncMiddleware(generateManifest))
52
53// Static client overrides
54// Must be consistent with static client overrides redirections in /support/nginx/peertube
55const staticClientOverrides = [
56 'assets/images/logo.svg',
57 'assets/images/favicon.png',
58 'assets/images/icons/icon-36x36.png',
59 'assets/images/icons/icon-48x48.png',
60 'assets/images/icons/icon-72x72.png',
61 'assets/images/icons/icon-96x96.png',
62 'assets/images/icons/icon-144x144.png',
63 'assets/images/icons/icon-192x192.png',
64 'assets/images/icons/icon-512x512.png'
65]
66
67for (const staticClientOverride of staticClientOverrides) {
68 const overridePhysicalPath = join(CONFIG.STORAGE.CLIENT_OVERRIDES_DIR, staticClientOverride)
69 clientsRouter.use(`/client/${staticClientOverride}`, asyncMiddleware(serveClientOverride(overridePhysicalPath)))
70}
71
72clientsRouter.use('/client/locales/:locale/:file.json', serveServerTranslations)
73clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE.CLIENT }))
74
75// 404 for static files not found
76clientsRouter.use('/client/*', (req: express.Request, res: express.Response) => {
77 res.sendStatus(HttpStatusCode.NOT_FOUND_404)
78})
79
80// Always serve index client page (the client is a single page application, let it handle routing)
81// Try to provide the right language index.html
82clientsRouter.use('/(:language)?', asyncMiddleware(serveIndexHTML))
83
84// ---------------------------------------------------------------------------
85
86export {
87 clientsRouter
88}
89
90// ---------------------------------------------------------------------------
91
92function serveServerTranslations (req: express.Request, res: express.Response) {
93 const locale = req.params.locale
94 const file = req.params.file
95
96 if (is18nLocale(locale) && LOCALE_FILES.includes(file)) {
97 const completeLocale = getCompleteLocale(locale)
98 const completeFileLocale = buildFileLocale(completeLocale)
99
100 const path = join(__dirname, `../../../client/dist/locale/${file}.${completeFileLocale}.json`)
101 return res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER })
102 }
103
104 return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
105}
106
107async function generateEmbedHtmlPage (req: express.Request, res: express.Response) {
108 const html = await ClientHtml.getEmbedHTML()
109
110 return sendHTML(html, res)
111}
112
113async function generateWatchHtmlPage (req: express.Request, res: express.Response) {
114 const html = await ClientHtml.getWatchHTMLPage(req.params.id + '', req, res)
115
116 return sendHTML(html, res)
117}
118
119async function generateWatchPlaylistHtmlPage (req: express.Request, res: express.Response) {
120 const html = await ClientHtml.getWatchPlaylistHTMLPage(req.params.id + '', req, res)
121
122 return sendHTML(html, res)
123}
124
125async function generateAccountHtmlPage (req: express.Request, res: express.Response) {
126 const html = await ClientHtml.getAccountHTMLPage(req.params.nameWithHost, req, res)
127
128 return sendHTML(html, res)
129}
130
131async function generateVideoChannelHtmlPage (req: express.Request, res: express.Response) {
132 const html = await ClientHtml.getVideoChannelHTMLPage(req.params.nameWithHost, req, res)
133
134 return sendHTML(html, res)
135}
136
137async function generateManifest (req: express.Request, res: express.Response) {
138 const manifestPhysicalPath = join(root(), 'client', 'dist', 'manifest.webmanifest')
139 const manifestJson = await readFile(manifestPhysicalPath, 'utf8')
140 const manifest = JSON.parse(manifestJson)
141
142 manifest.name = CONFIG.INSTANCE.NAME
143 manifest.short_name = CONFIG.INSTANCE.NAME
144 manifest.description = CONFIG.INSTANCE.SHORT_DESCRIPTION
145
146 res.json(manifest)
147}
148
149function serveClientOverride (path: string) {
150 return async (req: express.Request, res: express.Response, next: express.NextFunction) => {
151 try {
152 await fs.access(path, constants.F_OK)
153 // Serve override client
154 res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER })
155 } catch {
156 // Serve dist client
157 next()
158 }
159 }
160}