]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/controllers/client.ts
Stop indexing /about/peertube
[github/Chocobozzz/PeerTube.git] / server / controllers / client.ts
CommitLineData
41fb13c3 1import express from 'express'
bd45d503 2import { constants, promises as fs } from 'fs'
1d22d251 3import { readFile } from 'fs-extra'
65fcc311 4import { join } from 'path'
eebd9838 5import { logger } from '@server/helpers/logger'
bd45d503 6import { CONFIG } from '@server/initializers/config'
eebd9838 7import { Hooks } from '@server/lib/plugins/hooks'
17e69744 8import { buildFileLocale, getCompleteLocale, is18nLocale, LOCALE_FILES } from '@shared/core-utils/i18n'
42cc1887 9import { HttpStatusCode } from '@shared/models'
e032aec9 10import { root } from '../helpers/core-utils'
f2eb23cd
RK
11import { STATIC_MAX_AGE } from '../initializers/constants'
12import { ClientHtml, sendHTML, serveIndexHTML } from '../lib/client-html'
42cc1887 13import { asyncMiddleware, disableRobots, embedCSP } from '../middlewares'
830bcd0f 14
65fcc311 15const clientsRouter = express.Router()
830bcd0f 16
e02643f3 17const distPath = join(root(), 'client', 'dist')
99941732 18const testEmbedPath = join(distPath, 'standalone', 'videos', 'test-embed.html')
830bcd0f 19
d8755eed 20// Special route that add OpenGraph and oEmbed tags
830bcd0f 21// Do not use a template engine for a so little thing
a1eda903
C
22clientsRouter.use([ '/w/p/:id', '/videos/watch/playlist/:id' ], asyncMiddleware(generateWatchPlaylistHtmlPage))
23clientsRouter.use([ '/w/:id', '/videos/watch/:id' ], asyncMiddleware(generateWatchHtmlPage))
9a911038
K
24clientsRouter.use([ '/accounts/:nameWithHost', '/a/:nameWithHost' ], asyncMiddleware(generateAccountHtmlPage))
25clientsRouter.use([ '/video-channels/:nameWithHost', '/c/:nameWithHost' ], asyncMiddleware(generateVideoChannelHtmlPage))
26clientsRouter.use('/@:nameWithHost', asyncMiddleware(generateActorHtmlPage))
830bcd0f 27
5abc96fc
C
28const embedMiddlewares = [
29 CONFIG.CSP.ENABLED
30 ? embedCSP
31 : (req: express.Request, res: express.Response, next: express.NextFunction) => next(),
dfab4fa9 32
eebd9838 33 // Set headers
cf649c2e 34 (req: express.Request, res: express.Response, next: express.NextFunction) => {
d00e2393 35 res.removeHeader('X-Frame-Options')
cf649c2e 36
78646451 37 // Don't cache HTML file since it's an index to the immutable JS/CSS files
cf649c2e
C
38 res.setHeader('Cache-Control', 'public, max-age=0')
39
40 next()
41 },
42
43 asyncMiddleware(generateEmbedHtmlPage)
5abc96fc
C
44]
45
46clientsRouter.use('/videos/embed', ...embedMiddlewares)
47clientsRouter.use('/video-playlists/embed', ...embedMiddlewares)
9054a8b6
C
48
49const testEmbedController = (req: express.Request, res: express.Response) => res.sendFile(testEmbedPath)
50
51clientsRouter.use('/videos/test-embed', testEmbedController)
52clientsRouter.use('/video-playlists/test-embed', testEmbedController)
830bcd0f 53
caf2aaf4 54// Dynamic PWA manifest
03aa518e 55clientsRouter.get('/manifest.webmanifest', asyncMiddleware(generateManifest))
caf2aaf4
K
56
57// Static client overrides
8872828d 58// Must be consistent with static client overrides redirections in /support/nginx/peertube
caf2aaf4
K
59const staticClientOverrides = [
60 'assets/images/logo.svg',
61 'assets/images/favicon.png',
62 'assets/images/icons/icon-36x36.png',
63 'assets/images/icons/icon-48x48.png',
64 'assets/images/icons/icon-72x72.png',
65 'assets/images/icons/icon-96x96.png',
66 'assets/images/icons/icon-144x144.png',
67 'assets/images/icons/icon-192x192.png',
68 'assets/images/icons/icon-512x512.png'
69]
70
71for (const staticClientOverride of staticClientOverrides) {
72 const overridePhysicalPath = join(CONFIG.STORAGE.CLIENT_OVERRIDES_DIR, staticClientOverride)
73 clientsRouter.use(`/client/${staticClientOverride}`, asyncMiddleware(serveClientOverride(overridePhysicalPath)))
74}
75
552d95b1 76clientsRouter.use('/client/locales/:locale/:file.json', serveServerTranslations)
cd4cb177 77clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE.CLIENT }))
552d95b1
C
78
79// 404 for static files not found
80clientsRouter.use('/client/*', (req: express.Request, res: express.Response) => {
76148b27 81 res.status(HttpStatusCode.NOT_FOUND_404).end()
552d95b1
C
82})
83
42cc1887
C
84// No index exceptions
85clientsRouter.all('/about/peertube',
86 disableRobots,
87 asyncMiddleware(serveIndexHTML)
88)
89
552d95b1
C
90// Always serve index client page (the client is a single page application, let it handle routing)
91// Try to provide the right language index.html
92clientsRouter.use('/(:language)?', asyncMiddleware(serveIndexHTML))
93
94// ---------------------------------------------------------------------------
95
96export {
97 clientsRouter
98}
99
100// ---------------------------------------------------------------------------
101
a1587156 102function serveServerTranslations (req: express.Request, res: express.Response) {
7ce44a74
C
103 const locale = req.params.locale
104 const file = req.params.file
105
bdd428a6 106 if (is18nLocale(locale) && LOCALE_FILES.includes(file)) {
74b7c6d4
C
107 const completeLocale = getCompleteLocale(locale)
108 const completeFileLocale = buildFileLocale(completeLocale)
cd4cb177 109
350131cb 110 const path = join(__dirname, `../../../client/dist/locale/${file}.${completeFileLocale}.json`)
cd4cb177 111 return res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER })
e945b184
C
112 }
113
76148b27 114 return res.status(HttpStatusCode.NOT_FOUND_404).end()
552d95b1 115}
79530164 116
cf649c2e 117async function generateEmbedHtmlPage (req: express.Request, res: express.Response) {
eebd9838
C
118 const hookName = req.originalUrl.startsWith('/video-playlists/')
119 ? 'filter:html.embed.video-playlist.allowed.result'
120 : 'filter:html.embed.video.allowed.result'
121
122 const allowParameters = { req }
123
124 const allowedResult = await Hooks.wrapFun(
125 isEmbedAllowed,
126 allowParameters,
127 hookName
128 )
129
130 if (!allowedResult || allowedResult.allowed !== true) {
131 logger.info('Embed is not allowed.', { allowedResult })
132
133 return sendHTML(allowedResult?.html || '', res)
134 }
135
cf649c2e
C
136 const html = await ClientHtml.getEmbedHTML()
137
138 return sendHTML(html, res)
139}
140
e032aec9
C
141async function generateWatchHtmlPage (req: express.Request, res: express.Response) {
142 const html = await ClientHtml.getWatchHTMLPage(req.params.id + '', req, res)
830bcd0f 143
e032aec9 144 return sendHTML(html, res)
830bcd0f
C
145}
146
8d987ec6
K
147async function generateWatchPlaylistHtmlPage (req: express.Request, res: express.Response) {
148 const html = await ClientHtml.getWatchPlaylistHTMLPage(req.params.id + '', req, res)
149
150 return sendHTML(html, res)
151}
152
92bf2f62
C
153async function generateAccountHtmlPage (req: express.Request, res: express.Response) {
154 const html = await ClientHtml.getAccountHTMLPage(req.params.nameWithHost, req, res)
155
156 return sendHTML(html, res)
157}
158
159async function generateVideoChannelHtmlPage (req: express.Request, res: express.Response) {
160 const html = await ClientHtml.getVideoChannelHTMLPage(req.params.nameWithHost, req, res)
9a911038
K
161
162 return sendHTML(html, res)
163}
164
165async function generateActorHtmlPage (req: express.Request, res: express.Response) {
166 const html = await ClientHtml.getActorHTMLPage(req.params.nameWithHost, req, res)
92bf2f62
C
167
168 return sendHTML(html, res)
169}
170
caf2aaf4
K
171async function generateManifest (req: express.Request, res: express.Response) {
172 const manifestPhysicalPath = join(root(), 'client', 'dist', 'manifest.webmanifest')
1d22d251 173 const manifestJson = await readFile(manifestPhysicalPath, 'utf8')
caf2aaf4
K
174 const manifest = JSON.parse(manifestJson)
175
176 manifest.name = CONFIG.INSTANCE.NAME
177 manifest.short_name = CONFIG.INSTANCE.NAME
178 manifest.description = CONFIG.INSTANCE.SHORT_DESCRIPTION
179
180 res.json(manifest)
181}
182
183function serveClientOverride (path: string) {
184 return async (req: express.Request, res: express.Response, next: express.NextFunction) => {
185 try {
186 await fs.access(path, constants.F_OK)
187 // Serve override client
188 res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER })
189 } catch {
190 // Serve dist client
191 next()
192 }
193 }
194}
eebd9838
C
195
196type AllowedResult = { allowed: boolean, html?: string }
197function isEmbedAllowed (_object: {
198 req: express.Request
199}): AllowedResult {
200 return { allowed: true }
201}