]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import { constants, promises as fs } from 'fs' | |
3 | import { readFile } from 'fs-extra' | |
4 | import { join } from 'path' | |
5 | import { logger } from '@server/helpers/logger' | |
6 | import { CONFIG } from '@server/initializers/config' | |
7 | import { Hooks } from '@server/lib/plugins/hooks' | |
8 | import { HttpStatusCode } from '@shared/core-utils' | |
9 | import { buildFileLocale, getCompleteLocale, is18nLocale, LOCALE_FILES } from '@shared/core-utils/i18n' | |
10 | import { root } from '../helpers/core-utils' | |
11 | import { STATIC_MAX_AGE } from '../initializers/constants' | |
12 | import { ClientHtml, sendHTML, serveIndexHTML } from '../lib/client-html' | |
13 | import { asyncMiddleware, embedCSP } from '../middlewares' | |
14 | ||
15 | const clientsRouter = express.Router() | |
16 | ||
17 | const distPath = join(root(), 'client', 'dist') | |
18 | const testEmbedPath = join(distPath, 'standalone', 'videos', 'test-embed.html') | |
19 | ||
20 | // Special route that add OpenGraph and oEmbed tags | |
21 | // Do not use a template engine for a so little thing | |
22 | clientsRouter.use('/videos/watch/playlist/:id', asyncMiddleware(generateWatchPlaylistHtmlPage)) | |
23 | clientsRouter.use('/videos/watch/:id', asyncMiddleware(generateWatchHtmlPage)) | |
24 | clientsRouter.use('/accounts/:nameWithHost', asyncMiddleware(generateAccountHtmlPage)) | |
25 | clientsRouter.use('/video-channels/:nameWithHost', asyncMiddleware(generateVideoChannelHtmlPage)) | |
26 | ||
27 | const embedMiddlewares = [ | |
28 | CONFIG.CSP.ENABLED | |
29 | ? embedCSP | |
30 | : (req: express.Request, res: express.Response, next: express.NextFunction) => next(), | |
31 | ||
32 | // Set headers | |
33 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
34 | res.removeHeader('X-Frame-Options') | |
35 | ||
36 | // Don't cache HTML file since it's an index to the immutable JS/CSS files | |
37 | res.setHeader('Cache-Control', 'public, max-age=0') | |
38 | ||
39 | next() | |
40 | }, | |
41 | ||
42 | asyncMiddleware(generateEmbedHtmlPage) | |
43 | ] | |
44 | ||
45 | clientsRouter.use('/videos/embed', ...embedMiddlewares) | |
46 | clientsRouter.use('/video-playlists/embed', ...embedMiddlewares) | |
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) | |
52 | ||
53 | // Dynamic PWA manifest | |
54 | clientsRouter.get('/manifest.webmanifest', asyncMiddleware(generateManifest)) | |
55 | ||
56 | // Static client overrides | |
57 | // Must be consistent with static client overrides redirections in /support/nginx/peertube | |
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 | ||
75 | clientsRouter.use('/client/locales/:locale/:file.json', serveServerTranslations) | |
76 | clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE.CLIENT })) | |
77 | ||
78 | // 404 for static files not found | |
79 | clientsRouter.use('/client/*', (req: express.Request, res: express.Response) => { | |
80 | res.sendStatus(HttpStatusCode.NOT_FOUND_404) | |
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 | ||
95 | function serveServerTranslations (req: express.Request, res: express.Response) { | |
96 | const locale = req.params.locale | |
97 | const file = req.params.file | |
98 | ||
99 | if (is18nLocale(locale) && LOCALE_FILES.includes(file)) { | |
100 | const completeLocale = getCompleteLocale(locale) | |
101 | const completeFileLocale = buildFileLocale(completeLocale) | |
102 | ||
103 | const path = join(__dirname, `../../../client/dist/locale/${file}.${completeFileLocale}.json`) | |
104 | return res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER }) | |
105 | } | |
106 | ||
107 | return res.sendStatus(HttpStatusCode.NOT_FOUND_404) | |
108 | } | |
109 | ||
110 | async function generateEmbedHtmlPage (req: express.Request, res: express.Response) { | |
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 | ||
129 | const html = await ClientHtml.getEmbedHTML() | |
130 | ||
131 | return sendHTML(html, res) | |
132 | } | |
133 | ||
134 | async function generateWatchHtmlPage (req: express.Request, res: express.Response) { | |
135 | const html = await ClientHtml.getWatchHTMLPage(req.params.id + '', req, res) | |
136 | ||
137 | return sendHTML(html, res) | |
138 | } | |
139 | ||
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 | ||
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 | ||
158 | async function generateManifest (req: express.Request, res: express.Response) { | |
159 | const manifestPhysicalPath = join(root(), 'client', 'dist', 'manifest.webmanifest') | |
160 | const manifestJson = await readFile(manifestPhysicalPath, 'utf8') | |
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 | } | |
182 | ||
183 | type AllowedResult = { allowed: boolean, html?: string } | |
184 | function isEmbedAllowed (_object: { | |
185 | req: express.Request | |
186 | }): AllowedResult { | |
187 | return { allowed: true } | |
188 | } |