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