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