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