]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import 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' |
17e69744 | 8 | import { buildFileLocale, getCompleteLocale, is18nLocale, LOCALE_FILES } from '@shared/core-utils/i18n' |
42cc1887 | 9 | import { HttpStatusCode } from '@shared/models' |
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' | |
527a52ac | 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 |
a1eda903 C |
22 | clientsRouter.use([ '/w/p/:id', '/videos/watch/playlist/:id' ], asyncMiddleware(generateWatchPlaylistHtmlPage)) |
23 | clientsRouter.use([ '/w/:id', '/videos/watch/:id' ], asyncMiddleware(generateWatchHtmlPage)) | |
9a911038 K |
24 | clientsRouter.use([ '/accounts/:nameWithHost', '/a/:nameWithHost' ], asyncMiddleware(generateAccountHtmlPage)) |
25 | clientsRouter.use([ '/video-channels/:nameWithHost', '/c/:nameWithHost' ], asyncMiddleware(generateVideoChannelHtmlPage)) | |
26 | clientsRouter.use('/@:nameWithHost', asyncMiddleware(generateActorHtmlPage)) | |
830bcd0f | 27 | |
5abc96fc C |
28 | const 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 | ||
46 | clientsRouter.use('/videos/embed', ...embedMiddlewares) | |
47 | clientsRouter.use('/video-playlists/embed', ...embedMiddlewares) | |
9054a8b6 C |
48 | |
49 | const testEmbedController = (req: express.Request, res: express.Response) => res.sendFile(testEmbedPath) | |
50 | ||
51 | clientsRouter.use('/videos/test-embed', testEmbedController) | |
52 | clientsRouter.use('/video-playlists/test-embed', testEmbedController) | |
830bcd0f | 53 | |
caf2aaf4 | 54 | // Dynamic PWA manifest |
03aa518e | 55 | clientsRouter.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 |
59 | const 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', | |
c80e458a | 68 | 'assets/images/icons/icon-512x512.png', |
69 | 'assets/images/default-playlist.jpg', | |
70 | 'assets/images/default-avatar-account.png', | |
71 | 'assets/images/default-avatar-video-channel.png' | |
caf2aaf4 K |
72 | ] |
73 | ||
74 | for (const staticClientOverride of staticClientOverrides) { | |
75 | const overridePhysicalPath = join(CONFIG.STORAGE.CLIENT_OVERRIDES_DIR, staticClientOverride) | |
76 | clientsRouter.use(`/client/${staticClientOverride}`, asyncMiddleware(serveClientOverride(overridePhysicalPath))) | |
77 | } | |
78 | ||
552d95b1 | 79 | clientsRouter.use('/client/locales/:locale/:file.json', serveServerTranslations) |
cd4cb177 | 80 | clientsRouter.use('/client', express.static(distPath, { maxAge: STATIC_MAX_AGE.CLIENT })) |
552d95b1 C |
81 | |
82 | // 404 for static files not found | |
83 | clientsRouter.use('/client/*', (req: express.Request, res: express.Response) => { | |
76148b27 | 84 | res.status(HttpStatusCode.NOT_FOUND_404).end() |
552d95b1 C |
85 | }) |
86 | ||
87 | // Always serve index client page (the client is a single page application, let it handle routing) | |
88 | // Try to provide the right language index.html | |
89 | clientsRouter.use('/(:language)?', asyncMiddleware(serveIndexHTML)) | |
90 | ||
91 | // --------------------------------------------------------------------------- | |
92 | ||
93 | export { | |
94 | clientsRouter | |
95 | } | |
96 | ||
97 | // --------------------------------------------------------------------------- | |
98 | ||
a1587156 | 99 | function serveServerTranslations (req: express.Request, res: express.Response) { |
7ce44a74 C |
100 | const locale = req.params.locale |
101 | const file = req.params.file | |
102 | ||
bdd428a6 | 103 | if (is18nLocale(locale) && LOCALE_FILES.includes(file)) { |
74b7c6d4 C |
104 | const completeLocale = getCompleteLocale(locale) |
105 | const completeFileLocale = buildFileLocale(completeLocale) | |
cd4cb177 | 106 | |
350131cb | 107 | const path = join(__dirname, `../../../client/dist/locale/${file}.${completeFileLocale}.json`) |
cd4cb177 | 108 | return res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER }) |
e945b184 C |
109 | } |
110 | ||
76148b27 | 111 | return res.status(HttpStatusCode.NOT_FOUND_404).end() |
552d95b1 | 112 | } |
79530164 | 113 | |
cf649c2e | 114 | async function generateEmbedHtmlPage (req: express.Request, res: express.Response) { |
eebd9838 C |
115 | const hookName = req.originalUrl.startsWith('/video-playlists/') |
116 | ? 'filter:html.embed.video-playlist.allowed.result' | |
117 | : 'filter:html.embed.video.allowed.result' | |
118 | ||
119 | const allowParameters = { req } | |
120 | ||
121 | const allowedResult = await Hooks.wrapFun( | |
122 | isEmbedAllowed, | |
123 | allowParameters, | |
124 | hookName | |
125 | ) | |
126 | ||
127 | if (!allowedResult || allowedResult.allowed !== true) { | |
128 | logger.info('Embed is not allowed.', { allowedResult }) | |
129 | ||
130 | return sendHTML(allowedResult?.html || '', res) | |
131 | } | |
132 | ||
cf649c2e C |
133 | const html = await ClientHtml.getEmbedHTML() |
134 | ||
135 | return sendHTML(html, res) | |
136 | } | |
137 | ||
e032aec9 C |
138 | async function generateWatchHtmlPage (req: express.Request, res: express.Response) { |
139 | const html = await ClientHtml.getWatchHTMLPage(req.params.id + '', req, res) | |
830bcd0f | 140 | |
e032aec9 | 141 | return sendHTML(html, res) |
830bcd0f C |
142 | } |
143 | ||
8d987ec6 K |
144 | async function generateWatchPlaylistHtmlPage (req: express.Request, res: express.Response) { |
145 | const html = await ClientHtml.getWatchPlaylistHTMLPage(req.params.id + '', req, res) | |
146 | ||
147 | return sendHTML(html, res) | |
148 | } | |
149 | ||
92bf2f62 C |
150 | async function generateAccountHtmlPage (req: express.Request, res: express.Response) { |
151 | const html = await ClientHtml.getAccountHTMLPage(req.params.nameWithHost, req, res) | |
152 | ||
153 | return sendHTML(html, res) | |
154 | } | |
155 | ||
156 | async function generateVideoChannelHtmlPage (req: express.Request, res: express.Response) { | |
157 | const html = await ClientHtml.getVideoChannelHTMLPage(req.params.nameWithHost, req, res) | |
9a911038 K |
158 | |
159 | return sendHTML(html, res) | |
160 | } | |
161 | ||
162 | async function generateActorHtmlPage (req: express.Request, res: express.Response) { | |
163 | const html = await ClientHtml.getActorHTMLPage(req.params.nameWithHost, req, res) | |
92bf2f62 C |
164 | |
165 | return sendHTML(html, res) | |
166 | } | |
167 | ||
caf2aaf4 K |
168 | async function generateManifest (req: express.Request, res: express.Response) { |
169 | const manifestPhysicalPath = join(root(), 'client', 'dist', 'manifest.webmanifest') | |
1d22d251 | 170 | const manifestJson = await readFile(manifestPhysicalPath, 'utf8') |
caf2aaf4 K |
171 | const manifest = JSON.parse(manifestJson) |
172 | ||
173 | manifest.name = CONFIG.INSTANCE.NAME | |
174 | manifest.short_name = CONFIG.INSTANCE.NAME | |
175 | manifest.description = CONFIG.INSTANCE.SHORT_DESCRIPTION | |
176 | ||
177 | res.json(manifest) | |
178 | } | |
179 | ||
180 | function serveClientOverride (path: string) { | |
181 | return async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
182 | try { | |
183 | await fs.access(path, constants.F_OK) | |
184 | // Serve override client | |
185 | res.sendFile(path, { maxAge: STATIC_MAX_AGE.SERVER }) | |
186 | } catch { | |
187 | // Serve dist client | |
188 | next() | |
189 | } | |
190 | } | |
191 | } | |
eebd9838 C |
192 | |
193 | type AllowedResult = { allowed: boolean, html?: string } | |
194 | function isEmbedAllowed (_object: { | |
195 | req: express.Request | |
196 | }): AllowedResult { | |
197 | return { allowed: true } | |
198 | } |