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