]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/client.ts
Fix auth add cli examples
[github/Chocobozzz/PeerTube.git] / server / controllers / client.ts
CommitLineData
4d4e5cd4 1import * as express from 'express'
bd45d503 2import { constants, promises as fs } from 'fs'
65fcc311 3import { join } from 'path'
bd45d503
C
4import { CONFIG } from '@server/initializers/config'
5import { buildFileLocale, getCompleteLocale, is18nLocale, LOCALE_FILES } from '@shared/core-utils/i18n'
e032aec9 6import { root } from '../helpers/core-utils'
bd45d503 7import { logger } from '../helpers/logger'
34dd7cb4 8import { ACCEPT_HEADERS, STATIC_MAX_AGE } from '../initializers/constants'
e032aec9 9import { ClientHtml } from '../lib/client-html'
bd45d503 10import { asyncMiddleware, embedCSP } from '../middlewares'
830bcd0f 11
65fcc311 12const clientsRouter = express.Router()
830bcd0f 13
e02643f3
C
14const distPath = join(root(), 'client', 'dist')
15const embedPath = join(distPath, 'standalone', 'videos', 'embed.html')
99941732 16const testEmbedPath = join(distPath, 'standalone', 'videos', 'test-embed.html')
830bcd0f 17
d8755eed 18// Special route that add OpenGraph and oEmbed tags
830bcd0f 19// Do not use a template engine for a so little thing
8d987ec6 20clientsRouter.use('/videos/watch/playlist/:id', asyncMiddleware(generateWatchPlaylistHtmlPage))
9aac4423 21clientsRouter.use('/videos/watch/:id', asyncMiddleware(generateWatchHtmlPage))
92bf2f62
C
22clientsRouter.use('/accounts/:nameWithHost', asyncMiddleware(generateAccountHtmlPage))
23clientsRouter.use('/video-channels/:nameWithHost', asyncMiddleware(generateVideoChannelHtmlPage))
830bcd0f 24
5abc96fc
C
25const embedMiddlewares = [
26 CONFIG.CSP.ENABLED
27 ? embedCSP
28 : (req: express.Request, res: express.Response, next: express.NextFunction) => next(),
dfab4fa9 29
9aac4423 30 (req: express.Request, res: express.Response) => {
d00e2393 31 res.removeHeader('X-Frame-Options')
78646451
C
32 // Don't cache HTML file since it's an index to the immutable JS/CSS files
33 res.sendFile(embedPath, { maxAge: 0 })
d00e2393 34 }
5abc96fc
C
35]
36
37clientsRouter.use('/videos/embed', ...embedMiddlewares)
38clientsRouter.use('/video-playlists/embed', ...embedMiddlewares)
9054a8b6
C
39
40const testEmbedController = (req: express.Request, res: express.Response) => res.sendFile(testEmbedPath)
41
42clientsRouter.use('/videos/test-embed', testEmbedController)
43clientsRouter.use('/video-playlists/test-embed', testEmbedController)
830bcd0f 44
79530164 45// Static HTML/CSS/JS client files
78967fca 46const staticClientFiles = [
78967fca
C
47 'ngsw-worker.js',
48 'ngsw.json'
49]
caf2aaf4 50
78967fca
C
51for (const staticClientFile of staticClientFiles) {
52 const path = join(root(), 'client', 'dist', staticClientFile)
78967fca 53
caf2aaf4 54 clientsRouter.get(`/${staticClientFile}`, (req: express.Request, res: express.Response) => {
cd4cb177
C
55 res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER })
56 })
57}
79530164 58
caf2aaf4
K
59// Dynamic PWA manifest
60clientsRouter.get('/manifest.webmanifest', asyncMiddleware(generateManifest))
61
62// Static client overrides
63const staticClientOverrides = [
64 'assets/images/logo.svg',
65 'assets/images/favicon.png',
66 'assets/images/icons/icon-36x36.png',
67 'assets/images/icons/icon-48x48.png',
68 'assets/images/icons/icon-72x72.png',
69 'assets/images/icons/icon-96x96.png',
70 'assets/images/icons/icon-144x144.png',
71 'assets/images/icons/icon-192x192.png',
72 'assets/images/icons/icon-512x512.png'
73]
74
75for (const staticClientOverride of staticClientOverrides) {
76 const overridePhysicalPath = join(CONFIG.STORAGE.CLIENT_OVERRIDES_DIR, staticClientOverride)
77 clientsRouter.use(`/client/${staticClientOverride}`, asyncMiddleware(serveClientOverride(overridePhysicalPath)))
78}
79
552d95b1 80clientsRouter.use('/client/locales/:locale/:file.json', serveServerTranslations)
cd4cb177 81clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE.CLIENT }))
552d95b1
C
82
83// 404 for static files not found
84clientsRouter.use('/client/*', (req: express.Request, res: express.Response) => {
85 res.sendStatus(404)
86})
87
88// Always serve index client page (the client is a single page application, let it handle routing)
89// Try to provide the right language index.html
90clientsRouter.use('/(:language)?', asyncMiddleware(serveIndexHTML))
91
92// ---------------------------------------------------------------------------
93
94export {
95 clientsRouter
96}
97
98// ---------------------------------------------------------------------------
99
a1587156 100function serveServerTranslations (req: express.Request, res: express.Response) {
7ce44a74
C
101 const locale = req.params.locale
102 const file = req.params.file
103
bdd428a6 104 if (is18nLocale(locale) && LOCALE_FILES.includes(file)) {
74b7c6d4
C
105 const completeLocale = getCompleteLocale(locale)
106 const completeFileLocale = buildFileLocale(completeLocale)
cd4cb177 107
350131cb 108 const path = join(__dirname, `../../../client/dist/locale/${file}.${completeFileLocale}.json`)
cd4cb177 109 return res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER })
e945b184
C
110 }
111
112 return res.sendStatus(404)
552d95b1 113}
79530164 114
552d95b1 115async function serveIndexHTML (req: express.Request, res: express.Response) {
989e526a 116 if (req.accepts(ACCEPT_HEADERS) === 'html') {
57c36b27
C
117 try {
118 await generateHTMLPage(req, res, req.params.language)
119 return
120 } catch (err) {
121 logger.error('Cannot generate HTML page.', err)
122 }
989e526a
C
123 }
124
125 return res.status(404).end()
65fcc311 126}
830bcd0f 127
e032aec9 128async function generateHTMLPage (req: express.Request, res: express.Response, paramLang?: string) {
9aac4423 129 const html = await ClientHtml.getDefaultHTMLPage(req, res, paramLang)
989e526a 130
e032aec9 131 return sendHTML(html, res)
989e526a
C
132}
133
e032aec9
C
134async function generateWatchHtmlPage (req: express.Request, res: express.Response) {
135 const html = await ClientHtml.getWatchHTMLPage(req.params.id + '', req, res)
830bcd0f 136
e032aec9 137 return sendHTML(html, res)
830bcd0f
C
138}
139
8d987ec6
K
140async function generateWatchPlaylistHtmlPage (req: express.Request, res: express.Response) {
141 const html = await ClientHtml.getWatchPlaylistHTMLPage(req.params.id + '', req, res)
142
143 return sendHTML(html, res)
144}
145
92bf2f62
C
146async function generateAccountHtmlPage (req: express.Request, res: express.Response) {
147 const html = await ClientHtml.getAccountHTMLPage(req.params.nameWithHost, req, res)
148
149 return sendHTML(html, res)
150}
151
152async function generateVideoChannelHtmlPage (req: express.Request, res: express.Response) {
153 const html = await ClientHtml.getVideoChannelHTMLPage(req.params.nameWithHost, req, res)
154
155 return sendHTML(html, res)
156}
157
e032aec9
C
158function sendHTML (html: string, res: express.Response) {
159 res.set('Content-Type', 'text/html; charset=UTF-8')
eb080476 160
e032aec9 161 return res.send(html)
830bcd0f 162}
caf2aaf4
K
163
164async function generateManifest (req: express.Request, res: express.Response) {
165 const manifestPhysicalPath = join(root(), 'client', 'dist', 'manifest.webmanifest')
166 const manifestJson = await fs.readFile(manifestPhysicalPath, 'utf8')
167 const manifest = JSON.parse(manifestJson)
168
169 manifest.name = CONFIG.INSTANCE.NAME
170 manifest.short_name = CONFIG.INSTANCE.NAME
171 manifest.description = CONFIG.INSTANCE.SHORT_DESCRIPTION
172
173 res.json(manifest)
174}
175
176function serveClientOverride (path: string) {
177 return async (req: express.Request, res: express.Response, next: express.NextFunction) => {
178 try {
179 await fs.access(path, constants.F_OK)
180 // Serve override client
181 res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER })
182 } catch {
183 // Serve dist client
184 next()
185 }
186 }
187}